Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aimed at fixing compilation error https://github.com/nlohmann/json/issues/4197. It was a school project, asking me to make contributions. Sorry for the inconvenience I made. #4232

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ cxx_standard = $(lastword c++11 $(filter c++%, $(subst ., ,$1)))
%.output: %.cpp
@echo "standard $(call cxx_standard $(<:.cpp=))"
$(MAKE) $(<:.cpp=) \
CPPFLAGS="-I $(SRCDIR) -DJSON_USE_GLOBAL_UDLS=0" \
CXXFLAGS="-std=$(call cxx_standard,$(<:.cpp=)) -Wno-deprecated-declarations"

CPPFLAGS="-I ../single_include -DJSON_USE_GLOBAL_UDLS=0" \
CXXFLAGS="-std=c++2a -Wno-deprecated-declarations"
CPPFLAGS += -include <compare>
examples/operator_spaceship__const_reference.c++20: examples/operator_spaceship__const_reference.c++20.cpp $(CXX) $(CXXFLAGS) $(CPPFLAGS) $< -o $@
./$(<:.cpp=) > $@
rm $(<:.cpp=)

Expand All @@ -41,5 +44,6 @@ check_output_portable: $(filter-out examples/meta.test examples/max_size.test ex

clean:
rm -fr $(EXAMPLES:.cpp=)
rm -f examples/operator_spaceship__const_reference.c++20
$(MAKE) clean -C docset
$(MAKE) clean -C mkdocs
92 changes: 92 additions & 0 deletions tests/src/unit-make1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++ (supporting code)
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT

#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
#include <iostream>
using nlohmann::json;

TEST_SUITE("nlohmann/json test suite - Extended")
{
TEST_CASE("Nested Structures")
{
SECTION("Nested Objects")
{
json nested_object = {
{"person", {
{"name", "Bob"},
{"age", 40},
{"address", {
{"city", "Example City"},
{"zip", "12345"}
}}
}}
};

CHECK(nested_object["person"]["name"] == "Bob");
CHECK(nested_object["person"]["address"]["city"] == "Example City");
}

SECTION("Nested Arrays")
{
json nested_array = {
{"numbers", {1, 2, {3, 4}, 5}}
};

CHECK(nested_array["numbers"][2][1] == 4);
}
}

TEST_CASE("Exception Handling")
{
SECTION("Parsing Invalid JSON")
{
// Expecting a parse error for invalid JSON
CHECK_THROWS_AS(json::parse("invalid_json_string"), json::parse_error);
}

SECTION("Accessing Nonexistent Key")
{
json object = {{"name", "Alice"}, {"age", 25}};

// Expecting an exception when accessing a nonexistent key
CHECK_THROWS_AS(object.at("nonexistent_key"), json::out_of_range);
}
}

TEST_CASE("Additional Serialization and Deserialization")
{
SECTION("Serialize and Deserialize with Custom Format")
{
json data = {{"key1", 42}, {"key2", "value"}};

// Serialize with indentation for human-readable format
std::string serialized = data.dump(2);

// Deserialize the serialized string
json parsed = json::parse(serialized);

CHECK(parsed == data);
}

// SECTION("Deserialize from Stream")
// {
// std::istringstream stream(R"({"name": "Charlie", "age": 35})");

// // Deserialize from the input stream
// json parsed;
// stream >> parsed;

// CHECK(parsed["name"] == "Charlie");
// CHECK(parsed["age"] == 35);
// }
}

// Add more test cases and sections as needed to cover other functionalities.
}
58 changes: 58 additions & 0 deletions tests/src/unit-make2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++ (supporting code)
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT

#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
#include <iostream>
using nlohmann::json;

TEST_SUITE("nlohmann/json test suite")
{
TEST_CASE("Basic JSON Operations")
{
SECTION("Construction and Type Checks")
{
json number = 42;
json text = "Hello, JSON!";
json array = {1, 2, 3};
json object = {{"key", "value"}};

CHECK(number.is_number());
CHECK(text.is_string());
CHECK(array.is_array());
CHECK(object.is_object());
}

SECTION("Serialization and Deserialization")
{
json original = {{"name", "John"}, {"age", 30}};
std::string serialized = original.dump();
json parsed = json::parse(serialized);

CHECK(parsed == original);
}

SECTION("Array and Object Operations")
{
json array = {1, 2, 3};
array.push_back(4);
array.insert(array.begin() + 1, 10);

CHECK(array.size() == 5);
CHECK(array[1] == 10);

json object = {{"name", "Alice"}, {"age", 25}};
object["city"] = "Wonderland";

CHECK(object["city"] == "Wonderland");
}
}

// Add more test cases and sections as needed to cover other functionalities.
}
57 changes: 57 additions & 0 deletions tests/src/unit-make3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++ (supporting code)
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT

#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
#include <iostream>
using nlohmann::json;

TEST_SUITE("nlohmann/json test suite - More")
{
TEST_CASE("Comparing JSON Objects")
{
SECTION("Ordering of Object Keys Matters")
{
json object1 = {
{"name", "Alice"},
{"age", 25},
{"city", "Wonderland"}
};

json object2 = {
{"name", "Alice"},
{"city", "Wonderland"},
{"age", 25}
};

// Expecting the objects to be different due to key order
CHECK(object1 != object2);
}

SECTION("Ordering of Object Keys Doesn't Matter")
{
json object1 = {
{"name", "Bob"},
{"age", 30},
{"city", "Example City"}
};

json object2 = {
{"age", 30},
{"name", "Bob"},
{"city", "Example City"}
};

// Expecting the objects to be considered equal
CHECK(object1 == object2);
}
}

// Add more test cases and sections as needed to cover other functionalities.
}