proxygen
JsonTestUtilTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdexcept>
18 
21 
22 using namespace folly;
23 
24 TEST(CompareJson, Simple) {
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 }
33 
34 TEST(CompareJson, Malformed) {
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 }
39 
40 TEST(CompareJsonWithTolerance, Simple) {
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 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
bool compareJsonWithTolerance(StringPiece json1, StringPiece json2, double tolerance)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define FOLLY_EXPECT_JSON_EQ(json1, json2)
Definition: JsonTestUtil.h:68
#define FOLLY_EXPECT_JSON_NEAR(json1, json2, tolerance)
Definition: JsonTestUtil.h:71
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST(SequencedExecutor, CPUThreadPoolExecutor)
bool compareJson(StringPiece json1, StringPiece json2)