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

Conversation

@sparkprime
Copy link
Contributor

@sparkprime sparkprime commented Mar 29, 2018

No description provided.

Copy link
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!

EXPECT_TRUE(v->Is<json::Null>());
EXPECT_EQ(json::NullType, v->type());
// Check it does not throw exception.
v->As<json::Null>();
Copy link
Contributor

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>())?

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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.
Copy link
Contributor

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

Copy link
Contributor Author

@sparkprime sparkprime Mar 30, 2018

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.

Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

TEST(TrivialParseTest, Null) {
GuardJsonException([](){
json::value v = json::Parser::FromString("null");
EXPECT_TRUE(v->Is<json::Null>());
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

" \"str\": \"asdfasdf\"\n"
"}\n";

const std::string canonical_json_text =
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


// Big tests.

const std::string json_text =
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

"\"str\":\"asdfasdf\""
"}",
v);
EXPECT_TOSTRING_EQ(canonical_json_text, v);
Copy link
Contributor

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

Copy link
Contributor Author

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());
});
Copy link
Contributor

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

Copy link
Contributor Author

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.

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());
Copy link
Contributor

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?

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs a comment...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@igorpeshansky igorpeshansky changed the title Add Clone() Is() As() testing Add JSON Clone() Is() As() testing. Mar 30, 2018
Copy link
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.

A couple more.

TEST(TrivialParseTest, Null) {
GuardJsonException([](){
json::value v = json::Parser::FromString("null");
EXPECT_TRUE(v->Is<json::Null>());
Copy link
Contributor

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.


// Big tests.

const char kComplexExample[] =
Copy link
Contributor

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.

Copy link
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
Contributor

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.

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

Almost there.


// Big tests.

const char kComplexExample[] =
Copy link
Contributor

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.

#include "gtest/gtest.h"

#include <functional>
#include <map>
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused?

Copy link
Contributor Author

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());
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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

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

Copy link
Contributor Author

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

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

Copy link
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
Contributor

@supriyagarg supriyagarg left a 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>();
Copy link
Contributor

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

Copy link
Contributor Author

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

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

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

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

2-space indent...

Copy link
Contributor

Choose a reason for hiding this comment

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

Just fixed.

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

LGTM :shipit:

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just fixed.

@igorpeshansky igorpeshansky merged commit e031c55 into master Mar 31, 2018
@igorpeshansky igorpeshansky deleted the dcunnin-more-json-unittest branch March 31, 2018 05:35
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