Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 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 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 5 | package starlark_test |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 6 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 7 | // This file defines tests of the Value API. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 8 | |
| 9 | import ( |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 10 | "fmt" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 11 | "testing" |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 12 | |
Alan Donovan | 6beab7e | 2018-10-31 17:53:09 -0400 | [diff] [blame] | 13 | "go.starlark.net/starlark" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 16 | func TestStringMethod(t *testing.T) { |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 17 | s := starlark.String("hello") |
alandonovan | c996ede | 2018-10-12 13:53:43 -0400 | [diff] [blame] | 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 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 36 | func TestListAppend(t *testing.T) { |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 37 | l := starlark.NewList(nil) |
| 38 | l.Append(starlark.String("hello")) |
| 39 | res, ok := starlark.AsString(l.Index(0)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 40 | if !ok { |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 41 | t.Errorf("failed list.Append() got: %s, want: starlark.String", l.Index(0).Type()) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 42 | } |
| 43 | if res != "hello" { |
| 44 | t.Errorf("failed list.Append() got: %+v, want: hello", res) |
| 45 | } |
| 46 | } |