File: regex_test.go

package info (click to toggle)
miller 6.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 88,244 kB
  • sloc: ruby: 162; sh: 120; makefile: 87; python: 46
file content (186 lines) | stat: -rw-r--r-- 5,443 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
175
176
177
178
179
180
181
182
183
184
185
186
// Most Miller tests (thousands of them) are command-line-driven via
// mlr regtest. Here are some cases needing special focus.

package lib

import (
	"testing"
)

type tDataForHasCaptures struct {
	replacement         string
	expectedHasCaptures bool
	expectedMatrix      [][]int
}

type tDataForSubGsub struct {
	input          string
	sregex         string
	replacement    string
	expectedOutput string
}

type tDataForMatches struct {
	input            string
	sregex           string
	expectedOutput   bool
	expectedCaptures []string
}

var dataForHasCaptures = []tDataForHasCaptures{
	{"foo", false, nil},
	{"\\0", true, [][]int{{0, 2, 0, 2}}},
	{"\\3", true, [][]int{{0, 2, 0, 2}}},
	{"\\34", true, [][]int{{0, 2, 0, 2}}},
	{"abc\\1def\\2ghi", true, [][]int{{3, 5, 3, 5}, {8, 10, 8, 10}}},
}

var dataForSub = []tDataForSubGsub{
	{"abcde", "c", "X", "abXde"},
	{"abcde", "z", "X", "abcde"},
	{"abcde", "[a-z]", "X", "Xbcde"},
	{"abcde", "[A-Z]", "X", "abcde"},

	{"abcde", "c", "X", "abXde"},
	{"abcde", "z", "X", "abcde"},
	{"abcde", "[a-z]", "X", "Xbcde"},
	{"abcde", "[A-Z]", "X", "abcde"},

	{"ab_cde", "(..)_(...)", "\\2\\1", "cdeab"},
	{"ab_cde", "(..)_(...)", "\\2-\\1", "cde-ab"},
	{"ab_cde", "(..)_(...)", "X\\2Y\\1Z", "XcdeYabZ"},

	{"foofoofoo", "(f.o)", "b\\1r", "bfoorfoofoo"},
	{"foofoofoo", "(f.*o)", "b\\1r", "bfoofoofoor"},
	{"foofoofoo", "(f.o)", "b\\2r", "brfoofoo"},
	{"foofoofoo", "(f.*o)", "b\\2r", "br"},
}

var dataForGsub = []tDataForSubGsub{
	{"abcde", "c", "X", "abXde"},
	{"abcde", "z", "X", "abcde"},
	{"abcde", "[a-z]", "X", "XXXXX"},
	{"abcde", "[A-Z]", "X", "abcde"},
	{"abcde", "[c-d]", "X", "abXXe"},

	{"abcde", "c", "X", "abXde"},
	{"abcde", "z", "X", "abcde"},
	{"abcde", "[a-z]", "X", "XXXXX"},
	{"abcde", "[A-Z]", "X", "abcde"},
	{"abcde", "[c-d]", "X", "abXXe"},

	{"abacad", "a(.)", "<\\1>", "<b><c><d>"},
	{"abacad", "a(.)", "<\\2>", "<><><>"},
}

var dataForMatches = []tDataForMatches{
	{"abcde", "[A-Z]", false, []string{"", "", "", "", "", "", "", "", "", ""}},
	{"abcde", "[a-z]", true, []string{"a", "", "", "", "", "", "", "", "", ""}},
	{"...ab_cde...", "(..)_(...)", true, []string{"ab_cde", "ab", "cde", "", "", "", "", "", "", ""}},
	{"...ab_cde...fg_hij...", "(..)_(...)", true, []string{"ab_cde", "ab", "cde", "", "", "", "", "", "", ""}},
	{"foofoofoo", "(f.o)", true, []string{"foo", "foo", "", "", "", "", "", "", "", ""}},
	{"foofoofoo", "(f.*o)", true, []string{"foofoofoo", "foofoofoo", "", "", "", "", "", "", "", ""}},
}

func TestRegexReplacementHasCaptures(t *testing.T) {
	for i, entry := range dataForHasCaptures {
		actualHasCaptures, actualMatrix := ReplacementHasCaptures(entry.replacement)
		if actualHasCaptures != entry.expectedHasCaptures {
			t.Fatalf("case %d replacement \"%s\" expected %v got %v\n",
				i, entry.replacement, entry.expectedHasCaptures, actualHasCaptures,
			)
		}
		if !compareMatrices(actualMatrix, entry.expectedMatrix) {
			t.Fatalf("case %d replacement \"%s\" expected matrix %#v got %#v\n",
				i, entry.replacement, entry.expectedMatrix, actualMatrix,
			)
		}
	}
}

func TestRegexSub(t *testing.T) {
	for i, entry := range dataForSub {
		actualOutput := RegexStringSub(entry.input, entry.sregex, entry.replacement)
		if actualOutput != entry.expectedOutput {
			t.Fatalf("case %d input \"%s\" sregex \"%s\" replacement \"%s\" expected \"%s\" got \"%s\"\n",
				i, entry.input, entry.sregex, entry.replacement, entry.expectedOutput, actualOutput,
			)
		}
	}
}

func TestRegexGsub(t *testing.T) {
	for i, entry := range dataForGsub {
		actualOutput := RegexStringGsub(entry.input, entry.sregex, entry.replacement)
		if actualOutput != entry.expectedOutput {
			t.Fatalf("case %d input \"%s\" sregex \"%s\" replacement \"%s\" expected \"%s\" got \"%s\"\n",
				i, entry.input, entry.sregex, entry.replacement, entry.expectedOutput, actualOutput,
			)
		}
	}
}

func TestRegexMatches(t *testing.T) {
	for i, entry := range dataForMatches {
		actualOutput, actualCaptures := RegexStringMatchWithCaptures(entry.input, entry.sregex)
		if actualOutput != entry.expectedOutput {
			t.Fatalf("case %d input \"%s\" sregex \"%s\" expected %v got %v\n",
				i, entry.input, entry.sregex, entry.expectedOutput, actualOutput,
			)
		}
		if !compareCaptures(actualCaptures, entry.expectedCaptures) {
			t.Fatalf("case %d input \"%s\" sregex \"%s\" expected captures %#v got %#v\n",
				i, entry.input, entry.sregex, entry.expectedCaptures, actualCaptures,
			)
		}
	}
}

func compareMatrices(
	actualMatrix [][]int,
	expectedMatrix [][]int,
) bool {
	if actualMatrix == nil && expectedMatrix == nil {
		return true
	}
	if actualMatrix == nil || expectedMatrix == nil {
		return false
	}
	if len(actualMatrix) != len(expectedMatrix) {
		return false
	}
	for i := range expectedMatrix {
		actualRow := actualMatrix[i]
		expectedRow := expectedMatrix[i]
		if len(actualRow) != len(expectedRow) {
			return false
		}
		for j := range expectedRow {
			if actualRow[j] != expectedRow[j] {
				return false
			}
		}
	}
	return true
}

func compareCaptures(
	actualCaptures []string,
	expectedCaptures []string,
) bool {
	if actualCaptures == nil && expectedCaptures == nil {
		return true
	}
	if actualCaptures == nil || expectedCaptures == nil {
		return false
	}
	if len(actualCaptures) != len(expectedCaptures) {
		return false
	}
	for i := range expectedCaptures {
		if actualCaptures[i] != expectedCaptures[i] {
			return false
		}
	}
	return true
}