github.com/galdor/go-ejson@v0.0.0-20231201100034-d335379f26b0/pointer_test.go (about) 1 package ejson 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestNewPointer(t *testing.T) { 10 assert := assert.New(t) 11 12 assert.Equal("", NewPointer().String()) 13 assert.Equal("/a", NewPointer("a").String()) 14 assert.Equal("/a/42/b", NewPointer("a", 42, "b").String()) 15 assert.Equal("/a/b/c", 16 NewPointer(NewPointer("a"), NewPointer("b", "c")).String()) 17 } 18 19 func TestPointerParse(t *testing.T) { 20 assert := assert.New(t) 21 22 assertParse := func(ep Pointer, s string) { 23 t.Helper() 24 25 var p Pointer 26 if assert.NoError(p.Parse(s), s) { 27 assert.Equal(ep, p, s) 28 } 29 } 30 31 assertParse(Pointer{}, "") 32 assertParse(Pointer{"foo"}, "/foo") 33 assertParse(Pointer{"foo", "bar"}, "/foo/bar") 34 assertParse(Pointer{"a", "b", "c"}, "/a/b/c") 35 assertParse(Pointer{"xy", "", "z", "", ""}, "/xy//z//") 36 assertParse(Pointer{"foo/bar", "~hello"}, "/foo~1bar/~0hello") 37 assertParse(Pointer{"~1", "/0"}, "/~01/~10") 38 } 39 40 func TestPointerString(t *testing.T) { 41 assert := assert.New(t) 42 43 assert.Equal("", Pointer{}.String()) 44 assert.Equal("/foo", Pointer{"foo"}.String()) 45 assert.Equal("/foo/bar", Pointer{"foo", "bar"}.String()) 46 assert.Equal("/a/b/c", Pointer{"a", "b", "c"}.String()) 47 assert.Equal("/xy//z//", Pointer{"xy", "", "z", "", ""}.String()) 48 assert.Equal("/foo~1bar/~0hello", Pointer{"foo/bar", "~hello"}.String()) 49 assert.Equal("/~01/~10", Pointer{"~1", "/0"}.String()) 50 } 51 52 func TestPointerPrepend(t *testing.T) { 53 assert := assert.New(t) 54 55 p := NewPointer() 56 57 p.Prepend("a") 58 assert.Equal("/a", p.String()) 59 60 p.Prepend("b") 61 assert.Equal("/b/a", p.String()) 62 63 p.Prepend("c", "d", "e") 64 assert.Equal("/c/d/e/b/a", p.String()) 65 } 66 67 func TestPointerAppend(t *testing.T) { 68 assert := assert.New(t) 69 70 p := NewPointer() 71 72 p.Append("a") 73 assert.Equal("/a", p.String()) 74 75 p.Append("b") 76 assert.Equal("/a/b", p.String()) 77 78 p.Append("c", "d", "e") 79 assert.Equal("/a/b/c/d/e", p.String()) 80 } 81 82 func TestPointerParent(t *testing.T) { 83 assert := assert.New(t) 84 85 assert.Equal("", Pointer{"a"}.Parent().String()) 86 assert.Equal("/a", Pointer{"a", "b"}.Parent().String()) 87 assert.Equal("/a/b", Pointer{"a", "b", "c"}.Parent().String()) 88 } 89 90 func TestPointerChild(t *testing.T) { 91 assert := assert.New(t) 92 93 assert.Equal("/a", Pointer{}.Child("a").String()) 94 assert.Equal("/a/b/c", Pointer{"a"}.Child("b", "c").String()) 95 assert.Equal("/a/b/c", Pointer{"a", "b"}.Child("c").String()) 96 assert.Equal("/a/1", Pointer{"a"}.Child(1).String()) 97 assert.Equal("/a/b/c", Pointer{"a"}.Child(Pointer{"b", "c"}).String()) 98 } 99 100 func TestPointerFind(t *testing.T) { 101 assert := assert.New(t) 102 103 obj := map[string]interface{}{ 104 "a": 42, 105 "b": map[string]interface{}{ 106 "x": 1, 107 }, 108 "c": []interface{}{ 109 map[string]interface{}{ 110 "x": 2, 111 }, 112 map[string]interface{}{ 113 "x": 3, 114 }, 115 }, 116 } 117 118 assert.Equal(obj, 119 NewPointer().Find(obj)) 120 121 assert.Equal(nil, 122 NewPointer("foo").Find(obj)) 123 124 assert.Equal(42, 125 NewPointer("a").Find(obj)) 126 127 assert.Equal(map[string]interface{}{"x": 1}, 128 NewPointer("b").Find(obj)) 129 130 assert.Equal(nil, 131 NewPointer("c", "foo").Find(obj)) 132 133 assert.Equal(nil, 134 NewPointer("c", "-2").Find(obj)) 135 136 assert.Equal(nil, 137 NewPointer("c", "3").Find(obj)) 138 139 assert.Equal(map[string]interface{}{"x": 2}, 140 NewPointer("c", "0").Find(obj)) 141 142 assert.Equal(2, 143 NewPointer("c", "0", "x").Find(obj)) 144 145 assert.Equal(3, 146 NewPointer("c", "1", "x").Find(obj)) 147 148 assert.Equal(nil, 149 NewPointer("c", "1", "y").Find(obj)) 150 151 assert.Equal(nil, 152 NewPointer("c", "1", "x", "2").Find(obj)) 153 }