Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.

Add unit tests for JSON parsing & serialization.#92

Merged
igorpeshansky merged 8 commits intomasterfrom
dcunnin-json-tests
Mar 27, 2018
Merged

Add unit tests for JSON parsing & serialization.#92
igorpeshansky merged 8 commits intomasterfrom
dcunnin-json-tests

Conversation

@sparkprime
Copy link
Copy Markdown
Contributor

No description provided.

@sparkprime
Copy link
Copy Markdown
Contributor Author

@igorpeshansky

Comment thread test/json_unittest.cc
-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.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

@sparkprime sparkprime Mar 27, 2018

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"

Copy link
Copy Markdown
Contributor

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...

Comment thread test/json_unittest.cc Outdated
// String edge cases

/*
* TODO(igorp): Seems that yajl does not support valid JSON.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments.

Comment thread test/Makefile
format_unittest \
health_checker_unittest \
resource_unittest \
json_unittest \
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread test/json_unittest.cc Outdated
#include "../src/json.h"
#include "gtest/gtest.h"

#define EXPECT_TO_STRING(v, s) EXPECT_EQ(v->ToString(), s)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPECT_TOSTRING_EQ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread test/json_unittest.cc Outdated
@@ -0,0 +1,385 @@
#include <functional>
Copy link
Copy Markdown
Contributor

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread 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);
Copy link
Copy Markdown
Contributor

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread test/json_unittest.cc Outdated
#include "../src/json.h"
#include "gtest/gtest.h"

#define EXPECT_TO_STRING(v, s) EXPECT_EQ(v->ToString(), s)
Copy link
Copy Markdown
Contributor

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread 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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover debugging code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread test/json_unittest.cc Outdated
TEST(EdgeTest, StringWithUTF8) {
GuardJsonException([](){
// This is Korean for "Hello World!".
std::unique_ptr<json::Value> v = json::Parser::FromString("\"안녕 세상아!\"");
Copy link
Copy Markdown
Contributor

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. 😄

Comment thread test/json_unittest.cc

// Number edge cases

TEST(EdgeTest, PositiveNumbers) {
Copy link
Copy Markdown
Contributor

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,
    });

?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Comment thread test/json_unittest.cc Outdated
});
}

TEST(BigTest, LotsOfNesting) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested objects, too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread test/json_unittest.cc Outdated

// Big tests.

TEST(BigTest, Realistic) {
Copy link
Copy Markdown
Contributor

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")},
    });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@igorpeshansky
Copy link
Copy Markdown
Contributor

@sparkprime Did you mean to push another commit?

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more comments.

Comment thread test/json_unittest.cc Outdated
@@ -0,0 +1,385 @@
#include <functional>
Copy link
Copy Markdown
Contributor

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.

Comment thread test/json_unittest.cc Outdated
}


// Trival Null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread test/json_unittest.cc
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"));
Copy link
Copy Markdown
Contributor

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"));

Comment thread test/json_unittest.cc

// Number edge cases

TEST(EdgeTest, PositiveNumbers) {
Copy link
Copy Markdown
Contributor

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.

Comment thread test/json_unittest.cc
-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.
Copy link
Copy Markdown
Contributor

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...

Comment thread test/json_unittest.cc Outdated
"123.0,123.45600000000000307,789.0,"
"1.0500000000000000444,1.9989999999999998999e-99"
"]",
v
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor stuff.

Comment thread test/json_unittest.cc Outdated
TEST(TrivialToStringTest, ObjectOneField) {
GuardJsonException([](){
json::value v = json::object({
{"f", json::number(2.0)}
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread test/json_unittest.cc Outdated
ASSERT_THROW(json::Parser::FromString("{\"x\":}"), json::Exception);
}


Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
LGTM :shipit:

Let's squash-and-merge this!

Copy link
Copy Markdown
Contributor

@bmoyles0117 bmoyles0117 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@igorpeshansky igorpeshansky merged commit 2aecc4d into master Mar 27, 2018
@igorpeshansky igorpeshansky deleted the dcunnin-json-tests branch March 27, 2018 23:10
dhrupadb pushed a commit that referenced this pull request Mar 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants