json_pointer_test.cpp 1.9 KB

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