xref: /aosp_15_r20/external/starlark-go/starlark/value_test.go (revision 4947cdc739c985f6d86941e22894f5cefe7c9e9a)
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
5package starlark_test
6
7// This file defines tests of the Value API.
8
9import (
10	"fmt"
11	"testing"
12
13	"go.starlark.net/starlark"
14)
15
16func TestStringMethod(t *testing.T) {
17	s := starlark.String("hello")
18	for i, test := range [][2]string{
19		// quoted string:
20		{s.String(), `"hello"`},
21		{fmt.Sprintf("%s", s), `"hello"`},
22		{fmt.Sprintf("%+s", s), `"hello"`},
23		{fmt.Sprintf("%v", s), `"hello"`},
24		{fmt.Sprintf("%+v", s), `"hello"`},
25		// unquoted:
26		{s.GoString(), `hello`},
27		{fmt.Sprintf("%#v", s), `hello`},
28	} {
29		got, want := test[0], test[1]
30		if got != want {
31			t.Errorf("#%d: got <<%s>>, want <<%s>>", i, got, want)
32		}
33	}
34}
35
36func TestListAppend(t *testing.T) {
37	l := starlark.NewList(nil)
38	l.Append(starlark.String("hello"))
39	res, ok := starlark.AsString(l.Index(0))
40	if !ok {
41		t.Errorf("failed list.Append() got: %s, want: starlark.String", l.Index(0).Type())
42	}
43	if res != "hello" {
44		t.Errorf("failed list.Append() got: %+v, want: hello", res)
45	}
46}
47