File: glob_test.go

package info (click to toggle)
golang-github-ryanuber-go-glob 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 72 kB
  • sloc: makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,485 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
package glob

import (
	"strings"
	"testing"
)

func testGlobMatch(t *testing.T, pattern, subj string) {
	if !Glob(pattern, subj) {
		t.Fatalf("%s should match %s", pattern, subj)
	}
}

func testGlobNoMatch(t *testing.T, pattern, subj string) {
	if Glob(pattern, subj) {
		t.Fatalf("%s should not match %s", pattern, subj)
	}
}

func TestEmptyPattern(t *testing.T) {
	testGlobMatch(t, "", "")
	testGlobNoMatch(t, "", "test")
}

func TestEmptySubject(t *testing.T) {
	for _, pattern := range []string{
		"",
		"*",
		"**",
		"***",
		"****************",
		strings.Repeat("*", 1000000),
	} {
		testGlobMatch(t, pattern, "")
	}

	for _, pattern := range []string{
		// No globs/non-glob characters
		"test",
		"*test*",

		// Trailing characters
		"*x",
		"*****************x",
		strings.Repeat("*", 1000000) + "x",

		// Leading characters
		"x*",
		"x*****************",
		"x" + strings.Repeat("*", 1000000),

		// Mixed leading/trailing characters
		"x*x",
		"x****************x",
		"x" + strings.Repeat("*", 1000000) + "x",
	} {
		testGlobNoMatch(t, pattern, "")
	}
}

func TestPatternWithoutGlobs(t *testing.T) {
	testGlobMatch(t, "test", "test")
}

func TestGlob(t *testing.T) {
	// Matches
	for _, pattern := range []string{
		"*test",           // Leading glob
		"this*",           // Trailing glob
		"this*test",       // Middle glob
		"*is *",           // String in between two globs
		"*is*a*",          // Lots of globs
		"**test**",        // Double glob characters
		"**is**a***test*", // Varying number of globs
		"* *",             // White space between globs
		"*",               // Lone glob
		"**********",      // Nothing but globs
		"*Ѿ*",             // Unicode with globs
		"*is a ϗѾ *",      // Mixed ASCII/unicode
	} {
		testGlobMatch(t, pattern, "this is a ϗѾ test")
	}

	// Non-matches
	for _, pattern := range []string{
		"test*",               // Implicit substring match
		"*is",                 // Partial match
		"*no*",                // Globs without a match between them
		" ",                   // Plain white space
		"* ",                  // Trailing white space
		" *",                  // Leading white space
		"*ʤ*",                 // Non-matching unicode
		"this*this is a test", // Repeated prefix
	} {
		testGlobNoMatch(t, pattern, "this is a test")
	}
}

func BenchmarkGlob(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !Glob("*quick*fox*dog", "The quick brown fox jumped over the lazy dog") {
			b.Fatalf("should match")
		}
	}
}