github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/value_test.go (about)

     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  
     5  package starlark_test
     6  
     7  // This file defines tests of the Value API.
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/k14s/starlark-go/starlark"
    14  )
    15  
    16  func 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  
    36  func 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  }