+ test cases for clear()
This commit is contained in:
parent
b60fe6e7e2
commit
8d4f675151
2 changed files with 83 additions and 13 deletions
|
@ -246,13 +246,13 @@ class JSON
|
||||||
const JSON& at(const int) const;
|
const JSON& at(const int) const;
|
||||||
|
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
inline JSON& operator[](const std::string&);
|
JSON& operator[](const std::string&);
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
JSON& operator[](const char*);
|
JSON& operator[](const char*);
|
||||||
/// operator to get an element in an object
|
/// operator to get an element in an object
|
||||||
const JSON& operator[](const std::string&) const;
|
const JSON& operator[](const std::string&) const;
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
inline JSON& at(const std::string&);
|
JSON& at(const std::string&);
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
JSON& at(const char*);
|
JSON& at(const char*);
|
||||||
/// operator to get an element in an object
|
/// operator to get an element in an object
|
||||||
|
@ -285,7 +285,7 @@ class JSON
|
||||||
/// lexicographically compares the values
|
/// lexicographically compares the values
|
||||||
bool operator==(const JSON&) const noexcept;
|
bool operator==(const JSON&) const noexcept;
|
||||||
/// lexicographically compares the values
|
/// lexicographically compares the values
|
||||||
inline bool operator!=(const JSON&) const noexcept;
|
bool operator!=(const JSON&) const noexcept;
|
||||||
|
|
||||||
/// returns an iterator to the beginning (array/object)
|
/// returns an iterator to the beginning (array/object)
|
||||||
iterator begin() noexcept;
|
iterator begin() noexcept;
|
||||||
|
|
|
@ -158,6 +158,12 @@ TEST_CASE("array")
|
||||||
// add initializer list
|
// add initializer list
|
||||||
j.push_back({"a", "b", "c"});
|
j.push_back({"a", "b", "c"});
|
||||||
CHECK (j.size() == 24);
|
CHECK (j.size() == 24);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
JSON j7 = {0, 1, 2, 3, 4, 5, 6};;
|
||||||
|
CHECK(j7.size() == 7);
|
||||||
|
j7.clear();
|
||||||
|
CHECK(j7.size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Iterators")
|
SECTION("Iterators")
|
||||||
|
@ -338,16 +344,30 @@ TEST_CASE("object")
|
||||||
const JSON k = j;
|
const JSON k = j;
|
||||||
|
|
||||||
// read
|
// read
|
||||||
const std::string v0 = j["k0"];
|
{
|
||||||
CHECK(v0 == "v0");
|
const std::string v0 = j["k0"];
|
||||||
auto v1 = j["k1"];
|
CHECK(v0 == "v0");
|
||||||
CHECK(v1 == nullptr);
|
auto v1 = j["k1"];
|
||||||
int v2 = j["k2"];
|
CHECK(v1 == nullptr);
|
||||||
CHECK(v2 == 42);
|
int v2 = j["k2"];
|
||||||
double v3 = j["k3"];
|
CHECK(v2 == 42);
|
||||||
CHECK(v3 == 3.141);
|
double v3 = j["k3"];
|
||||||
bool v4 = j["k4"];
|
CHECK(v3 == 3.141);
|
||||||
CHECK(v4 == true);
|
bool v4 = j["k4"];
|
||||||
|
CHECK(v4 == true);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string v0 = j[std::string("k0")];
|
||||||
|
CHECK(v0 == "v0");
|
||||||
|
auto v1 = j[std::string("k1")];
|
||||||
|
CHECK(v1 == nullptr);
|
||||||
|
int v2 = j[std::string("k2")];
|
||||||
|
CHECK(v2 == 42);
|
||||||
|
double v3 = j[std::string("k3")];
|
||||||
|
CHECK(v3 == 3.141);
|
||||||
|
bool v4 = j[std::string("k4")];
|
||||||
|
CHECK(v4 == true);
|
||||||
|
}
|
||||||
|
|
||||||
// write (replace)
|
// write (replace)
|
||||||
j["k0"] = "new v0";
|
j["k0"] = "new v0";
|
||||||
|
@ -435,6 +455,12 @@ TEST_CASE("object")
|
||||||
JSON nonarray = 1;
|
JSON nonarray = 1;
|
||||||
CHECK_THROWS_AS(const int i = nonarray["v1"], std::domain_error);
|
CHECK_THROWS_AS(const int i = nonarray["v1"], std::domain_error);
|
||||||
CHECK_THROWS_AS(nonarray["v1"] = 10, std::domain_error);
|
CHECK_THROWS_AS(nonarray["v1"] = 10, std::domain_error);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
JSON j7 = {{"k0", 0}, {"k1", 1}, {"k2", 2}, {"k3", 3}};
|
||||||
|
CHECK(j7.size() == 4);
|
||||||
|
j7.clear();
|
||||||
|
CHECK(j7.size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Iterators")
|
SECTION("Iterators")
|
||||||
|
@ -630,6 +656,14 @@ TEST_CASE("null")
|
||||||
JSON j1 = nullptr;
|
JSON j1 = nullptr;
|
||||||
CHECK(j1.type() == JSON::value_type::null);
|
CHECK(j1.type() == JSON::value_type::null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Operators")
|
||||||
|
{
|
||||||
|
// clear()
|
||||||
|
JSON j1 = nullptr;
|
||||||
|
j1.clear();
|
||||||
|
CHECK(j1 == JSON(nullptr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("string")
|
TEST_CASE("string")
|
||||||
|
@ -699,6 +733,15 @@ TEST_CASE("string")
|
||||||
JSON j3 = std::move(v3);
|
JSON j3 = std::move(v3);
|
||||||
CHECK(j3.get<std::string>() == "Hello, world");
|
CHECK(j3.get<std::string>() == "Hello, world");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Operators")
|
||||||
|
{
|
||||||
|
// clear()
|
||||||
|
JSON j1 = std::string("Hello, world");
|
||||||
|
CHECK(j1.get<std::string>() == "Hello, world");
|
||||||
|
j1.clear();
|
||||||
|
CHECK(j1.get<std::string>() == "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("boolean")
|
TEST_CASE("boolean")
|
||||||
|
@ -765,6 +808,15 @@ TEST_CASE("boolean")
|
||||||
bool v2 = j2;
|
bool v2 = j2;
|
||||||
CHECK(j2.get<bool>() == v2);
|
CHECK(j2.get<bool>() == v2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Operators")
|
||||||
|
{
|
||||||
|
// clear()
|
||||||
|
JSON j1 = true;
|
||||||
|
CHECK(j1.get<bool>() == true);
|
||||||
|
j1.clear();
|
||||||
|
CHECK(j1.get<bool>() == false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("number (int)")
|
TEST_CASE("number (int)")
|
||||||
|
@ -831,6 +883,15 @@ TEST_CASE("number (int)")
|
||||||
int v2 = j2;
|
int v2 = j2;
|
||||||
CHECK(j2.get<int>() == v2);
|
CHECK(j2.get<int>() == v2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Operators")
|
||||||
|
{
|
||||||
|
// clear()
|
||||||
|
JSON j1 = 42;
|
||||||
|
CHECK(j1.get<int>() == 42);
|
||||||
|
j1.clear();
|
||||||
|
CHECK(j1.get<int>() == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("number (float)")
|
TEST_CASE("number (float)")
|
||||||
|
@ -897,6 +958,15 @@ TEST_CASE("number (float)")
|
||||||
double v2 = j2;
|
double v2 = j2;
|
||||||
CHECK(j2.get<double>() == v2);
|
CHECK(j2.get<double>() == v2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Operators")
|
||||||
|
{
|
||||||
|
// clear()
|
||||||
|
JSON j1 = 3.1415926;
|
||||||
|
CHECK(j1.get<double>() == 3.1415926);
|
||||||
|
j1.clear();
|
||||||
|
CHECK(j1.get<double>() == 0.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parser")
|
TEST_CASE("Parser")
|
||||||
|
|
Loading…
Reference in a new issue