github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/value_test.go (about)

     1  package eval
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/u-root/u-root/cmds/core/elvish/eval/vals"
     9  	"github.com/u-root/u-root/cmds/core/elvish/glob"
    10  )
    11  
    12  var reprTests = []struct {
    13  	v    interface{}
    14  	want string
    15  }{
    16  	{"233", "233"},
    17  	{"a\nb", `"a\nb"`},
    18  	{"foo bar", "'foo bar'"},
    19  	{"a\x00b", `"a\x00b"`},
    20  	{true, "$true"},
    21  	{false, "$false"},
    22  	{&Exception{nil, nil}, "$ok"},
    23  	{&Exception{errors.New("foo bar"), nil}, "?(fail 'foo bar')"},
    24  	{&Exception{
    25  		PipelineError{[]*Exception{{nil, nil}, {errors.New("lorem"), nil}}}, nil},
    26  		"?(multi-error $ok ?(fail lorem))"},
    27  	{&Exception{Return, nil}, "?(return)"},
    28  	{vals.EmptyList, "[]"},
    29  	{vals.MakeList("bash", false), "[bash $false]"},
    30  	{vals.MakeMap(map[interface{}]interface{}{}), "[&]"},
    31  	{vals.MakeMap(map[interface{}]interface{}{&Exception{nil, nil}: "elvish"}), "[&$ok=elvish]"},
    32  	// TODO: test maps of more elements
    33  }
    34  
    35  func TestRepr(t *testing.T) {
    36  	for _, test := range reprTests {
    37  		repr := vals.Repr(test.v, vals.NoPretty)
    38  		if repr != test.want {
    39  			t.Errorf("Repr = %s, want %s", repr, test.want)
    40  		}
    41  	}
    42  }
    43  
    44  var stringToSegmentsTests = []struct {
    45  	s    string
    46  	want []glob.Segment
    47  }{
    48  	{"", []glob.Segment{}},
    49  	{"a", []glob.Segment{glob.Literal{"a"}}},
    50  	{"/a", []glob.Segment{glob.Slash{}, glob.Literal{"a"}}},
    51  	{"a/", []glob.Segment{glob.Literal{"a"}, glob.Slash{}}},
    52  	{"/a/", []glob.Segment{glob.Slash{}, glob.Literal{"a"}, glob.Slash{}}},
    53  	{"a//b", []glob.Segment{glob.Literal{"a"}, glob.Slash{}, glob.Literal{"b"}}},
    54  }
    55  
    56  func TestStringToSegments(t *testing.T) {
    57  	for _, tc := range stringToSegmentsTests {
    58  		segs := stringToSegments(tc.s)
    59  		if !reflect.DeepEqual(segs, tc.want) {
    60  			t.Errorf("stringToSegments(%q) => %v, want %v", tc.s, segs, tc.want)
    61  		}
    62  	}
    63  }