-
Notifications
You must be signed in to change notification settings - Fork 8
/
sort_test.go
197 lines (183 loc) · 3.3 KB
/
sort_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2014 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import (
"testing"
"github.com/limetext/backend"
"github.com/limetext/text"
)
type sortTest struct {
text string
caseSensitive bool
reverse bool
removeDuplicates bool
sel []text.Region
expect string
}
func runSortTest(command string, tests []sortTest, t *testing.T) {
ed := backend.GetEditor()
w := ed.NewWindow()
defer w.Close()
for i, test := range tests {
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
e := v.BeginEdit()
v.Insert(e, 0, test.text)
v.EndEdit(e)
v.Sel().Clear()
for _, r := range test.sel {
v.Sel().Add(r)
}
args := backend.Args{
"case_sensitive": test.caseSensitive,
"reverse": test.reverse,
"remove_duplicates": test.removeDuplicates,
}
ed.CommandHandler().RunTextCommand(v, command, args)
if d := v.Substr(text.Region{0, v.Size()}); d != test.expect {
t.Errorf("Test %d: Excepted %#v,\n but got %#v", i, test.expect, d)
}
}
}
func TestSortLines(t *testing.T) {
tests := []sortTest{
{ // Case sensitive
"B\nc\na",
true,
false,
false,
[]text.Region{{0, 5}},
"B\na\nc",
},
{ // Case insensitive
"text\nSublime\nlime",
false,
false,
false,
[]text.Region{{0, 17}},
"lime\nSublime\ntext",
},
{ // Reverse
"b\nc\na",
true,
true,
false,
[]text.Region{{0, 5}},
"c\nb\na",
},
{ // Noncontinuous selection
"b\nc\na",
true,
false,
false,
[]text.Region{{0, 1}, {4, 5}},
"a\nc\nb",
},
{ // Noncontinuous selection, out of order
"b\nc\na",
true,
false,
false,
[]text.Region{{4, 5}, {0, 1}},
"a\nc\nb",
},
{ // Remove duplicates
"a\nb\na",
true,
false,
true,
[]text.Region{{0, 5}},
"a\nb\n",
},
{ // Remove duplicates case insensitive
"a\nb\nA",
false,
false,
true,
[]text.Region{{0, 5}},
"a\nb\n",
},
{ // No duplicates removal
"c\nb\na\nc\n",
true,
false,
false,
[]text.Region{{0, 8}},
"a\nb\nc\nc\n",
},
}
runSortTest("sort_lines", tests, t)
}
func TestSortSelection(t *testing.T) {
tests := []sortTest{
{ // Case sensitive
"Bca",
true,
false,
false,
[]text.Region{{0, 1}, {1, 2}, {2, 3}},
"Bac",
},
{ // Case insensitive
"textSublimelime",
false,
false,
false,
[]text.Region{{0, 4}, {4, 11}, {11, 15}},
"limeSublimetext",
},
{ // Reverse
"bca",
true,
true,
false,
[]text.Region{{0, 1}, {1, 2}, {2, 3}},
"cba",
},
{ // Noncontinuous selection
"bca",
true,
false,
false,
[]text.Region{{0, 1}, {2, 3}},
"acb",
},
{ // Noncontinuous selection, out of order
"bca",
true,
false,
false,
[]text.Region{{2, 3}, {0, 1}},
"acb",
},
{ // Remove duplicates
"aba",
true,
false,
true,
[]text.Region{{0, 1}, {1, 2}, {2, 3}},
"ab",
},
{ // Remove duplicates case insensitive
"abA",
false,
false,
true,
[]text.Region{{0, 1}, {1, 2}, {2, 3}},
"ab",
},
{ // No duplicates removal
"cbac",
true,
false,
false,
[]text.Region{{0, 1}, {1, 2}, {2, 3}, {3, 4}},
"abcc",
},
}
runSortTest("sort_selection", tests, t)
}