1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 <folly/json_pointer.h>
18 #include <folly/portability/GMock.h>
19 #include <folly/portability/GTest.h>
20 
21 using folly::json_pointer;
22 using ::testing::ElementsAreArray;
23 
24 class JsonPointerTest : public ::testing::Test {};
25 
TEST_F(JsonPointerTest,ValidPointers)26 TEST_F(JsonPointerTest, ValidPointers) {
27   EXPECT_THAT(
28       json_pointer::parse("").tokens(),
29       ElementsAreArray(std::vector<std::string>{}));
30   EXPECT_THAT(json_pointer::parse("/").tokens(), ElementsAreArray({""}));
31   EXPECT_THAT(
32       json_pointer::parse("/1/2/3").tokens(),
33       ElementsAreArray({"1", "2", "3"}));
34   EXPECT_THAT(
35       json_pointer::parse("/~0~1/~0/10").tokens(),
36       ElementsAreArray({"~/", "~", "10"}));
37 }
38 
TEST_F(JsonPointerTest,InvalidPointers)39 TEST_F(JsonPointerTest, InvalidPointers) {
40   EXPECT_EQ(
41       json_pointer::parse_error::invalid_first_character,
42       json_pointer::try_parse("a").error());
43   EXPECT_EQ(
44       json_pointer::parse_error::invalid_escape_sequence,
45       json_pointer::try_parse("/~").error());
46   EXPECT_EQ(
47       json_pointer::parse_error::invalid_escape_sequence,
48       json_pointer::try_parse("/~x").error());
49 }
50 
TEST_F(JsonPointerTest,IsPrefixTo)51 TEST_F(JsonPointerTest, IsPrefixTo) {
52   EXPECT_TRUE(
53       json_pointer::parse("/a/b").is_prefix_of(json_pointer::parse("/a/b/c")));
54   EXPECT_FALSE(
55       json_pointer::parse("/a/b").is_prefix_of(json_pointer::parse("/a/d/e")));
56   EXPECT_FALSE(
57       json_pointer::parse("/a/b/c").is_prefix_of(json_pointer::parse("/a/b")));
58 }
59