xref: /aosp_15_r20/external/starlark-go/starlarkstruct/struct_test.go (revision 4947cdc739c985f6d86941e22894f5cefe7c9e9a)
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
5package starlarkstruct_test
6
7import (
8	"fmt"
9	"path/filepath"
10	"testing"
11
12	"go.starlark.net/starlark"
13	"go.starlark.net/starlarkstruct"
14	"go.starlark.net/starlarktest"
15)
16
17func Test(t *testing.T) {
18	testdata := starlarktest.DataFile("starlarkstruct", ".")
19	thread := &starlark.Thread{Load: load}
20	starlarktest.SetReporter(thread, t)
21	filename := filepath.Join(testdata, "testdata/struct.star")
22	predeclared := starlark.StringDict{
23		"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
24		"gensym": starlark.NewBuiltin("gensym", gensym),
25	}
26	if _, err := starlark.ExecFile(thread, filename, nil, predeclared); err != nil {
27		if err, ok := err.(*starlark.EvalError); ok {
28			t.Fatal(err.Backtrace())
29		}
30		t.Fatal(err)
31	}
32}
33
34// load implements the 'load' operation as used in the evaluator tests.
35func load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
36	if module == "assert.star" {
37		return starlarktest.LoadAssertModule()
38	}
39	return nil, fmt.Errorf("load not implemented")
40}
41
42// gensym is a built-in function that generates a unique symbol.
43func gensym(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
44	var name string
45	if err := starlark.UnpackArgs("gensym", args, kwargs, "name", &name); err != nil {
46		return nil, err
47	}
48	return &symbol{name: name}, nil
49}
50
51// A symbol is a distinct value that acts as a constructor of "branded"
52// struct instances, like a class symbol in Python or a "provider" in Bazel.
53type symbol struct{ name string }
54
55var _ starlark.Callable = (*symbol)(nil)
56
57func (sym *symbol) Name() string          { return sym.name }
58func (sym *symbol) String() string        { return sym.name }
59func (sym *symbol) Type() string          { return "symbol" }
60func (sym *symbol) Freeze()               {} // immutable
61func (sym *symbol) Truth() starlark.Bool  { return starlark.True }
62func (sym *symbol) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", sym.Type()) }
63
64func (sym *symbol) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
65	if len(args) > 0 {
66		return nil, fmt.Errorf("%s: unexpected positional arguments", sym)
67	}
68	return starlarkstruct.FromKeywords(sym, kwargs), nil
69}
70