github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/glob/parse_test.go (about)

     1  package glob
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  var parseCases = []struct {
     9  	src  string
    10  	want []Segment
    11  }{
    12  	{``, []Segment{}},
    13  	{`foo`, []Segment{Literal{"foo"}}},
    14  	{`*foo*bar`, []Segment{
    15  		Wild{Star, false, nil}, Literal{"foo"},
    16  		Wild{Star, false, nil}, Literal{"bar"}}},
    17  	{`foo**bar`, []Segment{
    18  		Literal{"foo"}, Wild{StarStar, false, nil}, Literal{"bar"}}},
    19  	{`/usr/a**b/c`, []Segment{
    20  		Slash{}, Literal{"usr"}, Slash{}, Literal{"a"},
    21  		Wild{StarStar, false, nil}, Literal{"b"}, Slash{}, Literal{"c"}}},
    22  	{`??b`, []Segment{
    23  		Wild{Question, false, nil}, Wild{Question, false, nil}, Literal{"b"}}},
    24  	// Multiple slashes should be parsed as one.
    25  	{`//a//b`, []Segment{
    26  		Slash{}, Literal{"a"}, Slash{}, Literal{"b"}}},
    27  	// Escaping.
    28  	{`\*\?b`, []Segment{
    29  		Literal{"*?b"},
    30  	}},
    31  	{`abc\`, []Segment{
    32  		Literal{"abc"},
    33  	}},
    34  }
    35  
    36  func TestParse(t *testing.T) {
    37  	for _, tc := range parseCases {
    38  		p := Parse(tc.src)
    39  		if !reflect.DeepEqual(p.Segments, tc.want) {
    40  			t.Errorf("Parse(%q) => %v, want %v", tc.src, p, tc.want)
    41  		}
    42  	}
    43  }