Debian Codegraph @

starlarkstruct/struct_test.go

raw
1 // Copyright 2018 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 starlarkstruct_test
6
7 import (
8 "fmt"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "go.starlark.net/starlark"
14 "go.starlark.net/starlarkstruct"
15 "go.starlark.net/starlarktest"
16 )
17
18 func Test(t *testing.T) {
19 testdata := starlarktest.DataFile("starlarkstruct", ".")
20 thread := &starlark.Thread{Load: load}
21 starlarktest.SetReporter(thread, t)
22 filename := filepath.Join(testdata, "testdata/struct.star")
23 predeclared := starlark.StringDict{
24 "struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
25 "gensym": starlark.NewBuiltin("gensym", gensym),
26 }
27 if _, err := starlark.ExecFile(thread, filename, nil, predeclared); err != nil {
28 if err, ok := err.(*starlark.EvalError); ok {
29 t.Fatal(err.Backtrace())
30 }
31 t.Fatal(err)
32 }
33 }
34
35 // load implements the 'load' operation as used in the evaluator tests.
36 func load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
37 if module == "assert.star" {
38 return starlarktest.LoadAssertModule()
39 }
40 return nil, fmt.Errorf("load not implemented")
41 }
42
43 // gensym is a built-in function that generates a unique symbol.
44 func gensym(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
45 var name string
46 if err := starlark.UnpackArgs("gensym", args, kwargs, "name", &name); err != nil {
47 return nil, err
48 }
49 return &symbol{name: name}, nil
50 }
51
52 // A symbol is a distinct value that acts as a constructor of "branded"
53 // struct instances, like a class symbol in Python or a "provider" in Bazel.
54 type symbol struct{ name string }
55
56 var _ starlark.Callable = (*symbol)(nil)
57
58 func (sym *symbol) Name() string { return sym.name }
59 func (sym *symbol) String() string { return sym.name }
60 func (sym *symbol) Type() string { return "symbol" }
61 func (sym *symbol) Freeze() {} // immutable
62 func (sym *symbol) Truth() starlark.Bool { return starlark.True }
63 func (sym *symbol) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", sym.Type()) }
64
65 func (sym *symbol) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
66 if len(args) > 0 {
67 return nil, fmt.Errorf("%s: unexpected positional arguments", sym)
68 }
69 return starlarkstruct.FromKeywords(sym, kwargs), nil
70 }
71
72 func TestAttrAt(t *testing.T) {
73 s := starlarkstruct.FromStringDict(starlarkstruct.Default, starlark.StringDict{
74 "b": starlark.MakeInt(2),
75 "a": starlark.MakeInt(1),
76 "c": starlark.MakeInt(3),
77 })
78
79 // Positions are those of AttrNames: sorted by field name.
80 names := s.AttrNames()
81 if got, want := strings.Join(names, ","), "a,b,c"; got != want {
82 t.Fatalf("AttrNames(): want %s, got %s", want, got)
83 }
84 if s.Len() != len(names) {
85 t.Fatalf("Len(): want %d, got %d", len(names), s.Len())
86 }
87 for pos, name := range names {
88 gotName, gotValue := s.AttrAt(pos)
89 if gotName != name {
90 t.Fatalf("AttrAt(%d): want %s, got %s", pos, name, gotName)
91 }
92 want, err := s.Attr(name)
93 if err != nil {
94 t.Fatalf("Attr(%s) failed: %v", name, err)
95 }
96 if eq, err := starlark.Equal(gotValue, want); err != nil {
97 t.Errorf("comparing AttrAt(%d) and Attr(%s): %v", pos, name, err)
98 } else if !eq {
99 t.Errorf("AttrAt(%d): want %s (= Attr(%s)), got %s", pos, want, name, gotValue)
100 }
101 }
102 }
103
104 func TestEntries(t *testing.T) {
105 s := starlarkstruct.FromStringDict(starlarkstruct.Default, starlark.StringDict{
106 "b": starlark.MakeInt(2),
107 "a": starlark.MakeInt(1),
108 "c": starlark.MakeInt(3),
109 })
110 t.Run("sequence matches Attr and AttrNames", func(t *testing.T) {
111 var names []string
112 for name, value := range s.Entries() {
113 names = append(names, name)
114 want, err := s.Attr(name)
115 if err != nil {
116 t.Fatalf("Attr(%s) failed: %v", name, err)
117 }
118 if eq, err := starlark.Equal(value, want); err != nil {
119 t.Errorf("comparing Entries value of %s and Attr(%s): %v", name, name, err)
120 } else if !eq {
121 t.Errorf("Entries: %s: want %s (= Attr(%s)), got %s", name, want, name, value)
122 }
123 }
124 if got, want := strings.Join(names, ","), strings.Join(s.AttrNames(), ","); got != want {
125 t.Errorf("Entries names: want %s, got %s", want, got)
126 }
127 })
128 t.Run("reusable iterator", func(t *testing.T) {
129 seq := s.Entries()
130 for range 2 {
131 var n int
132 for range seq {
133 n++
134 }
135 if n != s.Len() {
136 t.Errorf("Entries: want %d entries, got %d", s.Len(), n)
137 }
138 }
139 })
140 t.Run("an empty struct yields nothing", func(t *testing.T) {
141 empty := starlarkstruct.FromStringDict(starlarkstruct.Default, nil)
142 for name, value := range empty.Entries() {
143 t.Errorf("Entries of empty struct yielded %s = %s", name, value)
144 }
145 })
146 }