forked from limetext/commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indent_test.go
174 lines (159 loc) · 3.64 KB
/
indent_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
// Copyright 2013 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 indentTest struct {
text string
translateTabsToSpaces interface{}
tabSize interface{}
sel []text.Region
expect string
}
func runIndentTest(t *testing.T, tests []indentTest, command string) {
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)
}
if val, ok := test.translateTabsToSpaces.(bool); ok {
v.Settings().Set("translate_tabs_to_spaces", val)
}
if val, ok := test.tabSize.(int); ok {
v.Settings().Set("tab_size", val)
}
ed.CommandHandler().RunTextCommand(v, command, nil)
if d := v.Substr(text.Region{0, v.Size()}); d != test.expect {
t.Errorf("Test %d: Expected \n%q, but got \n%q", i, test.expect, d)
}
}
}
func TestIndent(t *testing.T) {
tests := []indentTest{
{ // translate_tabs_to_spaces = false
// indent should be "\t"
"a\n b\n c\n d\n",
false,
4,
[]text.Region{{0, 1}},
"\ta\n b\n c\n d\n",
},
{ // translate_tabs_to_spaces = nil
// indent should be "\t"
"a\n b\n c\n d\n",
nil,
1,
[]text.Region{{0, 1}},
"\ta\n b\n c\n d\n",
},
{ // translate_tabs_to_spaces = true and tab_size = 2
// indent should be " "
"a\n b\n c\n d\n",
true,
2,
[]text.Region{{0, 1}},
" a\n b\n c\n d\n",
},
{ // translate_tabs_to_spaces = true and tab_size = nil
// indent should be " "
"a\n b\n c\n d\n",
true,
nil,
[]text.Region{{0, 1}},
" a\n b\n c\n d\n",
},
{ // region include the 1st line and the 4th line
// indent should add to the begining of 1st and 4th line
"a\n b\n c\n d\n",
false,
1,
[]text.Region{{0, 1}, {11, 12}},
"\ta\n b\n c\n\t d\n",
},
{ // region selected reversely
// should perform indent
"a\n b\n c\n d\n",
false,
1,
[]text.Region{{3, 0}},
"\ta\n\t b\n c\n d\n",
},
}
runIndentTest(t, tests, "indent")
}
func TestUnindent(t *testing.T) {
tests := []indentTest{
{ // translate_tabs_to_spaces = false
// indent should be "\t"
"\ta\n b\n c\n\t d\n",
false,
4,
[]text.Region{{0, 19}},
"a\nb\n c\n d\n",
},
{ // translate_tabs_to_spaces = nil
// indent should be "\t"
"\ta\n b\n c\n d\n",
nil,
1,
[]text.Region{{0, 1}},
"a\n b\n c\n d\n",
},
{ // translate_tabs_to_spaces = true and tab_size = 2
// indent should be " "
" a\n b\n c\n d\n",
true,
2,
[]text.Region{{0, 1}},
"a\n b\n c\n d\n",
},
{ // translate_tabs_to_spaces = true and tab_size = nil
// indent should be " "
" a\n b\n c\n d\n",
true,
nil,
[]text.Region{{0, 1}},
"a\n b\n c\n d\n",
},
{ // region include the 1st line and the 4th line
// unindent should remove from the begining of 1st and 4th line
"\ta\n b\n c\n \t d\n",
false,
1,
[]text.Region{{0, 1}, {11, 12}},
"a\n b\n c\n\t d\n",
},
{ // region selected reversely
// should perform unindent
"\ta\n\t b\n c\n d\n",
false,
4,
[]text.Region{{3, 0}},
"a\n b\n c\n d\n",
},
{ // empty strings
// should perform unindent
"",
false,
nil,
[]text.Region{{0, 0}},
"",
},
}
runIndentTest(t, tests, "unindent")
}