proxygen
json_patch_test.cpp File Reference

Go to the source code of this file.

Classes

class  JsonPatchTest
 

Typedefs

using err_code = folly::json_patch::parse_error_code
 
using op_code = folly::json_patch::patch_operation_code
 

Functions

 TEST_F (JsonPatchTest, ValidPatch)
 
 TEST_F (JsonPatchTest, InvalidPatches)
 

Typedef Documentation

Function Documentation

TEST_F ( JsonPatchTest  ,
ValidPatch   
)

Definition at line 32 of file json_patch_test.cpp.

References folly::json_patch::add, folly::json_patch::copy, EXPECT_EQ, folly::json_patch::move, folly::none, parse(), folly::parseJson(), folly::json_patch::remove, folly::json_patch::replace, and folly::json_patch::test.

32  {
33  // from RFC 6902
34  constexpr folly::StringPiece jsonPatchStr = R"(
35  [
36  { "op": "test", "path": "/a/b/c", "value": "foo" },
37  { "op": "remove", "path": "/a/b/c" },
38  { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
39  { "op": "replace", "path": "/a/b/c", "value": 42 },
40  { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
41  { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
42  ])";
43  auto const expected = std::vector<json_patch::patch_operation>{
45  json_pointer::parse("/a/b/c"),
47  dynamic("foo")},
49  json_pointer::parse("/a/b/c"),
51  folly::none},
52  {op_code::add,
53  json_pointer::parse("/a/b/c"),
55  folly::parseJson(R"(["foo", "bar"])")},
57  json_pointer::parse("/a/b/c"),
59  dynamic(42)},
61  json_pointer::parse("/a/b/d"),
62  json_pointer::parse("/a/b/c"),
63  folly::none},
65  json_pointer::parse("/a/b/e"),
66  json_pointer::parse("/a/b/d"),
67  folly::none}};
68  auto const parsed =
69  json_patch::try_parse(folly::parseJson(jsonPatchStr)).value().ops();
70  EXPECT_EQ(expected, parsed);
71 }
size_t parse(const char *buf, size_t len)
Definition: test.c:1591
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr None none
Definition: Optional.h:87
TEST_F ( JsonPatchTest  ,
InvalidPatches   
)

Definition at line 73 of file json_patch_test.cpp.

References folly::pushmi::operators::error(), EXPECT_EQ, folly::json_patch::invalid_shape, folly::json_patch::malformed_from_attr, folly::json_patch::malformed_op, folly::json_patch::malformed_path_attr, folly::json_patch::missing_from_attr, folly::json_patch::missing_op, folly::json_patch::missing_path_attr, folly::json_patch::missing_value_attr, object, folly::json_patch::overlapping_pointers, folly::parseJson(), and folly::json_patch::unknown_op.

73  {
74  EXPECT_EQ(
76  json_patch::try_parse(dynamic::object()).error().error_code);
77 
78  EXPECT_EQ(
80  json_patch::try_parse(dynamic::array(dynamic::array()))
81  .error()
82  .error_code);
83 
84  EXPECT_EQ(
86  json_patch::try_parse(folly::parseJson(R"([{"path": "/a/b/c"}])"))
87  .error()
88  .error_code);
89 
90  EXPECT_EQ(
92  json_patch::try_parse(
93  folly::parseJson(R"([{"op": "blah", "path": "/a/b/c"}])"))
94  .error()
95  .error_code);
96 
97  EXPECT_EQ(
99  json_patch::try_parse(
100  folly::parseJson(R"([{"op": ["blah"], "path": "/a/b/c"}])"))
101  .error()
102  .error_code);
103 
104  EXPECT_EQ(
106  json_patch::try_parse(folly::parseJson(R"([{"op": "test"}])"))
107  .error()
108  .error_code);
109 
110  EXPECT_EQ(
112  json_patch::try_parse(
113  folly::parseJson(R"([{"op": "test", "path" : "a/z/x"}])"))
114  .error()
115  .error_code);
116 
117  EXPECT_EQ(
119  json_patch::try_parse(
120  folly::parseJson(R"([{"op": "test", "path" : ["a/z/x"]}])"))
121  .error()
122  .error_code);
123 
124  EXPECT_EQ(
126  json_patch::try_parse(
127  folly::parseJson(R"([{"op": "copy", "path" : "/a/b/c"}])"))
128  .error()
129  .error_code);
130 
131  EXPECT_EQ(
133  json_patch::try_parse(
135  R"([{"op": "copy", "from" : "c/d/e", "path" : "/a/b/c"}])"))
136  .error()
137  .error_code);
138 
139  EXPECT_EQ(
141  json_patch::try_parse(
143  R"([{"op": "move", "from" : "/a/b/c", "path" : "/a/b/c"}])"))
144  .error()
145  .error_code);
146 
147  EXPECT_EQ(
149  json_patch::try_parse(
151  R"([{"op": "move", "from" : "/a/b/c", "path" : "/a/b/c/d"}])"))
152  .error()
153  .error_code);
154 
155  // validate presence of mandatory per-operation attributes
156 
157  EXPECT_EQ(
159  json_patch::try_parse(
160  folly::parseJson(R"([{"op": "test", "path" : "/a/b/c"}])"))
161  .error()
162  .error_code);
163 
164  EXPECT_EQ(
166  json_patch::try_parse(
167  folly::parseJson(R"([{"op": "replace", "path" : "/a/b/c"}])"))
168  .error()
169  .error_code);
170 
171  EXPECT_EQ(
173  json_patch::try_parse(
174  folly::parseJson(R"([{"op": "move", "path" : "/a/b/c"}])"))
175  .error()
176  .error_code);
177 
178  EXPECT_EQ(
180  json_patch::try_parse(
181  folly::parseJson(R"([{"op": "copy", "path" : "/a/b/c"}])"))
182  .error()
183  .error_code);
184 
185  // test the object reference in error: in patch below, 3rd entry is incorrect
186 
187  constexpr folly::StringPiece jsonPatchStr = R"(
188  [
189  { "op": "test", "path": "/a/b/c", "value": "foo" },
190  { "op": "remove", "path": "/a/b/c" },
191  { "op": "add", "path": "/a/b/c" }
192  ])";
193  auto jsonObj = folly::parseJson(jsonPatchStr);
194  auto err = json_patch::try_parse(jsonObj).error();
195  EXPECT_EQ(err_code::missing_value_attr, err.error_code);
196  // the invalid entry - check pointers and values they point at
197  EXPECT_EQ(&jsonObj[2], err.obj);
198  EXPECT_EQ(jsonObj[2], *err.obj);
199 }
void * object
Definition: AtFork.cpp:32
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48