forked from ad-freiburg/qlever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonUtilTest.cpp
117 lines (102 loc) · 4.3 KB
/
JsonUtilTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2023, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Andre Schlegel (July of 2023, [email protected])
#include <gtest/gtest.h>
#include <ostream>
#include "../test/util/GTestHelpers.h"
#include "gtest/gtest.h"
#include "util/File.h"
#include "util/json.h"
/*
@brief Do the `fileToJson` test with the wanted json class type.
*/
template <OrderedOrUnorderedJson WantedJsonClassType>
static void doFileToJsonTest(
ad_utility::source_location l = ad_utility::source_location::current()) {
// For generating better messages, when failing a test.
auto trace{generateLocationTrace(l, "doFileToJsonTest")};
// The `fileToJson` function with the wanted json class type.
const auto& fileToJsonWithWantedType = [](std::string_view jsonFileName) {
return fileToJson<WantedJsonClassType>(jsonFileName);
};
// Creates a file with the given name and content. Can be deleted with
// `ad_utility::deleteFile(fileName)`.
auto createFile = [](std::string_view fileName, auto fileContent) {
std::ofstream tempStream{ad_utility::makeOfstream(fileName)};
tempStream << fileContent << std::endl;
tempStream.close();
};
// The helper function only wants `.json` files.
AD_EXPECT_THROW_WITH_MESSAGE(
fileToJsonWithWantedType("NotAJsonFile.txt"),
::testing::ContainsRegex(R"(NotAJsonFile\.txt.*json file)"));
AD_EXPECT_THROW_WITH_MESSAGE(
fileToJsonWithWantedType("NotAJsonFile.md"),
::testing::ContainsRegex(R"(NotAJsonFile\.md.*json file)"));
AD_EXPECT_THROW_WITH_MESSAGE(
fileToJsonWithWantedType("NotAJsonFile.mp4"),
::testing::ContainsRegex(R"(NotAJsonFile\.mp4.*json file)"));
// File doesn't exist.
AD_EXPECT_THROW_WITH_MESSAGE(
fileToJsonWithWantedType("FileINeverCreated.json"),
::testing::ContainsRegex(R"(Could not open file)"));
// File exists, but doesn't contain valid json.
createFile("NotJson.json", R"("d":4)");
AD_EXPECT_THROW_WITH_MESSAGE(
fileToJsonWithWantedType("NotJson.json"),
::testing::ContainsRegex("could not be parsed as JSON"));
ad_utility::deleteFile("NotJson.json");
// Creates a temporare file, containing the given json object, and checks, if
// `fileToJson` recreates it correctly.
auto makeTempFileAndCompare =
[&createFile, &fileToJsonWithWantedType](const WantedJsonClassType& j) {
// Creating the file.
constexpr std::string_view fileName{"TempTestFile.json"};
createFile(fileName, j);
EXPECT_EQ(j, fileToJsonWithWantedType(fileName));
// Deleting the file.
ad_utility::deleteFile(fileName);
};
makeTempFileAndCompare(
WantedJsonClassType::parse(R"({ "name" : "John Smith",
"sku" : "20223",
"price" : 23.95,
"shipTo" : { "name" : "Jane Smith",
"address" : "123 Maple Street",
"city" : "Pretendville",
"state" : "NY",
"zip" : "12345" },
"billTo" : { "name" : "John Smith",
"address" : "123 Maple Street",
"city" : "Pretendville",
"state" : "NY",
"zip" : "12345" }
})"));
}
TEST(JsonUtilityFunctionTests, fileToJson) {
doFileToJsonTest<nlohmann::json>();
doFileToJsonTest<nlohmann::ordered_json>();
}
TEST(JsonUtilTests, JsonToTypeString) {
// All official data types in json.
ASSERT_EQ("array",
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::array)));
ASSERT_EQ("boolean",
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::boolean)));
ASSERT_EQ("null",
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::null)));
ASSERT_EQ(
"number",
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::number_float)));
ASSERT_EQ("number", jsonToTypeString(nlohmann::json(
nlohmann::json::value_t::number_integer)));
ASSERT_EQ("number", jsonToTypeString(nlohmann::json(
nlohmann::json::value_t::number_unsigned)));
ASSERT_EQ("object",
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::object)));
ASSERT_EQ("string",
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::string)));
// Unofficial types shouldn't work.
ASSERT_ANY_THROW(
jsonToTypeString(nlohmann::json(nlohmann::json::value_t::discarded)));
}