xref: /aosp_15_r20/external/starlark-go/starlark/value_test.go (revision 4947cdc739c985f6d86941e22894f5cefe7c9e9a)
1*4947cdc7SCole Faust// Copyright 2017 The Bazel Authors. All rights reserved.
2*4947cdc7SCole Faust// Use of this source code is governed by a BSD-style
3*4947cdc7SCole Faust// license that can be found in the LICENSE file.
4*4947cdc7SCole Faust
5*4947cdc7SCole Faustpackage starlark_test
6*4947cdc7SCole Faust
7*4947cdc7SCole Faust// This file defines tests of the Value API.
8*4947cdc7SCole Faust
9*4947cdc7SCole Faustimport (
10*4947cdc7SCole Faust	"fmt"
11*4947cdc7SCole Faust	"testing"
12*4947cdc7SCole Faust
13*4947cdc7SCole Faust	"go.starlark.net/starlark"
14*4947cdc7SCole Faust)
15*4947cdc7SCole Faust
16*4947cdc7SCole Faustfunc TestStringMethod(t *testing.T) {
17*4947cdc7SCole Faust	s := starlark.String("hello")
18*4947cdc7SCole Faust	for i, test := range [][2]string{
19*4947cdc7SCole Faust		// quoted string:
20*4947cdc7SCole Faust		{s.String(), `"hello"`},
21*4947cdc7SCole Faust		{fmt.Sprintf("%s", s), `"hello"`},
22*4947cdc7SCole Faust		{fmt.Sprintf("%+s", s), `"hello"`},
23*4947cdc7SCole Faust		{fmt.Sprintf("%v", s), `"hello"`},
24*4947cdc7SCole Faust		{fmt.Sprintf("%+v", s), `"hello"`},
25*4947cdc7SCole Faust		// unquoted:
26*4947cdc7SCole Faust		{s.GoString(), `hello`},
27*4947cdc7SCole Faust		{fmt.Sprintf("%#v", s), `hello`},
28*4947cdc7SCole Faust	} {
29*4947cdc7SCole Faust		got, want := test[0], test[1]
30*4947cdc7SCole Faust		if got != want {
31*4947cdc7SCole Faust			t.Errorf("#%d: got <<%s>>, want <<%s>>", i, got, want)
32*4947cdc7SCole Faust		}
33*4947cdc7SCole Faust	}
34*4947cdc7SCole Faust}
35*4947cdc7SCole Faust
36*4947cdc7SCole Faustfunc TestListAppend(t *testing.T) {
37*4947cdc7SCole Faust	l := starlark.NewList(nil)
38*4947cdc7SCole Faust	l.Append(starlark.String("hello"))
39*4947cdc7SCole Faust	res, ok := starlark.AsString(l.Index(0))
40*4947cdc7SCole Faust	if !ok {
41*4947cdc7SCole Faust		t.Errorf("failed list.Append() got: %s, want: starlark.String", l.Index(0).Type())
42*4947cdc7SCole Faust	}
43*4947cdc7SCole Faust	if res != "hello" {
44*4947cdc7SCole Faust		t.Errorf("failed list.Append() got: %+v, want: hello", res)
45*4947cdc7SCole Faust	}
46*4947cdc7SCole Faust}
47