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.
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.
That doesn't explain json::Parser::FromString("-0")->ToString() == "0"
There was a problem hiding this comment.
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.
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).
| format_unittest \ | ||
| health_checker_unittest \ | ||
| resource_unittest \ | ||
| json_unittest \ |
There was a problem hiding this comment.
Want to keep these in alphabetical order?
test/json_unittest.cc
Outdated
| #include "../src/json.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #define EXPECT_TO_STRING(v, s) EXPECT_EQ(v->ToString(), s) |
test/json_unittest.cc
Outdated
| @@ -0,0 +1,385 @@ | |||
| #include <functional> | |||
There was a problem hiding this comment.
Let's have this after the first two includes (for consistency).
There was a problem hiding this comment.
Let's add a blank line before this one.
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.
The convention seems to be EXPECT_EQ(expected, actual).
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.
The convention seems to be EXPECT_EQ(expected, actual).
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.
Leftover debugging code?
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.
My heartfelt thanks for not using the poop emoji. 😄
|
|
||
| // Number edge cases | ||
|
|
||
| TEST(EdgeTest, PositiveNumbers) { |
There was a problem hiding this comment.
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.
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.
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.
Nested objects, too?
test/json_unittest.cc
Outdated
|
|
||
| // Big tests. | ||
|
|
||
| TEST(BigTest, Realistic) { |
There was a problem hiding this comment.
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")},
});|
@sparkprime Did you mean to push another commit? |
igorpeshansky
left a comment
There was a problem hiding this comment.
Some more comments.
test/json_unittest.cc
Outdated
| @@ -0,0 +1,385 @@ | |||
| #include <functional> | |||
There was a problem hiding this comment.
Let's add a blank line before this one.
test/json_unittest.cc
Outdated
| } | ||
|
|
||
|
|
||
| // Trival Null |
There was a problem hiding this comment.
| 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.
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.
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.
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.
Might as well close the parenthesis on this line. Also in other EXPECT_TOSTRING_EQ calls below.
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.
Let's add a trailing comma, to indicate our preference to that style.
test/json_unittest.cc
Outdated
| ASSERT_THROW(json::Parser::FromString("{\"x\":}"), json::Exception); | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
You can remove one of these two blank lines.
igorpeshansky
left a comment
There was a problem hiding this comment.
Thanks!
LGTM ![]()
Let's squash-and-merge this!
No description provided.