-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_lex_test.go
183 lines (166 loc) · 4.29 KB
/
patch_lex_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
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
// SPDX-FileCopyrightText: 2020 Alvar Penning
//
// SPDX-License-Identifier: GPL-3.0-or-later
package pmwiki
import (
"reflect"
"testing"
)
func TestLexPatchValid(t *testing.T) {
input1 := ""
items1 := []patchLexItem{
{patchEOF, ""},
}
input2 := "\n\n\n\n"
items2 := []patchLexItem{
{patchEOF, ""},
}
input3 := "0a1\n"
items3 := []patchLexItem{
{patchRange, "0"},
{patchMode, "a"},
{patchRange, "1"},
{patchEOF, ""},
}
input4 := "0a1,5\n"
items4 := []patchLexItem{
{patchRange, "0"},
{patchMode, "a"},
{patchRange, "1,5"},
{patchEOF, ""},
}
input5 := "0a1\n> addition\n"
items5 := []patchLexItem{
{patchRange, "0"},
{patchMode, "a"},
{patchRange, "1"},
{patchAddition, "addition"},
{patchEOF, ""},
}
input6 := "0a1,3\n> multiline\n> addition\n> yay\n"
items6 := []patchLexItem{
{patchRange, "0"},
{patchMode, "a"},
{patchRange, "1,3"},
{patchAddition, "multiline"},
{patchAddition, "addition"},
{patchAddition, "yay"},
{patchEOF, ""},
}
input7 := "23d23\n< gone\n"
items7 := []patchLexItem{
{patchRange, "23"},
{patchMode, "d"},
{patchRange, "23"},
{patchDeletion, "gone"},
{patchEOF, ""},
}
input8 := "5c5\n< foo\n---\n> bar\n"
items8 := []patchLexItem{
{patchRange, "5"},
{patchMode, "c"},
{patchRange, "5"},
{patchDeletion, "foo"},
{patchAddition, "bar"},
{patchEOF, ""},
}
// Example from <https://en.wikipedia.org/wiki/Diff#Usage>
input9 := "0a1,6\n> This is an important\n> notice! It should\n> therefore be located at\n" +
"> the beginning of this\n> document!\n>\n11,15d16\n< This paragraph contains\n" +
"< text that is outdated.\n< It will be deleted in the\n< near future.\n<\n17c18\n" +
"< check this dokument. On\n---\n> check this document. On\n24a26,29\n>\n" +
"> This paragraph contains\n> important new additions\n> to this document.\n"
items9 := []patchLexItem{
{patchRange, "0"},
{patchMode, "a"},
{patchRange, "1,6"},
{patchAddition, "This is an important"},
{patchAddition, "notice! It should"},
{patchAddition, "therefore be located at"},
{patchAddition, "the beginning of this"},
{patchAddition, "document!"},
{patchAddition, ""},
{patchRange, "11,15"},
{patchMode, "d"},
{patchRange, "16"},
{patchDeletion, "This paragraph contains"},
{patchDeletion, "text that is outdated."},
{patchDeletion, "It will be deleted in the"},
{patchDeletion, "near future."},
{patchDeletion, ""},
{patchRange, "17"},
{patchMode, "c"},
{patchRange, "18"},
{patchDeletion, "check this dokument. On"},
{patchAddition, "check this document. On"},
{patchRange, "24"},
{patchMode, "a"},
{patchRange, "26,29"},
{patchAddition, ""},
{patchAddition, "This paragraph contains"},
{patchAddition, "important new additions"},
{patchAddition, "to this document."},
{patchEOF, ""},
}
tests := []struct {
name string
input string
items []patchLexItem
}{
{"empty input", input1, items1},
{"newlines", input2, items2},
{"simple header", input3, items3},
{"simple range header", input4, items4},
{"simple addition", input5, items5},
{"multiline addition", input6, items6},
{"simple deletion", input7, items7},
{"simple change", input8, items8},
{"Wikipedia example", input9, items9},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var items []patchLexItem
for item := range lexPatch(test.input) {
items = append(items, item)
}
if items == nil {
t.Fatal("there are no items")
}
if len(test.items) != len(items) {
t.Fatalf("length mismatches")
}
for i := 0; i < len(test.items); i++ {
if !reflect.DeepEqual(test.items[i], items[i]) {
t.Fatalf("%d, %v != %v", i, test.items[i], items[i])
}
}
})
}
}
func TestLexPatchInvalid(t *testing.T) {
tests := []struct {
name string
input string
}{
{"early eof", "1"},
{"invalid range", "1-2"},
{"double comma range", "1,2,3"},
{"invalid mode", "1x"},
{"invalid beginning", "| foo"},
{"invalid addition", ">A"},
{"invalid deletion", "<A"},
{"addition early end", "> addition"},
{"deletion early end", "< deletion"},
{"double dash", "--"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for item := range lexPatch(test.input) {
if item.t == patchError {
return
}
}
t.Fatal("received no error")
})
}
}