github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/util/glob/glob_test.go (about)

     1  package glob
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var globs = [][]string{
    11  	{"/", `^/$`},
    12  	{"/a", `^/a$`},
    13  	{"/a.b", `^/a\.b$`},
    14  	{"/a-b", `^/a\-b$`},
    15  	{"/a?", `^/a[^/]$`},
    16  	{"/a/b", `^/a/b$`},
    17  	{"/*", `^/[^/]*$`},
    18  	{"/*/a", `^/[^/]*/a$`},
    19  	{"/*a/b", `^/[^/]*a/b$`},
    20  	{"/a*/b", `^/a[^/]*/b$`},
    21  	{"/a*a/b", `^/a[^/]*a/b$`},
    22  	{"/*a*/b", `^/[^/]*a[^/]*/b$`},
    23  	{"/**", `^/.*$`},
    24  	{"/**/a", `^/.*/a$`},
    25  }
    26  
    27  var matches = [][]string{
    28  	{"/a/b", "/a/b"},
    29  	{"/a?", "/ab", "/ac"},
    30  	{"/a*", "/a", "/ab", "/abc"},
    31  	{"/a**", "/a", "/ab", "/abc", "/a/", "/a/b", "/ab/c"},
    32  	{`c:\a\b\.d`, `c:\a\b\.d`},
    33  	{`c:\**\.d`, `c:\a\b\.d`},
    34  }
    35  
    36  var nonMatches = [][]string{
    37  	{"/a/b", "/a/c", "/a/", "/a/b/", "/a/bc"},
    38  	{"/a?", "/", "/abc", "/a", "/a/"},
    39  	{"/a*", "/", "/a/", "/ba"},
    40  	{"/a**", "/", "/ba"},
    41  }
    42  
    43  var _ = Describe("Glob", func() {
    44  	It("translates globs to regexes", func() {
    45  		for _, parts := range globs {
    46  			pat, exp := parts[0], parts[1]
    47  			got, err := translateGlob(pat)
    48  
    49  			Expect(err).NotTo(HaveOccurred())
    50  			Expect(exp).To(Equal(got), "expected %q, but got %q from %q", exp, got, pat)
    51  		}
    52  	})
    53  
    54  	It("creates regexes that match correct file paths", func() {
    55  		for _, parts := range matches {
    56  			pat, paths := parts[0], parts[1:]
    57  			glob, err := CompileGlob(pat)
    58  
    59  			Expect(err).NotTo(HaveOccurred())
    60  			for _, path := range paths {
    61  				Expect(glob.Match(path)).To(BeTrue(), "path %q should match %q", pat, path)
    62  			}
    63  		}
    64  	})
    65  
    66  	It("creates regexes that do not match file incorrect file paths", func() {
    67  		for _, parts := range nonMatches {
    68  			pat, paths := parts[0], parts[1:]
    69  			glob, err := CompileGlob(pat)
    70  
    71  			Expect(err).NotTo(HaveOccurred())
    72  			for _, path := range paths {
    73  				Expect(glob.Match(path)).To(BeFalse(), "path %q should match %q", pat, path)
    74  			}
    75  		}
    76  	})
    77  })
    78  
    79  func TestGlobSuite(t *testing.T) {
    80  	RegisterFailHandler(Fail)
    81  	RunSpecs(t, "Glob Suite")
    82  }