github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/jsonhelpers/json_helpers_test.go (about) 1 package jsonhelpers 2 3 import ( 4 "fmt" 5 "log" 6 "sort" 7 "testing" 8 9 jsonw "github.com/keybase/go-jsonw" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func makeJSONDangerous(json string) *jsonw.Wrapper { 14 w, err := jsonw.Unmarshal([]byte(json)) 15 if err != nil { 16 log.Panic(err) 17 } 18 return w 19 } 20 21 type jsonStringSimpleTest struct { 22 shouldWork bool 23 json *jsonw.Wrapper 24 out string 25 } 26 27 var jsonStringSimpleTests = []jsonStringSimpleTest{ 28 {true, makeJSONDangerous("1"), "1"}, 29 {true, makeJSONDangerous(`"hey"`), "hey"}, 30 {true, makeJSONDangerous("true"), "true"}, 31 {true, makeJSONDangerous("false"), "false"}, 32 {false, makeJSONDangerous("null"), ""}, 33 {false, makeJSONDangerous(`{"a": "b", "1": 2}`), ""}, 34 {false, makeJSONDangerous(`[1, {"a": "b"}, "three"]`), ""}, 35 } 36 37 func TestJSONStringSimple(t *testing.T) { 38 for _, test := range jsonStringSimpleTests { 39 out, err := JSONStringSimple(test.json) 40 if test.shouldWork { 41 require.NoError(t, err) 42 require.Equal(t, test.out, out) 43 } else { 44 require.Error(t, err) 45 } 46 } 47 } 48 49 func TestPyindex(t *testing.T) { 50 tests := []struct { 51 index int 52 len int 53 x int 54 ok bool 55 }{ 56 {0, 0, 0, false}, 57 {0, -1, 0, false}, 58 {0, 4, 0, true}, 59 {3, 4, 3, true}, 60 {4, 4, 0, false}, 61 {5, 4, 0, false}, 62 {-1, 4, 3, true}, 63 {-2, 4, 2, true}, 64 {-4, 4, 0, true}, 65 {-5, 4, 0, false}, 66 } 67 68 for _, test := range tests { 69 x, ok := pyindex(test.index, test.len) 70 if test.ok { 71 require.True(t, ok) 72 require.Equal(t, test.x, x) 73 } else { 74 require.False(t, ok) 75 } 76 } 77 } 78 79 type jsonUnpackArrayTest struct { 80 json *jsonw.Wrapper 81 shouldWork bool 82 out []*jsonw.Wrapper 83 } 84 85 var jsonUnpackArrayTests = []jsonUnpackArrayTest{ 86 {makeJSONDangerous("1"), false, nil}, 87 {makeJSONDangerous(`"hey"`), false, nil}, 88 {makeJSONDangerous(`{"a": "b"}`), false, nil}, 89 {makeJSONDangerous(`[1, {"a": "b"}, "three"]`), true, []*jsonw.Wrapper{ 90 makeJSONDangerous(`1`), makeJSONDangerous(`{"a": "b"}`), makeJSONDangerous(`"three"`), 91 }}, 92 } 93 94 func TestJSONUnpackArray(t *testing.T) { 95 for _, test := range jsonUnpackArrayTests { 96 arr, err := jsonUnpackArray(test.json) 97 if test.shouldWork { 98 require.NoError(t, err) 99 } else { 100 require.Error(t, err) 101 } 102 require.Equal(t, len(test.out), len(arr)) 103 104 for j, x := range arr { 105 y := test.out[j] 106 a, err := x.Marshal() 107 require.NoError(t, err) 108 109 b, err := y.Marshal() 110 require.NoError(t, err) 111 require.Equal(t, a, b) 112 } 113 } 114 } 115 116 type jsonGetChildrenTest struct { 117 json *jsonw.Wrapper 118 shouldWork bool 119 out []*jsonw.Wrapper 120 } 121 122 var jsonGetChildrenTests = []jsonGetChildrenTest{ 123 {makeJSONDangerous("1"), false, nil}, 124 {makeJSONDangerous(`"hey"`), false, nil}, 125 {makeJSONDangerous(`{"a": "b", "1": 2}`), true, []*jsonw.Wrapper{ 126 makeJSONDangerous(`"b"`), makeJSONDangerous(`2`), 127 }}, 128 {makeJSONDangerous(`[1, {"a": "b"}, "three"]`), true, []*jsonw.Wrapper{ 129 makeJSONDangerous(`1`), makeJSONDangerous(`{"a": "b"}`), makeJSONDangerous(`"three"`), 130 }}, 131 } 132 133 func compareJSONStringLists(xs, ys []*jsonw.Wrapper) error { 134 a1, err := getJSONStringList(xs) 135 if err != nil { 136 return err 137 } 138 a2, err := getJSONStringList(ys) 139 if err != nil { 140 return err 141 } 142 sort.Strings(a1) 143 sort.Strings(a2) 144 if len(a1) != len(a2) { 145 return fmt.Errorf("lists differ in length %v %v", len(a1), len(a2)) 146 } 147 for i, s1 := range a1 { 148 s2 := a2[i] 149 if s1 != s2 { 150 return fmt.Errorf("element [%v] differ\n%v\n%v", i, s1, s2) 151 } 152 } 153 return nil 154 } 155 156 func getJSONStringList(xs []*jsonw.Wrapper) ([]string, error) { 157 var ret []string 158 for _, x := range xs { 159 b, err := x.Marshal() 160 if err != nil { 161 return nil, err 162 } 163 ret = append(ret, string(b)) 164 } 165 return ret, nil 166 } 167 168 func TestJSONGetChildren(t *testing.T) { 169 for _, test := range jsonGetChildrenTests { 170 arr, err := JSONGetChildren(test.json) 171 if test.shouldWork { 172 require.NoError(t, err) 173 } else { 174 require.Error(t, err) 175 } 176 require.Equal(t, len(test.out), len(arr)) 177 178 err = compareJSONStringLists(arr, test.out) 179 require.NoError(t, err) 180 } 181 }