-
Notifications
You must be signed in to change notification settings - Fork 11
Add unit tests for JSON parsing & serialization. #92
Conversation
ee4bacd to
0db257e
Compare
| -10.0, -10.0e+0, -10.0e-0, -10.0e0, -10.0E+0, -10.0E-0, -10.0E0, | ||
| -123, -123.456, -789, -1.05, -1.999e-99, | ||
| })); | ||
| // TODO(igorp): This first one looks spurious. |
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.
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.
Sorry, which first one? Do you mean -0 transforming to 0.0? That's because with int, there is no separate representation for -0, so it gets converted to static_cast<double>(0). Nothing to do here.
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 explain json::Parser::FromString("-0")->ToString() == "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.
Hmm, true. Ok, warrants further investigation. Let's remove the username from the TODO, though, and add what you just said...
test/json_unittest.cc
Outdated
| // String edge cases | ||
|
|
||
| /* | ||
| * TODO(igorp): Seems that yajl does not support valid JSON. |
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.
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.
There's already an issue for this. Let's just change this to:
// TODO: yajl doesn't support newlines in strings (https://github.com/lloyd/yajl/issues/180).
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.
Some comments.
| format_unittest \ | ||
| health_checker_unittest \ | ||
| resource_unittest \ | ||
| json_unittest \ |
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.
Want to keep these in alphabetical order?
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
| #include "../src/json.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #define EXPECT_TO_STRING(v, s) EXPECT_EQ(v->ToString(), s) |
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.
EXPECT_TOSTRING_EQ?
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
| @@ -0,0 +1,385 @@ | |||
| #include <functional> | |||
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 have this after the first two includes (for consistency).
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.
Let's add a blank line before this one.
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([](){ | ||
| std::unique_ptr<json::Value> v = json::Parser::FromString("null"); | ||
| EXPECT_EQ(v->type(), json::NullType); |
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.
The convention seems to be EXPECT_EQ(expected, actual).
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
| #include "../src/json.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #define EXPECT_TO_STRING(v, s) EXPECT_EQ(v->ToString(), s) |
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.
The convention seems to be EXPECT_EQ(expected, actual).
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(EdgeTest, StringWithUnicodeEscape) { | ||
| GuardJsonException([](){ | ||
| std::unique_ptr<json::Value> v = json::Parser::FromString("\"foo\\u000abar\""); | ||
| //EXPECT_EQ(v->As<json::String>()->value(), "foo\nbar"); |
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.
Leftover debugging code?
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(EdgeTest, StringWithUTF8) { | ||
| GuardJsonException([](){ | ||
| // This is Korean for "Hello World!". | ||
| std::unique_ptr<json::Value> v = json::Parser::FromString("\"안녕 세상아!\""); |
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.
My heartfelt thanks for not using the poop emoji. 😄
|
|
||
| // Number edge cases | ||
|
|
||
| TEST(EdgeTest, PositiveNumbers) { |
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.
Is this a parse/ToString test? Would it make sense to also test JSON construction:
json::value v = json::array({
0, 0e+0, 0e-0, 0e0, 0E+0, 0E-0, 0E0,
0.0, 0.0e+0, 0.0e-0, 0.0e0, 0.0E+0, 0.0E-0, 0.0E0,
10, 10e+0, 10e-0, 10e0, 10E+0, 10E-0, 10E0,
10.0, 10.0e+0, 10.0e-0, 10.0e0, 10.0E+0, 10.0E-0, 10.0E0,
123, 123.456, 789, 1.05, 1.999e-99,
});?
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 testing the C++ parser though. I could have split it into two parts, but since we do a lot of that already (and therefore ensure good coverage) I figured an end-to-end test is enough here.
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 is also testing the contract provided by those functions and constructors... Let's do this in the RealisticConstruction tests.
test/json_unittest.cc
Outdated
| }); | ||
| } | ||
|
|
||
| TEST(BigTest, LotsOfNesting) { |
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.
Nested objects, too?
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. | ||
|
|
||
| TEST(BigTest, Realistic) { |
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'd also check construction:
json::value v = json::object({
{"foo", json::array({json::number(1), json::number(2), json::number(3)})},
{"bar", json::object({{"x", json::number(0)}, {"y", json::null()}})},
{"baz", json::boolean(true)},
{"str", json::string("asdfasdf")},
});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.
|
@sparkprime Did you mean to push another commit? |
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.
Some more comments.
test/json_unittest.cc
Outdated
| @@ -0,0 +1,385 @@ | |||
| #include <functional> | |||
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 add a blank line before this one.
test/json_unittest.cc
Outdated
| } | ||
|
|
||
|
|
||
| // Trival 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.
| json::value v = json::Parser::FromString("{\"f\":2}"); | ||
| const auto& obj = v->As<json::Object>(); | ||
| EXPECT_EQ(1, obj->size()); | ||
| EXPECT_EQ(2.0, obj->Get<json::Number>("f")); |
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 add this here or in a separate test:
EXPECT_TRUE(v->As<json::Object>()->Has("f"));
EXPECT_FALSE(v->As<json::Object>()->Has("g"));|
|
||
| // Number edge cases | ||
|
|
||
| TEST(EdgeTest, PositiveNumbers) { |
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 is also testing the contract provided by those functions and constructors... Let's do this in the RealisticConstruction tests.
| -10.0, -10.0e+0, -10.0e-0, -10.0e0, -10.0E+0, -10.0E-0, -10.0E0, | ||
| -123, -123.456, -789, -1.05, -1.999e-99, | ||
| })); | ||
| // TODO(igorp): This first one looks spurious. |
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.
Hmm, true. Ok, warrants further investigation. Let's remove the username from the TODO, though, and add what you just said...
test/json_unittest.cc
Outdated
| "123.0,123.45600000000000307,789.0," | ||
| "1.0500000000000000444,1.9989999999999998999e-99" | ||
| "]", | ||
| 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.
Might as well close the parenthesis on this line. Also in other EXPECT_TOSTRING_EQ calls 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.
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.
Minor stuff.
test/json_unittest.cc
Outdated
| TEST(TrivialToStringTest, ObjectOneField) { | ||
| GuardJsonException([](){ | ||
| json::value v = json::object({ | ||
| {"f", json::number(2.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.
Let's add a trailing comma, to indicate our preference to that style.
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
| ASSERT_THROW(json::Parser::FromString("{\"x\":}"), 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.
You can remove one of these two blank lines.
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.
Thanks!
LGTM ![]()
Let's squash-and-merge this!
bmoyles0117
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
No description provided.