RFC6901 JSON Pointer implementation in OCaml using jsont
at main 3.7 kB view raw
1JSON Pointer Parsing Tests (RFC 6901) 2 3Root pointer (empty string): 4 $ ./test_pointer.exe parse "" 5 OK: [] 6 7RFC 6901 Section 5 examples: 8 $ ./test_pointer.exe parse "/foo" 9 OK: [Mem:foo] 10 $ ./test_pointer.exe parse "/foo/0" 11 OK: [Mem:foo, Nth:0] 12 $ ./test_pointer.exe parse "/" 13 OK: [Mem:] 14 $ ./test_pointer.exe parse "/a~1b" 15 OK: [Mem:a/b] 16 $ ./test_pointer.exe parse "/c%d" 17 OK: [Mem:c%d] 18 $ ./test_pointer.exe parse "/e^f" 19 OK: [Mem:e^f] 20 $ ./test_pointer.exe parse "/g|h" 21 OK: [Mem:g|h] 22 $ ./test_pointer.exe parse '/i\j' 23 OK: [Mem:i\j] 24 $ ./test_pointer.exe parse '/k"l' 25 OK: [Mem:k"l] 26 $ ./test_pointer.exe parse "/ " 27 OK: [Mem: ] 28 $ ./test_pointer.exe parse "/m~0n" 29 OK: [Mem:m~n] 30 31Array indices: 32 $ ./test_pointer.exe parse "/0" 33 OK: [Nth:0] 34 $ ./test_pointer.exe parse "/1" 35 OK: [Nth:1] 36 $ ./test_pointer.exe parse "/10" 37 OK: [Nth:10] 38 $ ./test_pointer.exe parse "/123" 39 OK: [Nth:123] 40 41End-of-array marker: 42 $ ./test_pointer.exe parse "/-" 43 OK: [, /-] 44 $ ./test_pointer.exe parse "/foo/-" 45 OK: [Mem:foo, /-] 46 47Multiple levels: 48 $ ./test_pointer.exe parse "/a/b/c" 49 OK: [Mem:a, Mem:b, Mem:c] 50 $ ./test_pointer.exe parse "/0/1/2" 51 OK: [Nth:0, Nth:1, Nth:2] 52 $ ./test_pointer.exe parse "/foo/0/bar/1" 53 OK: [Mem:foo, Nth:0, Mem:bar, Nth:1] 54 55Escape sequences: 56 $ ./test_pointer.exe parse "/~0" 57 OK: [Mem:~] 58 $ ./test_pointer.exe parse "/~1" 59 OK: [Mem:/] 60 $ ./test_pointer.exe parse "/~0~1" 61 OK: [Mem:~/] 62 $ ./test_pointer.exe parse "/~1~0" 63 OK: [Mem:/~] 64 $ ./test_pointer.exe parse "/~01" 65 OK: [Mem:~1] 66 67Empty member names: 68 $ ./test_pointer.exe parse "//" 69 OK: [Mem:, Mem:] 70 $ ./test_pointer.exe parse "///" 71 OK: [Mem:, Mem:, Mem:] 72 73Invalid: must start with / 74 $ ./test_pointer.exe parse "foo" 75 ERROR: Invalid JSON Pointer: must be empty or start with '/': foo 76 $ ./test_pointer.exe parse "a/b" 77 ERROR: Invalid JSON Pointer: must be empty or start with '/': a/b 78 79Invalid: incomplete escape 80 $ ./test_pointer.exe parse "/~" 81 ERROR: Invalid JSON Pointer: incomplete escape sequence at end 82 $ ./test_pointer.exe parse "/foo~" 83 ERROR: Invalid JSON Pointer: incomplete escape sequence at end 84 85Invalid: bad escape sequence 86 $ ./test_pointer.exe parse "/~2" 87 ERROR: Invalid JSON Pointer: invalid escape sequence ~2 88 $ ./test_pointer.exe parse "/~a" 89 ERROR: Invalid JSON Pointer: invalid escape sequence ~a 90 91Leading zeros - valid as tokens, become member names (invalid as array indices at eval time): 92 $ ./test_pointer.exe parse "/00" 93 OK: [Mem:00] 94 $ ./test_pointer.exe parse "/01" 95 OK: [Mem:01] 96 $ ./test_pointer.exe parse "/007" 97 OK: [Mem:007] 98 99RFC 6901 Section 4: ~01 decodes to ~1, not / (order matters): 100 $ ./test_pointer.exe parse "/~01" 101 OK: [Mem:~1] 102 $ ./test_pointer.exe parse "/~10" 103 OK: [Mem:/0] 104 105Unicode characters (RFC 6901 specifies JSON Pointer is Unicode): 106 $ ./test_pointer.exe parse "/café" 107 OK: [Mem:café] 108 $ ./test_pointer.exe parse "/日本語" 109 OK: [Mem:日本語] 110 $ ./test_pointer.exe parse "/emoji🎉" 111 OK: [Mem:emoji🎉] 112 113Numeric member names (should be parsed as indices when valid): 114 $ ./test_pointer.exe parse "/0" 115 OK: [Nth:0] 116 $ ./test_pointer.exe parse "/-1" 117 OK: [Mem:-1] 118 $ ./test_pointer.exe parse "/+1" 119 OK: [Mem:+1] 120 121Very deep paths: 122 $ ./test_pointer.exe parse "/a/b/c/d/e/f/g/h/i/j" 123 OK: [Mem:a, Mem:b, Mem:c, Mem:d, Mem:e, Mem:f, Mem:g, Mem:h, Mem:i, Mem:j] 124 125Complex escape sequences: 126 $ ./test_pointer.exe parse "/~0~0" 127 OK: [Mem:~~] 128 $ ./test_pointer.exe parse "/~1~1" 129 OK: [Mem://] 130 $ ./test_pointer.exe parse "/a~0b~1c~0d~1e" 131 OK: [Mem:a~b/c~d/e] 132 133Invalid: tilde at end of path: 134 $ ./test_pointer.exe parse "/foo/bar~" 135 ERROR: Invalid JSON Pointer: incomplete escape sequence at end