|
| 1 | +namespace ByteBard.AsyncAPI.Tests |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Text.Json.Nodes; |
| 5 | + using ByteBard.AsyncAPI.Exceptions; |
| 6 | + using ByteBard.AsyncAPI.Readers.ParseNodes; |
| 7 | + using FluentAssertions; |
| 8 | + using NUnit.Framework; |
| 9 | + |
| 10 | + internal class JsonPointer_Should |
| 11 | + { |
| 12 | + // The example document from RFC 6901 section 5. |
| 13 | + private const string RfcDocument = |
| 14 | + """ |
| 15 | + { |
| 16 | + "foo": ["bar", "baz"], |
| 17 | + "": 0, |
| 18 | + "a/b": 1, |
| 19 | + "c%d": 2, |
| 20 | + "e^f": 3, |
| 21 | + "g|h": 4, |
| 22 | + "i\\j": 5, |
| 23 | + "k\"l": 6, |
| 24 | + " ": 7, |
| 25 | + "m~n": 8 |
| 26 | + } |
| 27 | + """; |
| 28 | + |
| 29 | + [TestCase("/foo/0", "\"bar\"")] |
| 30 | + [TestCase("/", "0")] |
| 31 | + [TestCase("/a~1b", "1")] |
| 32 | + [TestCase("/c%d", "2")] |
| 33 | + [TestCase("/e^f", "3")] |
| 34 | + [TestCase("/g|h", "4")] |
| 35 | + [TestCase("/i\\j", "5")] |
| 36 | + [TestCase("/k\"l", "6")] |
| 37 | + [TestCase("/ ", "7")] |
| 38 | + [TestCase("/m~0n", "8")] |
| 39 | + public void Find_ResolvesTheRfcStringRepresentationExamples(string pointer, string expected) |
| 40 | + { |
| 41 | + var json = JsonNode.Parse(RfcDocument); |
| 42 | + |
| 43 | + new JsonPointer(pointer).Find(json).ToJsonString().Should().Be(expected); |
| 44 | + } |
| 45 | + |
| 46 | + [TestCase("/foo/0", "\"bar\"")] |
| 47 | + [TestCase("/", "0")] |
| 48 | + [TestCase("/a~1b", "1")] |
| 49 | + [TestCase("/c%25d", "2")] |
| 50 | + [TestCase("/e%5Ef", "3")] |
| 51 | + [TestCase("/g%7Ch", "4")] |
| 52 | + [TestCase("/i%5Cj", "5")] |
| 53 | + [TestCase("/k%22l", "6")] |
| 54 | + [TestCase("/%20", "7")] |
| 55 | + [TestCase("/m~0n", "8")] |
| 56 | + public void Find_ResolvesTheRfcUriFragmentExamples(string fragment, string expected) |
| 57 | + { |
| 58 | + var json = JsonNode.Parse(RfcDocument); |
| 59 | + |
| 60 | + JsonPointer.FromUriFragment(fragment).Find(json).ToJsonString().Should().Be(expected); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void EmptyPointer_ReferencesTheWholeDocument() |
| 65 | + { |
| 66 | + var json = JsonNode.Parse(RfcDocument); |
| 67 | + |
| 68 | + new JsonPointer(string.Empty).Find(json).Should().BeSameAs(json); |
| 69 | + JsonPointer.FromUriFragment(string.Empty).Find(json).Should().BeSameAs(json); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public void StringRepresentation_IsNotPercentDecoded() |
| 74 | + { |
| 75 | + var json = JsonNode.Parse("""{ "a b": 1 }"""); |
| 76 | + |
| 77 | + new JsonPointer("/a%20b").Find(json).Should().BeNull(); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void Unescape_DecodesTilde1BeforeTilde0() |
| 82 | + { |
| 83 | + new JsonPointer("/~01").Tokens.Should().Equal("~1"); |
| 84 | + } |
| 85 | + |
| 86 | + [TestCase("foo")] |
| 87 | + [TestCase("#/foo")] |
| 88 | + [TestCase("/foo~")] |
| 89 | + [TestCase("/foo~2")] |
| 90 | + [TestCase("/~a")] |
| 91 | + public void Constructor_Throws_WhenThePointerIsMalformed(string pointer) |
| 92 | + { |
| 93 | + var act = () => new JsonPointer(pointer); |
| 94 | + |
| 95 | + act.Should().Throw<AsyncApiException>(); |
| 96 | + } |
| 97 | + |
| 98 | + [TestCase("foo")] |
| 99 | + [TestCase("/foo~2")] |
| 100 | + [TestCase("/foo%7E2")] |
| 101 | + public void FromUriFragment_Throws_WhenTheDecodedPointerIsMalformed(string fragment) |
| 102 | + { |
| 103 | + var act = () => JsonPointer.FromUriFragment(fragment); |
| 104 | + |
| 105 | + act.Should().Throw<AsyncApiException>(); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public void Constructor_Throws_WhenThePointerIsNull() |
| 110 | + { |
| 111 | + var act = () => new JsonPointer(null); |
| 112 | + |
| 113 | + act.Should().Throw<ArgumentNullException>(); |
| 114 | + } |
| 115 | + |
| 116 | + [TestCase("")] |
| 117 | + [TestCase("/")] |
| 118 | + [TestCase("/foo/0")] |
| 119 | + [TestCase("/a~1b/m~0n")] |
| 120 | + [TestCase("//")] |
| 121 | + public void ToString_RoundTripsTheStringRepresentation(string pointer) |
| 122 | + { |
| 123 | + new JsonPointer(pointer).ToString().Should().Be(pointer); |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public void ParentPointer_OfASingleTokenPointer_IsTheWholeDocument() |
| 128 | + { |
| 129 | + new JsonPointer("/foo").ParentPointer.ToString().Should().BeEmpty(); |
| 130 | + new JsonPointer(string.Empty).ParentPointer.Should().BeNull(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments