Add JSON Clone() Is() As() testing.#104
Conversation
test/json_unittest.cc
Outdated
| EXPECT_TRUE(v->Is<json::Null>()); | ||
| EXPECT_EQ(json::NullType, v->type()); | ||
| // Check it does not throw exception. | ||
| v->As<json::Null>(); |
There was a problem hiding this comment.
How about EXPECT_EQ(json::NullType, v->As<json::Null>())?
There was a problem hiding this comment.
That doesn't work but is there an EXPECT_DYNAMIC_CAST or something?
There was a problem hiding this comment.
Oops, I meant EXPECT_EQ(json::NullType, v->As<json::Null>()->type()). That's what I get for typing reviews into a tiny window on my phone.
test/json_unittest.cc
Outdated
| json::value v = json::Parser::FromString("null"); | ||
| EXPECT_TRUE(v->Is<json::Null>()); | ||
| EXPECT_EQ(json::NullType, v->type()); | ||
| // Check it does not throw exception. |
There was a problem hiding this comment.
Let's also check that all of the other conversions do...
There was a problem hiding this comment.
That is implicit in the ASSERT_EQ that the other tests have.
There was a problem hiding this comment.
Huh? I'm confused. The contract here is: convert or throw. I was suggesting that we check:
EXPECT_THROW(v->As<json::Boolean>(), json::Exception);
EXPECT_THROW(v->As<json::Number>(), json::Exception);
EXPECT_THROW(v->As<json::String>(), json::Exception);
EXPECT_THROW(v->As<json::Array>(), json::Exception);
EXPECT_THROW(v->As<json::Object>(), json::Exception);And the same for the other cases...
There was a problem hiding this comment.
ah yes I get you now
test/json_unittest.cc
Outdated
| TEST(TrivialParseTest, Null) { | ||
| GuardJsonException([](){ | ||
| json::value v = json::Parser::FromString("null"); | ||
| EXPECT_TRUE(v->Is<json::Null>()); |
There was a problem hiding this comment.
Let's also check that Is<any-other-type>() is false.
There was a problem hiding this comment.
I've added a test that all types are distinct instead, otherwise this gets silly.
There was a problem hiding this comment.
That's separate. You can test that all of the enum values are distinct (which, BTW, we can assert statically in the source — I'll make that change later), but then there's the behavior of the Is template... It would be good to confirm that, e.g., v->Is<json::Number>() is false for a json::Boolean.
test/json_unittest.cc
Outdated
| " \"str\": \"asdfasdf\"\n" | ||
| "}\n"; | ||
|
|
||
| const std::string canonical_json_text = |
There was a problem hiding this comment.
Can't have global non-POD types... This needs to be a constexpr const char[].
test/json_unittest.cc
Outdated
|
|
||
| // Big tests. | ||
|
|
||
| const std::string json_text = |
There was a problem hiding this comment.
Can't have global non-POD types... This needs to be a constexpr const char[].
There was a problem hiding this comment.
Constants should be named kConstantDescription.
test/json_unittest.cc
Outdated
| "\"str\":\"asdfasdf\"" | ||
| "}", | ||
| v); | ||
| EXPECT_TOSTRING_EQ(canonical_json_text, v); |
There was a problem hiding this comment.
I'm not so sure this is a good idea. The expected value now sits far away from the test. Might be better to live with a little duplication...
There was a problem hiding this comment.
Done (inlined in this test only).
| EXPECT_TRUE(v->Is<json::Array>()); | ||
| EXPECT_EQ(json::ArrayType, v->type()); | ||
| EXPECT_TRUE(v->As<json::Array>()->empty()); | ||
| }); |
There was a problem hiding this comment.
For the nonempty case, can you also check that you can extract a json::value using operator[] (i.e., (*array)[0]) and at (i.e., array->at(0))?
There was a problem hiding this comment.
(*array)[0] was already there, added at.
test/json_unittest.cc
Outdated
| json::value v = json::Parser::FromString("{}"); | ||
| EXPECT_TRUE(v->Is<json::Object>()); | ||
| EXPECT_EQ(json::ObjectType, v->type()); | ||
| EXPECT_TRUE(v->As<json::Object>()->empty()); |
There was a problem hiding this comment.
For the nonempty case, can you also check that you can extract a json::value using at (i.e., obj->at("f")) and that obj->at("g") throws std::out_of_bounds?
|
|
||
| namespace { | ||
|
|
||
| void ExpectSerializesToSelf(const std::string& json_text) { |
There was a problem hiding this comment.
This needs a comment...
test/json_unittest.cc
Outdated
| TEST(TrivialParseTest, Null) { | ||
| GuardJsonException([](){ | ||
| json::value v = json::Parser::FromString("null"); | ||
| EXPECT_TRUE(v->Is<json::Null>()); |
There was a problem hiding this comment.
That's separate. You can test that all of the enum values are distinct (which, BTW, we can assert statically in the source — I'll make that change later), but then there's the behavior of the Is template... It would be good to confirm that, e.g., v->Is<json::Number>() is false for a json::Boolean.
test/json_unittest.cc
Outdated
|
|
||
| // Big tests. | ||
|
|
||
| const char kComplexExample[] = |
There was a problem hiding this comment.
constexpr const char kComplexExample[]. Also below.
There was a problem hiding this comment.
You need both, unfortunately. constexpr refers to the array, and const refers to the char.
test/json_unittest.cc
Outdated
|
|
||
| // Big tests. | ||
|
|
||
| const char kComplexExample[] = |
There was a problem hiding this comment.
You need both, unfortunately. constexpr refers to the array, and const refers to the char.
test/json_unittest.cc
Outdated
| #include "gtest/gtest.h" | ||
|
|
||
| #include <functional> | ||
| #include <map> |
| json::NullType, json::BooleanType, json::NumberType, json::StringType, | ||
| json::ArrayType, json::ObjectType | ||
| }); | ||
| std::set<json::Type> all_types_set(all_types.begin(), all_types.end()); |
There was a problem hiding this comment.
You'll want to #include <set> for this...
test/json_unittest.cc
Outdated
| EXPECT_TRUE(v->As<json::Object>()->Has("f")); | ||
| EXPECT_FALSE(v->As<json::Object>()->Has("g")); | ||
| EXPECT_TRUE(obj->Has("f")); | ||
| EXPECT_FALSE(obj->Has("g")); |
There was a problem hiding this comment.
Let's group all "f"-related checks together (and all "g"-related checks to follow).
| EXPECT_FALSE(obj->Has("g")); | ||
| EXPECT_EQ(2.0, obj->Get<json::Number>("f")); | ||
| EXPECT_EQ(2.0, obj->at("f")->As<json::Number>()->value()); | ||
| EXPECT_THROW(obj->Get<json::String>("f"), json::Exception); |
There was a problem hiding this comment.
Might as well check that all Get methods throw except for Get<json::Number>.
| EXPECT_THROW(v->As<json::Boolean>(), json::Exception); | ||
| EXPECT_THROW(v->As<json::Number>(), json::Exception); | ||
| EXPECT_THROW(v->As<json::String>(), json::Exception); | ||
| const auto& arr = v->As<json::Array>(); |
There was a problem hiding this comment.
move this line down - to just before the expect_true
There was a problem hiding this comment.
They're in that order deliberately, as that's the order of complexity of the json types
| EXPECT_THROW(v->As<json::Number>(), json::Exception); | ||
| EXPECT_THROW(v->As<json::String>(), json::Exception); | ||
| const auto& arr = v->As<json::Array>(); | ||
| EXPECT_THROW(v->As<json::Object>(), json::Exception); |
There was a problem hiding this comment.
ditto, move the arr declaration down
test/json_unittest.cc
Outdated
| // Verifies that the given json parses, clones, and serializes to its original | ||
| // form. | ||
| void ExpectSerializesToSelf(const std::string& json_text) { | ||
| json::value v = json::Parser::FromString(json_text); |
test/json_unittest.cc
Outdated
| // Verifies that the given json parses, clones, and serializes to its original | ||
| // form. | ||
| void ExpectSerializesToSelf(const std::string& json_text) { | ||
| json::value v = json::Parser::FromString(json_text); |
No description provided.