@@ -2,6 +2,8 @@ package function
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "reflect"
5
7
"testing"
6
8
7
9
"gopkg.in/bblfsh/sdk.v2/uast"
@@ -209,17 +211,6 @@ func TestUASTExtract(t *testing.T) {
209
211
"Identifier" , "Expression" , "Call" , "Callee" ,
210
212
},
211
213
},
212
- {
213
- name : "key_" + uast .KeyPos ,
214
- key : uast .KeyPos ,
215
- expected : []interface {}{
216
- "Start: [Offset:28 Line:4 Col:5], End: [Offset:31 Line:4 Col:8]" ,
217
- "Start: [Offset:47 Line:5 Col:9], End: [Offset:48 Line:5 Col:10]" ,
218
- "Start: [Offset:51 Line:5 Col:13], End: [Offset:52 Line:5 Col:14]" ,
219
- "Start: [Offset:60 Line:7 Col:7], End: [Offset:63 Line:7 Col:10]" ,
220
- "Start: [Offset:54 Line:7 Col:1], End: [Offset:59 Line:7 Col:6]" ,
221
- },
222
- },
223
214
{
224
215
name : "key_ctx" ,
225
216
key : "ctx" ,
@@ -250,6 +241,41 @@ func TestUASTExtract(t *testing.T) {
250
241
require .ElementsMatch (t , test .expected , foo )
251
242
})
252
243
}
244
+ expectedPos := []string {
245
+ `{"end":{"offset":31,"line":4,"col":8},"start":{"offset":28,"line":4,"col":5}}` ,
246
+ `{"end":{"offset":48,"line":5,"col":10},"start":{"offset":47,"line":5,"col":9}}` ,
247
+ `{"end":{"offset":52,"line":5,"col":14},"start":{"offset":51,"line":5,"col":13}}` ,
248
+ `{"end":{"offset":63,"line":7,"col":10},"start":{"offset":60,"line":7,"col":7}}` ,
249
+ `{"end":{"offset":59,"line":7,"col":6},"start":{"offset":54,"line":7,"col":1}}` ,
250
+ }
251
+ t .Run ("key_" + uast .KeyRoles , func (t * testing.T ) {
252
+ row := sql .NewRow (filteredNodes ["annotated" ], uast .KeyPos )
253
+
254
+ fn := NewUASTExtract (
255
+ expression .NewGetField (0 , sql .Blob , "" , false ),
256
+ expression .NewLiteral (uast .KeyPos , sql .Text ),
257
+ )
258
+
259
+ pos , err := fn .Eval (ctx , row )
260
+ require .NoError (t , err )
261
+
262
+ arr , ok := pos .([]interface {})
263
+ require .True (t , ok )
264
+ for i , jsonstr := range expectedPos {
265
+ var exp , act uast.Positions
266
+
267
+ err = json .Unmarshal ([]byte (jsonstr ), & exp )
268
+ require .NoError (t , err )
269
+
270
+ str , ok := arr [i ].(string )
271
+ require .True (t , ok )
272
+ err = json .Unmarshal ([]byte (str ), & act )
273
+ require .NoError (t , err )
274
+
275
+ require .True (t , reflect .DeepEqual (exp , act ))
276
+ }
277
+ })
278
+
253
279
}
254
280
255
281
func TestUASTChildren (t * testing.T ) {
0 commit comments