Debian Codegraph @

starlark/value_test.go

raw
1 // Copyright 2017 The Bazel Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package starlark_test
6
7 // This file defines tests of the Value API.
8
9 import (
10 "fmt"
11 "iter"
12 "testing"
13
14 "github.com/google/go-cmp/cmp"
15 "go.starlark.net/starlark"
16 )
17
18 func TestStringMethod(t *testing.T) {
19 s := starlark.String("hello")
20 for i, test := range [][2]string{
21 // quoted string:
22 {s.String(), `"hello"`},
23 {fmt.Sprintf("%s", s), `"hello"`},
24 {fmt.Sprintf("%+s", s), `"hello"`},
25 {fmt.Sprintf("%v", s), `"hello"`},
26 {fmt.Sprintf("%+v", s), `"hello"`},
27 // unquoted:
28 {s.GoString(), `hello`},
29 {fmt.Sprintf("%#v", s), `hello`},
30 } {
31 got, want := test[0], test[1]
32 if got != want {
33 t.Errorf("#%d: got <<%s>>, want <<%s>>", i, got, want)
34 }
35 }
36 }
37
38 func TestListAppend(t *testing.T) {
39 l := starlark.NewList(nil)
40 l.Append(starlark.String("hello"))
41 res, ok := starlark.AsString(l.Index(0))
42 if !ok {
43 t.Errorf("failed list.Append() got: %s, want: starlark.String", l.Index(0).Type())
44 }
45 if res != "hello" {
46 t.Errorf("failed list.Append() got: %+v, want: hello", res)
47 }
48 }
49
50 func TestParamDefault(t *testing.T) {
51 tests := []struct {
52 desc string
53 starFn string
54 wantDefaults []starlark.Value
55 }{
56 {
57 desc: "function with all required params",
58 starFn: "all_required",
59 wantDefaults: []starlark.Value{nil, nil, nil},
60 },
61 {
62 desc: "function with all optional params",
63 starFn: "all_opt",
64 wantDefaults: []starlark.Value{
65 starlark.String("a"),
66 starlark.None,
67 starlark.String(""),
68 },
69 },
70 {
71 desc: "function with required and optional params",
72 starFn: "mix_required_opt",
73 wantDefaults: []starlark.Value{
74 nil,
75 nil,
76 starlark.String("c"),
77 starlark.String("d"),
78 },
79 },
80 {
81 desc: "function with required, optional, and varargs params",
82 starFn: "with_varargs",
83 wantDefaults: []starlark.Value{
84 nil,
85 starlark.String("b"),
86 nil,
87 },
88 },
89 {
90 desc: "function with required, optional, varargs, and keyword-only params",
91 starFn: "with_varargs_kwonly",
92 wantDefaults: []starlark.Value{
93 nil,
94 starlark.String("b"),
95 starlark.String("c"),
96 nil,
97 nil,
98 },
99 },
100 {
101 desc: "function with required, optional, and keyword-only params",
102 starFn: "with_kwonly",
103 wantDefaults: []starlark.Value{
104 nil,
105 starlark.String("b"),
106 starlark.String("c"),
107 nil,
108 },
109 },
110 {
111 desc: "function with required, optional, and kwargs params",
112 starFn: "with_kwargs",
113 wantDefaults: []starlark.Value{
114 nil,
115 starlark.String("b"),
116 starlark.String("c"),
117 nil,
118 },
119 },
120 {
121 desc: "function with required, optional, varargs, kw-only, and kwargs params",
122 starFn: "with_varargs_kwonly_kwargs",
123 wantDefaults: []starlark.Value{
124 nil,
125 starlark.String("b"),
126 starlark.String("c"),
127 nil,
128 starlark.String("e"),
129 nil,
130 nil,
131 },
132 },
133 }
134
135 for _, tt := range tests {
136 t.Run(tt.desc, func(t *testing.T) {
137 thread := &starlark.Thread{}
138 filename := "testdata/function_param.star"
139 globals, err := starlark.ExecFile(thread, filename, nil, nil)
140 if err != nil {
141 t.Fatalf("ExecFile(%v, %q) failed: %v", thread, filename, err)
142 }
143
144 fn, ok := globals[tt.starFn].(*starlark.Function)
145 if !ok {
146 t.Fatalf("value %v is not a Starlark function", globals[tt.starFn])
147 }
148
149 var paramDefaults []starlark.Value
150 for i := 0; i < fn.NumParams(); i++ {
151 paramDefaults = append(paramDefaults, fn.ParamDefault(i))
152 }
153 if diff := cmp.Diff(tt.wantDefaults, paramDefaults); diff != "" {
154 t.Errorf("param defaults got diff (-want +got):\n%s", diff)
155 }
156 })
157 }
158 }
159
160 func TestElementsIteratorCount(t *testing.T) {
161 d := starlark.NewDict(1)
162 m := &fakeMapping{Dict: d}
163 assertNoElementsFastPath(t, m)
164
165 // Asking for the sequence must not start an iteration.
166 _ = starlark.Elements(m)
167 err := d.SetKey(starlark.String("one"), starlark.MakeInt(1))
168 if err != nil {
169 t.Error("Elements(m) started an iteration")
170 }
171 }
172
173 func TestEntriesIteratorCount(t *testing.T) {
174 d := starlark.NewDict(1)
175 m := &fakeMapping{Dict: d}
176 assertNoEntriesFastPath(t, m)
177
178 // Asking for the sequence must not start an iteration.
179 _ = starlark.Entries(m)
180 err := d.SetKey(starlark.String("one"), starlark.MakeInt(1))
181 if err != nil {
182 t.Error("Entries(m) started an iteration")
183 }
184 }
185
186 // A fakeMapping is a starlark.IterableMapping that deliberately does not
187 // implement the Elements or Entries fast paths, so that the standalone
188 // starlark.Elements and starlark.Entries functions must use the generic
189 // Iterate/Done code path.
190 type fakeMapping struct {
191 *starlark.Dict
192 }
193
194 var _ starlark.IterableMapping = (*fakeMapping)(nil)
195
196 func (m *fakeMapping) Elements() {}
197
198 func (m *fakeMapping) Entries() {}
199
200 func assertNoElementsFastPath(t *testing.T, v any) {
201 t.Helper()
202 if _, ok := v.(interface {
203 Elements() iter.Seq[starlark.Value]
204 }); ok {
205 t.Fatalf("%T has an Elements fast path, so this test would not exercise Elements' generic code path", v)
206 }
207 }
208
209 func assertNoEntriesFastPath(t *testing.T, v any) {
210 t.Helper()
211 if _, ok := v.(interface {
212 Entries() iter.Seq2[starlark.Value, starlark.Value]
213 }); ok {
214 t.Fatalf("%T has an Entries fast path, so this test would not exercise Entries' generic code path", v)
215 }
216 }