-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more test cases to json dump tests
- Loading branch information
Showing
4 changed files
with
100 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,49 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
#include <memory> | ||
#include <utility> | ||
#include <gmock/gmock.h> | ||
|
||
#include "json.h" | ||
#include "test_utils.h" | ||
|
||
using ::test_utils::TestData; | ||
using ::test_utils::PrepareTestData; | ||
using ::testing::AnyOfArray; | ||
|
||
class JsonDumpsTestingFixture: public ::testing::TestWithParam<std::pair<json::Json, std::string>> {}; | ||
class JsonDumpsTestingFixture: public ::testing::TestWithParam<TestData<json::Json>> {}; | ||
|
||
INSTANTIATE_TEST_SUITE_P( | ||
JsonDumpTests, | ||
JsonDumpsTestingFixture, | ||
::testing::Values( | ||
std::make_pair(json::Json::null(), "null"), | ||
std::make_pair(json::Json(2.0), "2"), | ||
std::make_pair(json::Json({ json::Json(true), json::Json(5.0), json::Json(static_cast<std::string>("Hello")) }), R"([true,5,"Hello"])") | ||
// Primitives. | ||
PrepareTestData<json::Json>(json::Json::null(), { "null" }), | ||
PrepareTestData<json::Json>(json::Json(2.0), { "2" }), | ||
PrepareTestData<json::Json>(json::Json(-1.5), { "-1.5" }), | ||
PrepareTestData<json::Json>(json::Json(true), { "true" }), | ||
PrepareTestData<json::Json>(json::Json(false), { "false" }), | ||
PrepareTestData<json::Json>(json::Json("Hello world"), { R"("Hello world")" }), | ||
PrepareTestData<json::Json>(json::Json::array(), { "[]" }), | ||
PrepareTestData<json::Json>(json::Json::object(), { "{}" }), | ||
|
||
// Arrays. | ||
PrepareTestData<json::Json>(json::Json({ json::Json(true), json::Json(5.0), json::Json(static_cast<std::string>("Hello")) }), { R"([true,5,"Hello"])" }), | ||
|
||
// Objects. | ||
PrepareTestData<json::Json>(json::Json({ | ||
std::make_pair("a", json::Json(true)), | ||
std::make_pair("b", json::Json({ json::Json(0.0), json::Json::null() })), | ||
}), { | ||
R"({"a":true,"b":[0,null]})", | ||
R"({"b":[0,null],"a":true})" | ||
}) | ||
) | ||
); | ||
|
||
TEST_P(JsonDumpsTestingFixture, JsonYieldsValidDump) { | ||
const auto& pair = GetParam(); | ||
|
||
const auto& json = pair.first; | ||
const auto& expected_json = pair.second; | ||
const auto& expected_jsons = pair.second; | ||
|
||
const auto& opt_dumped_json = json::Json::toJson(json); | ||
|
||
EXPECT_EQ(expected_json, opt_dumped_json); | ||
EXPECT_THAT(opt_dumped_json, AnyOfArray(expected_jsons)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef __JSONC_TEST_UTILS_H__ | ||
#define __JSONC_TEST_UTILS_H__ | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace test_utils { | ||
|
||
// Expected value is a vector of strings as JSON specification | ||
// does not provide any gurantees on how keys are ordered within a | ||
// JSON Object, therefore we provide all permutations of keys and | ||
// expect that one of them matches. | ||
template<typename T> | ||
using TestData = std::pair<T, std::vector<std::string>>; | ||
|
||
template<typename T> | ||
inline TestData<T> PrepareTestData(const T& actual, | ||
const std::vector<std::string>& expected) { | ||
return std::make_pair(actual, expected); | ||
} | ||
|
||
} // namespace test_utils | ||
|
||
#endif // __JSONC_TEST_UTILS_H__ |