-
Notifications
You must be signed in to change notification settings - Fork 11
Conversation
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about EXPECT_EQ(json::NullType, v->As<json::Null>())?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work but is there an EXPECT_DYNAMIC_CAST or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also check that all of the other conversions do...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is implicit in the ASSERT_EQ that the other tests have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes I get you now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also check that Is<any-other-type>() is false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a test that all types are distinct instead, otherwise this gets silly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/json_unittest.cc
Outdated
| " \"str\": \"asdfasdf\"\n" | ||
| "}\n"; | ||
|
|
||
| const std::string canonical_json_text = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't have global non-POD types... This needs to be a constexpr const char[].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/json_unittest.cc
Outdated
|
|
||
| // Big tests. | ||
|
|
||
| const std::string json_text = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't have global non-POD types... This needs to be a constexpr const char[].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants should be named kConstantDescription.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/json_unittest.cc
Outdated
| "\"str\":\"asdfasdf\"" | ||
| "}", | ||
| v); | ||
| EXPECT_TOSTRING_EQ(canonical_json_text, v); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(*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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
|
||
| namespace { | ||
|
|
||
| void ExpectSerializesToSelf(const std::string& json_text) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a comment...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr const char kComplexExample[]. Also below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need both, unfortunately. constexpr refers to the array, and const refers to the char.
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there.
test/json_unittest.cc
Outdated
|
|
||
| // Big tests. | ||
|
|
||
| const char kComplexExample[] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to #include <set> for this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's group all "f"-related checks together (and all "g"-related checks to follow).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well check that all Get methods throw except for Get<json::Number>.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
supriyagarg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this line down - to just before the expect_true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2-space indent...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just fixed.
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ![]()
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just fixed.
No description provided.