blob: 6420a95b3b31b3722e8154075c267ef0ced2bceb [file] [log] [blame]
Alan Donovan312d1a52017-10-02 10:10:28 -04001// 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
Alan Donovane3deafe2018-10-23 11:05:09 -04005package starlark_test
alandonovanc996ede2018-10-12 13:53:43 -04006
Alan Donovan312d1a52017-10-02 10:10:28 -04007// This file defines tests of the Value API.
Alan Donovan312d1a52017-10-02 10:10:28 -04008
9import (
alandonovanc996ede2018-10-12 13:53:43 -040010 "fmt"
Alan Donovan312d1a52017-10-02 10:10:28 -040011 "testing"
alandonovanc996ede2018-10-12 13:53:43 -040012
Alan Donovan6beab7e2018-10-31 17:53:09 -040013 "go.starlark.net/starlark"
Alan Donovan312d1a52017-10-02 10:10:28 -040014)
15
alandonovanc996ede2018-10-12 13:53:43 -040016func TestStringMethod(t *testing.T) {
Alan Donovane3deafe2018-10-23 11:05:09 -040017 s := starlark.String("hello")
alandonovanc996ede2018-10-12 13:53:43 -040018 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
Alan Donovan312d1a52017-10-02 10:10:28 -040036func TestListAppend(t *testing.T) {
Alan Donovane3deafe2018-10-23 11:05:09 -040037 l := starlark.NewList(nil)
38 l.Append(starlark.String("hello"))
39 res, ok := starlark.AsString(l.Index(0))
Alan Donovan312d1a52017-10-02 10:10:28 -040040 if !ok {
Alan Donovane3deafe2018-10-23 11:05:09 -040041 t.Errorf("failed list.Append() got: %s, want: starlark.String", l.Index(0).Type())
Alan Donovan312d1a52017-10-02 10:10:28 -040042 }
43 if res != "hello" {
44 t.Errorf("failed list.Append() got: %+v, want: hello", res)
45 }
46}