File: match_test.go

package info (click to toggle)
reflex 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: makefile: 4
file content (109 lines) | stat: -rw-r--r-- 2,569 bytes parent folder | download | duplicates (3)
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
package main

import (
	"regexp"
	"testing"
)

func TestMatchers(t *testing.T) {
	var (
		glob    = &globMatcher{glob: "foo*"}
		globInv = &globMatcher{glob: "foo*", inverse: true}

		regex    = newRegexMatcher(regexp.MustCompile("foo.*"), false)
		regexInv = newRegexMatcher(regexp.MustCompile("foo.*"), true)

		multi = multiMatcher{
			newRegexMatcher(regexp.MustCompile("foo"), false),
			newRegexMatcher(regexp.MustCompile(`\.go$`), false),
			newRegexMatcher(regexp.MustCompile("foobar"), true),
		}
	)
	for _, tt := range []struct {
		m    Matcher
		s    string
		want bool
	}{
		{glob, "foo", true},
		{glob, "foobar", true},
		{glob, "bar", false},
		{globInv, "foo", false},
		{globInv, "foobar", false},
		{globInv, "bar", true},

		{regex, "foo", true},
		{regex, "foobar", true},
		{regex, "bar", false},
		{regexInv, "foo", false},
		{regexInv, "foobar", false},
		{regexInv, "bar", true},

		{multi, "foo.go", true},
		{multi, "foo/bar.go", true},
		{multi, "foobar/blah.go", false},
	} {
		if got := tt.m.Match(tt.s); got != tt.want {
			t.Errorf("(%v).Match(%q): got %t; want %t",
				tt.m, tt.s, got, tt.want)
		}
	}
}

func TestExcludePrefix(t *testing.T) {
	m := newRegexMatcher(regexp.MustCompile("foo"), false)
	if m.ExcludePrefix("bar") {
		t.Error("m.ExcludePrefix gave true for a non-inverted matcher")
	}

	for _, tt := range []struct {
		re     string
		prefix string
		want   bool
	}{
		{"foo", "foo", true},
		{"((foo{3,4})|abc*)+|foo", "foo", true},
		{"foo$", "foo", false},
		{`foo\b`, "foo", false},
		{`(foo\b)|(baz$)`, "foo", false},
	} {
		m := newRegexMatcher(regexp.MustCompile(tt.re), true)
		if got := m.ExcludePrefix(tt.prefix); got != tt.want {
			t.Errorf("(%v).ExcludePrefix(%q): got %t; want %t",
				m, tt.prefix, got, tt.want)
		}
	}
}

func TestDefaultExcludes(t *testing.T) {
	for _, tt := range []struct {
		name string
		want bool
	}{
		{".git/HEAD", false},
		{"foo.git", true},
		{"foo/bar.git", true},
		{"foo/bar/.git/HEAD", false},
		{"foo~", false},
		{"foo/bar~", false},
		{"~foo", true},
		{"foo~bar", true},
		{"foo.swp", false},
		{"foo.swp.bar", true},
		{"foo/bar.swp", false},
		{"foo.#123", false},
		{"foo#123", true},
		{"foo/bar.#123", false},
		{"#foo#", false},
		{"foo/#bar#", false},
		{".DS_Store", false},
		{"foo/.DS_Store", false},
	} {
		if got := defaultExcludeMatcher.Match(tt.name); got != tt.want {
			if got {
				t.Errorf("%q was excluded by the default excludes matcher", tt.name)
			} else {
				t.Errorf("%q was not excluded by the default excludes matcher", tt.name)
			}
		}
	}
}