github.com/mithrandie/csvq@v1.18.1/lib/json/query_parser_test.go (about) 1 package json 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 var parseQueryTests = []struct { 9 Input string 10 Expect QueryExpression 11 Error string 12 }{ 13 { 14 Input: "", 15 Expect: nil, 16 }, 17 { 18 Input: "abc", 19 Expect: Element{ 20 Label: "abc", 21 }, 22 }, 23 { 24 Input: "`abc\\`def`", 25 Expect: Element{ 26 Label: "abc`def", 27 }, 28 }, 29 { 30 Input: "[0]", 31 Expect: ArrayItem{ 32 Index: 0, 33 }, 34 }, 35 { 36 Input: "[]", 37 Expect: RowValueExpr{}, 38 }, 39 { 40 Input: "{}", 41 Expect: TableExpr{}, 42 }, 43 { 44 Input: "abc.def", 45 Expect: Element{ 46 Label: "abc", 47 Child: Element{ 48 Label: "def", 49 }, 50 }, 51 }, 52 { 53 Input: "abc[1]", 54 Expect: Element{ 55 Label: "abc", 56 Child: ArrayItem{ 57 Index: 1, 58 }, 59 }, 60 }, 61 { 62 Input: "abc[]", 63 Expect: Element{ 64 Label: "abc", 65 Child: RowValueExpr{}, 66 }, 67 }, 68 { 69 Input: "abc{}", 70 Expect: Element{ 71 Label: "abc", 72 Child: TableExpr{}, 73 }, 74 }, 75 { 76 Input: "abc[2].def", 77 Expect: Element{ 78 Label: "abc", 79 Child: ArrayItem{ 80 Index: 2, 81 Child: Element{ 82 Label: "def", 83 }, 84 }, 85 }, 86 }, 87 { 88 Input: "abc[2][1]", 89 Expect: Element{ 90 Label: "abc", 91 Child: ArrayItem{ 92 Index: 2, 93 Child: ArrayItem{ 94 Index: 1, 95 }, 96 }, 97 }, 98 }, 99 { 100 Input: "abc[22][]", 101 Expect: Element{ 102 Label: "abc", 103 Child: ArrayItem{ 104 Index: 22, 105 Child: RowValueExpr{}, 106 }, 107 }, 108 }, 109 { 110 Input: "abc[2]{}", 111 Expect: Element{ 112 Label: "abc", 113 Child: ArrayItem{ 114 Index: 2, 115 Child: TableExpr{}, 116 }, 117 }, 118 }, 119 { 120 Input: "abc[].def", 121 Expect: Element{ 122 Label: "abc", 123 Child: RowValueExpr{ 124 Child: Element{ 125 Label: "def", 126 }, 127 }, 128 }, 129 }, 130 { 131 Input: "abc[][1]", 132 Expect: Element{ 133 Label: "abc", 134 Child: RowValueExpr{ 135 Child: ArrayItem{ 136 Index: 1, 137 }, 138 }, 139 }, 140 }, 141 { 142 Input: "{abc}", 143 Expect: TableExpr{ 144 Fields: []FieldExpr{ 145 { 146 Element: Element{Label: "abc"}, 147 }, 148 }, 149 }, 150 }, 151 { 152 Input: "{abc, def as alias}", 153 Expect: TableExpr{ 154 Fields: []FieldExpr{ 155 { 156 Element: Element{Label: "abc"}, 157 }, 158 { 159 Element: Element{Label: "def"}, 160 Alias: "alias", 161 }, 162 }, 163 }, 164 }, 165 { 166 Input: "{abc.def, ghi[2]}", 167 Expect: TableExpr{ 168 Fields: []FieldExpr{ 169 { 170 Element: Element{ 171 Label: "abc", 172 Child: Element{Label: "def"}, 173 }, 174 }, 175 { 176 Element: Element{ 177 Label: "ghi", 178 Child: ArrayItem{ 179 Index: 2, 180 }, 181 }, 182 }, 183 }, 184 }, 185 }, 186 { 187 Input: "abc.\"def\".ghi", 188 Expect: Element{ 189 Label: "abc", 190 Child: Element{ 191 Label: "def", 192 Child: Element{ 193 Label: "ghi", 194 }, 195 }, 196 }, 197 }, 198 { 199 Input: "abc def", 200 Error: "column 5: unexpected token \"def\"", 201 }, 202 { 203 Input: "abc[].def{}", 204 Error: "column 10: unexpected token \"{\"", 205 }, 206 { 207 Input: "abc[].def[]", 208 Error: "column 11: unexpected token \"]\"", 209 }, 210 { 211 Input: "abc{}.def", 212 Error: "column 6: unexpected token \".\"", 213 }, 214 { 215 Input: "abc{def[]}", 216 Error: "column 9: unexpected token \"]\"", 217 }, 218 { 219 Input: "abc{def{}}", 220 Error: "column 8: unexpected token \"{\"", 221 }, 222 { 223 Input: "abc{def{}}", 224 Error: "column 8: unexpected token \"{\"", 225 }, 226 { 227 Input: "`abc", 228 Error: "column 4: string not terminated", 229 }, 230 { 231 Input: "abc[", 232 Error: "column 4: unexpected termination", 233 }, 234 } 235 236 func TestParseQuery(t *testing.T) { 237 for _, v := range parseQueryTests { 238 result, err := ParseQuery(v.Input) 239 if err != nil { 240 if len(v.Error) < 1 { 241 t.Errorf("unexpected error %q for %q", err.Error(), v.Input) 242 } else if err.Error() != v.Error { 243 t.Errorf("error %q, want error %q for %q", err, v.Error, v.Input) 244 } 245 continue 246 } 247 if 0 < len(v.Error) { 248 t.Errorf("no error, want error %q for %q", v.Error, v.Input) 249 continue 250 } 251 if !reflect.DeepEqual(result, v.Expect) { 252 t.Errorf("result = %#v, want %#v for %q", result, v.Expect, v.Input) 253 } 254 } 255 }