forked from zyedidia/sregx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sregx_test.go
141 lines (121 loc) · 2.54 KB
/
sregx_test.go
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
package sregx_test
import (
"bytes"
"regexp"
"testing"
"github.com/zyedidia/sregx"
)
type Test struct {
name string
input string
want string
}
func check(cmd sregx.Command, tests []Test, t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := cmd.Evaluate([]byte(tt.input))
if !bytes.Equal([]byte(tt.want), out) {
t.Errorf("got %q, want %q", out, tt.want)
}
})
}
}
func TestS(t *testing.T) {
cmd := sregx.S{
Patt: regexp.MustCompile("([A-Za-z]+) ([A-Za-z]+)"),
Replace: []byte("$2 $1"),
}
tests := []Test{
{"s1", "hello world", "world hello"},
}
check(cmd, tests, t)
}
func TestD(t *testing.T) {
cmd := sregx.X{
Patt: regexp.MustCompile("string"),
Cmd: sregx.D{},
}
tests := []Test{
{"d1", "string", ""},
{"d2", "hello string hi string test", "hello hi test"},
}
check(cmd, tests, t)
}
func TestCVar(t *testing.T) {
// Renames c variables called 'n' to 'num'. Omits matches in strings.
// expression: y/".*"/y/'.*'/x/[a-zA-Z0-9]+/g/n/v/../c/num/
cmd := sregx.Y{
Patt: regexp.MustCompile(`".*"`),
Cmd: sregx.Y{
Patt: regexp.MustCompile(`'.*'`),
Cmd: sregx.X{
Patt: regexp.MustCompile(`[a-zA-z0-9]+`),
Cmd: sregx.G{
Patt: regexp.MustCompile(`n`),
Cmd: sregx.V{
Patt: regexp.MustCompile(`..`),
Cmd: sregx.C{
Change: []byte("num"),
},
},
},
},
},
}
cin := `#include <stdio.h>
int main() {
char* n = "hello n \n";
printf("%s\n", n);
}
`
cout := `#include <stdio.h>
int main() {
char* num = "hello n \n";
printf("%s\n", num);
}
`
tests := []Test{
{"cvar1", "n", "num"},
{"cvar2", cin, cout},
}
check(cmd, tests, t)
}
func TestICapitalize(t *testing.T) {
// Program to capitalize 'i's
// x/[A-Za-z]+/ g/i/ v/../ c/I/
cmd := sregx.X{
Patt: regexp.MustCompile("[A-Za-z]+"),
Cmd: sregx.G{
Patt: regexp.MustCompile("i"),
Cmd: sregx.V{
Patt: regexp.MustCompile(".."),
Cmd: sregx.C{
Change: []byte("I"),
},
},
},
}
tests := []Test{
{"i1", "i am making tests", "I am making tests"},
{"i2", "ii i i i iii", "ii I I I iii"},
}
check(cmd, tests, t)
}
func TestICapitalizeAlternate(t *testing.T) {
// Alternate program to capitalize 'i's
// x/[A-Za-z]+/ g/^i$/ c/I/
cmd := sregx.X{
Patt: regexp.MustCompile("[A-Za-z]+"),
Cmd: sregx.G{
Patt: regexp.MustCompile("^i$"),
Cmd: sregx.C{
Change: []byte("I"),
},
},
}
tests := []Test{
{"ialt1", "i am making tests", "I am making tests"},
{"ialt2", "ii i i i iii", "ii I I I iii"},
}
check(cmd, tests, t)
}