github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/transform/staticlark/functions_test.go (about) 1 package staticlark 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "go.starlark.net/syntax" 9 ) 10 11 func TestCollectFunctions(t *testing.T) { 12 filename := "testdata/some_funcs.star" 13 14 f, err := syntax.Parse(filename, nil, 0) 15 if err != nil { 16 t.Error(err) 17 } 18 funcs, _, err := collectFuncDefsTopLevelCalls(f.Stmts) 19 if err != nil { 20 t.Error(err) 21 } 22 23 text := "" 24 for i, f := range funcs { 25 text = fmt.Sprintf("%s%d: %s %v\n", text, i, f.name, f.callNames) 26 } 27 28 expect := `0: use_branch [print] 29 1: branch_multiple [print print] 30 2: branch_no_else [print print] 31 3: branch_nested [print print] 32 4: top_level_func [use_branch len branch_multiple branch_no_else another_function] 33 5: another_function [branch_nested branch_no_else] 34 6: branch_elses [print print print print print] 35 7: branch_elses_contained [print print print print print print] 36 ` 37 if diff := cmp.Diff(expect, text); diff != "" { 38 t.Errorf("mismatch (-want +got):\n%s", diff) 39 } 40 } 41 42 func TestCollectFunctionsAllSyntax(t *testing.T) { 43 filename := "testdata/all_syntax.star" 44 45 f, err := syntax.Parse(filename, nil, 0) 46 if err != nil { 47 t.Error(err) 48 } 49 funcs, _, err := collectFuncDefsTopLevelCalls(f.Stmts) 50 if err != nil { 51 t.Error(err) 52 } 53 54 text := "" 55 for i, f := range funcs { 56 text = fmt.Sprintf("%s%d: %s %v\n", text, i, f.name, f.callNames) 57 } 58 59 // TODO(dustmop): fact_iter is the name of 2 different inner functions 60 // Should also appear in this list 61 expect := `0: some_vals [add_one] 62 1: add_one [] 63 2: mult [] 64 3: half [] 65 4: calc [fact_iter] 66 5: yet_more [math.floor fact_iter] 67 6: do_math [some_vals calc yet_more print] 68 ` 69 if diff := cmp.Diff(expect, text); diff != "" { 70 t.Errorf("mismatch (-want +got):\n%s", diff) 71 } 72 }