Skip to content

Commit 8a298b0

Browse files
committed
gitbase, function: test new uast modes
Signed-off-by: Javi Fontan <[email protected]>
1 parent 135211f commit 8a298b0

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

internal/function/uast_test.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ print(sum(3, 5))
2626
`
2727

2828
const testXPath = "//*[@roleIdentifier]"
29+
const testXPathSemantic = "//Identifier"
30+
const testXPathNative = "//."
2931

3032
func TestUAST(t *testing.T) {
3133
ctx, cleanup := setup(t)
@@ -46,7 +48,15 @@ func TestUAST(t *testing.T) {
4648
XPath: expression.NewGetField(2, sql.Text, "", false),
4749
}
4850

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+
4958
uast, filteredNodes := bblfshFixtures(t, ctx)
59+
_, mb := bblfshFixturesWithMode(t, ctx)
5060

5161
testCases := []struct {
5262
name string
@@ -61,6 +71,11 @@ func TestUAST(t *testing.T) {
6171
{"blob with unsupported lang", fn2, sql.NewRow([]byte(testCode), "YAML"), nil},
6272
{"blob with lang", fn2, sql.NewRow([]byte(testCode), "Python"), uast},
6373
{"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"]},
6479
}
6580

6681
for _, tt := range testCases {
@@ -98,6 +113,7 @@ func TestUASTXPath(t *testing.T) {
98113
{"left is nil", sql.NewRow(nil, "foo"), nil, nil},
99114
{"right is nil", sql.NewRow(uast, nil), nil, nil},
100115
{"both given", sql.NewRow(uast, testXPath), filteredNodes, nil},
116+
// {"blob is invalid", fn4, sql.NewRow([]byte(testCode), "Python", testXPath, "invalid"), nil},
101117
}
102118

103119
for _, tt := range testCases {
@@ -156,7 +172,7 @@ func bblfshFixtures(t *testing.T, ctx *sql.Context) (uast []interface{}, filtere
156172
client, err := ctx.Session.(*gitbase.Session).BblfshClient()
157173
require.NoError(t, err)
158174

159-
resp, err := client.Parse(context.Background(), bblfsh.Semantic, "python", []byte(testCode))
175+
resp, err := client.Parse(context.Background(), "python", []byte(testCode))
160176
require.NoError(t, err)
161177
require.Equal(t, protocol.Ok, resp.Status, "errors: %v", resp.Errors)
162178
testUAST, err := resp.UAST.Marshal()
@@ -175,6 +191,58 @@ func bblfshFixtures(t *testing.T, ctx *sql.Context) (uast []interface{}, filtere
175191
return []interface{}{testUAST}, identBlobs
176192
}
177193

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+
178246
func setup(t *testing.T) (*sql.Context, func()) {
179247
t.Helper()
180248
require.NoError(t, fixtures.Init())

0 commit comments

Comments
 (0)