MCPcopy Index your code
hub / github.com/go-openapi/jsonpointer / TestEscaping

Function TestEscaping

pointer_test.go:18–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestEscaping(t *testing.T) {
19 t.Parallel()
20
21 t.Run("escaped pointer strings against test document", func(t *testing.T) {
22 ins := []string{`/`, `/`, `/a~1b`, `/a~1b`, `/c%d`, `/e^f`, `/g|h`, `/i\j`, `/k"l`, `/ `, `/m~0n`}
23 outs := []float64{0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8}
24
25 for i := range ins {
26 t.Run("should create a JSONPointer", func(t *testing.T) {
27 p, err := New(ins[i])
28 require.NoError(t, err, "input: %v", ins[i])
29
30 t.Run("should get JSONPointer from document", func(t *testing.T) {
31 result, _, err := p.Get(testDocumentJSON(t))
32 require.NoError(t, err, "input: %v", ins[i])
33 assert.InDeltaf(t, outs[i], result, 1e-6, "input: %v", ins[i])
34 })
35 })
36 }
37 })
38
39 t.Run("special escapes", func(t *testing.T) {
40 t.Parallel()
41
42 t.Run("with escape then unescape", func(t *testing.T) {
43 const original = "a/"
44
45 t.Run("unescaping an escaped string should yield the original", func(t *testing.T) {
46 esc := Escape(original)
47 assert.EqualT(t, "a~1", esc)
48
49 unesc := Unescape(esc)
50 assert.EqualT(t, original, unesc)
51 })
52 })
53
54 t.Run("with multiple escapes", func(t *testing.T) {
55 unesc := Unescape("~01")
56 assert.EqualT(t, "~1", unesc)
57 assert.EqualT(t, "~01", Escape(unesc))
58
59 const (
60 original = "~/"
61 escaped = "~0~1"
62 )
63
64 assert.EqualT(t, escaped, Escape(original))
65 assert.EqualT(t, original, Unescape(escaped))
66 })
67
68 t.Run("with escaped characters in pointer", func(t *testing.T) {
69 t.Run("escaped ~", func(t *testing.T) {
70 s := Escape("m~n")
71 assert.EqualT(t, "m~0n", s)
72 })
73 t.Run("escaped /", func(t *testing.T) {
74 s := Escape("m/n")
75 assert.EqualT(t, "m~1n", s)

Callers

nothing calls this directly

Calls 5

NewFunction · 0.85
testDocumentJSONFunction · 0.85
EscapeFunction · 0.85
UnescapeFunction · 0.85
GetMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…