File: glob_test.go

package info (click to toggle)
golang-github-mb0-glob 0.0~git20160210.1eb79d2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 80 kB
  • sloc: makefile: 2
file content (174 lines) | stat: -rw-r--r-- 2,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2012 The Go Authors for the original package path/filepath.
// Copyright 2012 Martin Schnabel for modifications.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package glob

import "testing"

func TestMetaRegexp(t *testing.T) {
	expr, err := Default().metaRegexp()
	if err != nil {
		t.Fatal(err)
	}
	match := []string{
		`fo*`,
		`fo?`,
		`fo[o]`,
		`*oo`,
	}
	fail := []string{
		`foo`,
		`foo/bar`,
		`foo\*`,
		`fo\?`,
		`fo\[o\]`,
	}

	for _, str := range match {
		if !expr.MatchString(str) {
			t.Errorf("%s must match", str)
		}
	}
	for _, str := range fail {
		if expr.MatchString(str) {
			t.Errorf("%s must fail", str)
		}
	}
}

func TestGlobStrings(t *testing.T) {
	list := []string{
		"a.b.c",
		"a.b.c.d",
		"a.bar",
		"a.foo",
		"b.bar",
		"b.foo",
	}
	tests := []struct {
		pat string
		res []string
	}{
		{"a.bar", []string{
			"a.bar",
		}},
		{"*", []string{
			"a",
			"b",
		}},
		{"a.*", []string{
			"a.b", "a.bar", "a.foo",
		}},
		{"a.b*", []string{
			"a.b", "a.bar",
		}},
		{"*.bar", []string{
			"a.bar", "b.bar",
		}},
		{"**", []string{
			"a",
			"a.b",
			"a.b.c",
			"a.b.c.d",
			"a.bar",
			"a.foo",
			"b",
			"b.bar",
			"b.foo",
		}},
		{"**.[^c-f]**", []string{
			"a.b",
			"a.b.c",
			"a.b.c.d",
			"a.bar",
			"b.bar",
		}},
	}
	c := Default()
	c.Separator = '.'
	g, err := New(c)
	if err != nil {
		t.Fatal(err)
	}
	for _, test := range tests {
		res, err := g.GlobStrings(list, test.pat)
		if err != nil {
			t.Error(err)
			continue
		}
		if !equals(res, test.res) {
			t.Errorf("pattern %q got:\n%v ", test.pat, res)
		}
	}
}

func TestFilepaths(t *testing.T) {
	list := []string{
		"a/b/c",
		"d/e/f",
		"g/h/i",
	}
	tests := []struct {
		pat string
		res []string
	}{
		{"", []string{"a", "d", "g"}},
		{"a", []string{"a/b"}},
		{"a/b", []string{"a/b/c"}},
	}
	g := defaultGlobber
	for _, test := range tests {
		res := g.filepaths(list, test.pat, false)
		if !equals(res, test.res) {
			t.Errorf("pattern %q got:\n%v ", test.pat, res)
		}
	}
}

func TestMatch(t *testing.T) {
	g := defaultGlobber
	match := []string{
		`foo/bar/baz`,
		`foo/*/baz`,
		`**/baz`,
		`**baz`,
		`foo/**`,
		`**z`,
		`f**z`,
		`?o**z`,
		`?o**z`,
		`**/*z`,
	}
	fail := []string{
		`foo`,
		`foo/*`,
		`*/baz`,
		`o**z`,
		`**/*r`,
	}
	var str = "foo/bar/baz"
	for _, pat := range match {
		if m, err := g.Match(pat, str); !m {
			t.Errorf("%s must match %v", pat, err)
		}
	}
	for _, pat := range fail {
		if m, err := g.Match(pat, str); m {
			t.Errorf("%s must fail %v", pat, err)
		}
	}
}

func equals(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i, str := range a {
		if str != b[i] {
			return false
		}
	}
	return true
}