@@ -26,6 +26,8 @@ print(sum(3, 5))
26
26
`
27
27
28
28
const testXPath = "//*[@roleIdentifier]"
29
+ const testXPathSemantic = "//Identifier"
30
+ const testXPathNative = "//."
29
31
30
32
func TestUAST (t * testing.T ) {
31
33
ctx , cleanup := setup (t )
@@ -46,7 +48,15 @@ func TestUAST(t *testing.T) {
46
48
XPath : expression .NewGetField (2 , sql .Text , "" , false ),
47
49
}
48
50
51
+ fn4 := & UAST {
52
+ Blob : expression .NewGetField (0 , sql .Blob , "" , false ),
53
+ Lang : expression .NewGetField (1 , sql .Text , "" , false ),
54
+ XPath : expression .NewGetField (2 , sql .Text , "" , false ),
55
+ Mode : expression .NewGetField (3 , sql .Text , "" , false ),
56
+ }
57
+
49
58
uast , filteredNodes := bblfshFixtures (t , ctx )
59
+ _ , mb := bblfshFixturesWithMode (t , ctx )
50
60
51
61
testCases := []struct {
52
62
name string
@@ -61,6 +71,11 @@ func TestUAST(t *testing.T) {
61
71
{"blob with unsupported lang" , fn2 , sql .NewRow ([]byte (testCode ), "YAML" ), nil },
62
72
{"blob with lang" , fn2 , sql .NewRow ([]byte (testCode ), "Python" ), uast },
63
73
{"blob with lang and xpath" , fn3 , sql .NewRow ([]byte (testCode ), "Python" , testXPath ), filteredNodes },
74
+
75
+ {"mode is nil" , fn4 , sql .NewRow ([]byte {}, "Ruby" , nil , nil ), nil },
76
+ {"uast is annotated" , fn4 , sql .NewRow ([]byte (testCode ), "Python" , testXPath , "annotated" ), mb ["annotated" ]},
77
+ {"uast is semantic" , fn4 , sql .NewRow ([]byte (testCode ), "Python" , testXPathSemantic , "semantic" ), mb ["semantic" ]},
78
+ {"uast is native" , fn4 , sql .NewRow ([]byte (testCode ), "Python" , testXPathNative , "native" ), mb ["native" ]},
64
79
}
65
80
66
81
for _ , tt := range testCases {
@@ -98,6 +113,7 @@ func TestUASTXPath(t *testing.T) {
98
113
{"left is nil" , sql .NewRow (nil , "foo" ), nil , nil },
99
114
{"right is nil" , sql .NewRow (uast , nil ), nil , nil },
100
115
{"both given" , sql .NewRow (uast , testXPath ), filteredNodes , nil },
116
+ // {"blob is invalid", fn4, sql.NewRow([]byte(testCode), "Python", testXPath, "invalid"), nil},
101
117
}
102
118
103
119
for _ , tt := range testCases {
@@ -156,7 +172,7 @@ func bblfshFixtures(t *testing.T, ctx *sql.Context) (uast []interface{}, filtere
156
172
client , err := ctx .Session .(* gitbase.Session ).BblfshClient ()
157
173
require .NoError (t , err )
158
174
159
- resp , err := client .Parse (context .Background (), bblfsh . Semantic , "python" , []byte (testCode ))
175
+ resp , err := client .Parse (context .Background (), "python" , []byte (testCode ))
160
176
require .NoError (t , err )
161
177
require .Equal (t , protocol .Ok , resp .Status , "errors: %v" , resp .Errors )
162
178
testUAST , err := resp .UAST .Marshal ()
@@ -175,6 +191,58 @@ func bblfshFixtures(t *testing.T, ctx *sql.Context) (uast []interface{}, filtere
175
191
return []interface {}{testUAST }, identBlobs
176
192
}
177
193
194
+ func bblfshFixturesWithMode (
195
+ t * testing.T ,
196
+ ctx * sql.Context ,
197
+ ) (map [string ][]interface {}, map [string ][]interface {}) {
198
+ t .Helper ()
199
+
200
+ uast := make (map [string ][]interface {})
201
+ filteredNodes := make (map [string ][]interface {})
202
+
203
+ modes := []struct {
204
+ n string
205
+ t bblfsh.Mode
206
+ x string
207
+ }{
208
+ {"annotated" , bblfsh .Annotated , testXPath },
209
+ {"semantic" , bblfsh .Semantic , testXPathSemantic },
210
+ {"native" , bblfsh .Native , testXPathNative },
211
+ }
212
+
213
+ client , err := ctx .Session .(* gitbase.Session ).BblfshClient ()
214
+ require .NoError (t , err )
215
+
216
+ for _ , mode := range modes {
217
+ resp , err := client .ParseWithMode (
218
+ context .Background (),
219
+ mode .t ,
220
+ "python" ,
221
+ []byte (testCode ),
222
+ )
223
+
224
+ require .NoError (t , err )
225
+ require .Equal (t , protocol .Ok , resp .Status , "errors: %v" , resp .Errors )
226
+ testUAST , err := resp .UAST .Marshal ()
227
+ require .NoError (t , err )
228
+
229
+ idents , err := tools .Filter (resp .UAST , mode .x )
230
+ require .NoError (t , err )
231
+
232
+ var identBlobs []interface {}
233
+ for _ , id := range idents {
234
+ i , err := id .Marshal ()
235
+ require .NoError (t , err )
236
+ identBlobs = append (identBlobs , i )
237
+ }
238
+
239
+ uast [mode .n ] = []interface {}{testUAST }
240
+ filteredNodes [mode .n ] = identBlobs
241
+ }
242
+
243
+ return uast , filteredNodes
244
+ }
245
+
178
246
func setup (t * testing.T ) (* sql.Context , func ()) {
179
247
t .Helper ()
180
248
require .NoError (t , fixtures .Init ())
0 commit comments