github.com/lab47/exprcore@v0.0.0-20210525052339-fb7d6bd9331e/exprcore/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 exprcore_test
     6  
     7  // This file defines tests of the Value API.
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/lab47/exprcore/exprcore"
    14  )
    15  
    16  func TestStringMethod(t *testing.T) {
    17  	s := exprcore.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 := exprcore.NewList(nil)
    38  	l.Append(exprcore.String("hello"))
    39  	res, ok := exprcore.AsString(l.Index(0))
    40  	if !ok {
    41  		t.Errorf("failed list.Append() got: %s, want: exprcore.String", l.Index(0).Type())
    42  	}
    43  	if res != "hello" {
    44  		t.Errorf("failed list.Append() got: %+v, want: hello", res)
    45  	}
    46  }