1
2
3
4
5 package starlark_test
6
7
8
9 import (
10 "fmt"
11 "testing"
12
13 "github.com/google/go-cmp/cmp"
14 "go.starlark.net/starlark"
15 )
16
17 func TestStringMethod(t *testing.T) {
18 s := starlark.String("hello")
19 for i, test := range [][2]string{
20
21 {s.String(), `"hello"`},
22 {fmt.Sprintf("%s", s), `"hello"`},
23 {fmt.Sprintf("%+s", s), `"hello"`},
24 {fmt.Sprintf("%v", s), `"hello"`},
25 {fmt.Sprintf("%+v", s), `"hello"`},
26
27 {s.GoString(), `hello`},
28 {fmt.Sprintf("%#v", s), `hello`},
29 } {
30 got, want := test[0], test[1]
31 if got != want {
32 t.Errorf("#%d: got <<%s>>, want <<%s>>", i, got, want)
33 }
34 }
35 }
36
37 func TestListAppend(t *testing.T) {
38 l := starlark.NewList(nil)
39 l.Append(starlark.String("hello"))
40 res, ok := starlark.AsString(l.Index(0))
41 if !ok {
42 t.Errorf("failed list.Append() got: %s, want: starlark.String", l.Index(0).Type())
43 }
44 if res != "hello" {
45 t.Errorf("failed list.Append() got: %+v, want: hello", res)
46 }
47 }
48
49 func TestParamDefault(t *testing.T) {
50 tests := []struct {
51 desc string
52 starFn string
53 wantDefaults []starlark.Value
54 }{
55 {
56 desc: "function with all required params",
57 starFn: "all_required",
58 wantDefaults: []starlark.Value{nil, nil, nil},
59 },
60 {
61 desc: "function with all optional params",
62 starFn: "all_opt",
63 wantDefaults: []starlark.Value{
64 starlark.String("a"),
65 starlark.None,
66 starlark.String(""),
67 },
68 },
69 {
70 desc: "function with required and optional params",
71 starFn: "mix_required_opt",
72 wantDefaults: []starlark.Value{
73 nil,
74 nil,
75 starlark.String("c"),
76 starlark.String("d"),
77 },
78 },
79 {
80 desc: "function with required, optional, and varargs params",
81 starFn: "with_varargs",
82 wantDefaults: []starlark.Value{
83 nil,
84 starlark.String("b"),
85 nil,
86 },
87 },
88 {
89 desc: "function with required, optional, varargs, and keyword-only params",
90 starFn: "with_varargs_kwonly",
91 wantDefaults: []starlark.Value{
92 nil,
93 starlark.String("b"),
94 starlark.String("c"),
95 nil,
96 nil,
97 },
98 },
99 {
100 desc: "function with required, optional, and keyword-only params",
101 starFn: "with_kwonly",
102 wantDefaults: []starlark.Value{
103 nil,
104 starlark.String("b"),
105 starlark.String("c"),
106 nil,
107 },
108 },
109 {
110 desc: "function with required, optional, and kwargs params",
111 starFn: "with_kwargs",
112 wantDefaults: []starlark.Value{
113 nil,
114 starlark.String("b"),
115 starlark.String("c"),
116 nil,
117 },
118 },
119 {
120 desc: "function with required, optional, varargs, kw-only, and kwargs params",
121 starFn: "with_varargs_kwonly_kwargs",
122 wantDefaults: []starlark.Value{
123 nil,
124 starlark.String("b"),
125 starlark.String("c"),
126 nil,
127 starlark.String("e"),
128 nil,
129 nil,
130 },
131 },
132 }
133
134 for _, tt := range tests {
135 t.Run(tt.desc, func(t *testing.T) {
136 thread := &starlark.Thread{}
137 filename := "testdata/function_param.star"
138 globals, err := starlark.ExecFile(thread, filename, nil, nil)
139 if err != nil {
140 t.Fatalf("ExecFile(%v, %q) failed: %v", thread, filename, err)
141 }
142
143 fn, ok := globals[tt.starFn].(*starlark.Function)
144 if !ok {
145 t.Fatalf("value %v is not a Starlark function", globals[tt.starFn])
146 }
147
148 var paramDefaults []starlark.Value
149 for i := 0; i < fn.NumParams(); i++ {
150 paramDefaults = append(paramDefaults, fn.ParamDefault(i))
151 }
152 if diff := cmp.Diff(tt.wantDefaults, paramDefaults); diff != "" {
153 t.Errorf("param defaults got diff (-want +got):\n%s", diff)
154 }
155 })
156 }
157 }
158
View as plain text