proxygen
JsonTestUtilTest.cpp File Reference
#include <stdexcept>
#include <folly/portability/GTest.h>
#include <folly/test/JsonTestUtil.h>

Go to the source code of this file.

Functions

 TEST (CompareJson, Simple)
 
 TEST (CompareJson, Malformed)
 
 TEST (CompareJsonWithTolerance, Simple)
 

Function Documentation

TEST ( CompareJson  ,
Simple   
)

Definition at line 24 of file JsonTestUtilTest.cpp.

References folly::compareJson(), EXPECT_FALSE, EXPECT_TRUE, and FOLLY_EXPECT_JSON_EQ.

24  {
25  constexpr StringPiece json1 = R"({"a": 1, "b": 2})";
26  constexpr StringPiece json2 = R"({"b": 2, "a": 1})";
27  EXPECT_TRUE(compareJson(json1, json2));
28  FOLLY_EXPECT_JSON_EQ(json1, json2);
29 
30  constexpr StringPiece json3 = R"({"b": 3, "a": 1})";
31  EXPECT_FALSE(compareJson(json1, json3));
32 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define FOLLY_EXPECT_JSON_EQ(json1, json2)
Definition: JsonTestUtil.h:68
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool compareJson(StringPiece json1, StringPiece json2)
TEST ( CompareJson  ,
Malformed   
)

Definition at line 34 of file JsonTestUtilTest.cpp.

References folly::compareJson(), and EXPECT_THROW.

34  {
35  constexpr StringPiece json1 = R"({"a": 1, "b": 2})";
36  constexpr StringPiece json2 = R"({"b": 2, "a": 1)";
37  EXPECT_THROW(compareJson(json1, json2), std::runtime_error);
38 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
bool compareJson(StringPiece json1, StringPiece json2)
TEST ( CompareJsonWithTolerance  ,
Simple   
)

Definition at line 40 of file JsonTestUtilTest.cpp.

References folly::compareJsonWithTolerance(), EXPECT_FALSE, EXPECT_TRUE, and FOLLY_EXPECT_JSON_NEAR.

40  {
41  // Use the same tolerance for all tests.
42  auto compare = [](StringPiece obj1, StringPiece obj2) {
43  return compareJsonWithTolerance(obj1, obj2, 0.1);
44  };
45 
46  EXPECT_TRUE(compare("1", "1.05"));
47  EXPECT_FALSE(compare("1", "1.2"));
48 
49  EXPECT_TRUE(compare("true", "true"));
50  EXPECT_FALSE(compare("true", "false"));
51  EXPECT_FALSE(compare("true", "1"));
52 
53  EXPECT_TRUE(compare(R"([1, 2, 3])", R"([1.05, 2, 3.01])"));
54  EXPECT_FALSE(compare(R"([1, 2, 3])", R"([1.2, 2, 3.01])"));
55  EXPECT_FALSE(compare(R"([1, 2, 3])", R"([1, 2])"));
56 
57  EXPECT_TRUE(compare("1.0", "1.05"));
58  EXPECT_FALSE(compare("1.0", "1.2"));
59 
60  EXPECT_TRUE(compare("1", "1"));
61  EXPECT_FALSE(compare("1", "2"));
62 
63  EXPECT_TRUE(compare(R"({"a": 1, "b": 2})", R"({"b": 2.01, "a": 1.05})"));
64  EXPECT_FALSE(compare(R"({"a": 1, "b": 2})", R"({"b": 2.01, "a": 1.2})"));
65  EXPECT_FALSE(compare(R"({"a": 1, "b": 2})", R"({"b": 2})"));
66  EXPECT_FALSE(compare(R"({"a": 1, "b": 2})", R"({"c": 2.01, "a": 1.05})"));
67 
68  EXPECT_TRUE(compare(R"("hello")", R"("hello")"));
69  EXPECT_FALSE(compare(R"("hello")", R"("world")"));
70 
72  R"({"a": 1, "b": 2})", R"({"b": 2.01, "a": 1.05})", 0.1);
73 }
bool compareJsonWithTolerance(StringPiece json1, StringPiece json2, double tolerance)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define FOLLY_EXPECT_JSON_NEAR(json1, json2, tolerance)
Definition: JsonTestUtil.h:71
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862