forked from orbitalquark/textadept-format
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_test.lua
More file actions
197 lines (161 loc) · 5.32 KB
/
init_test.lua
File metadata and controls
197 lines (161 loc) · 5.32 KB
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 2020-2026 Mitchell. See LICENSE.
local format = require('format')
local have_clang_format = LINUX or OSX and not os.getenv('CI')
local have_fmt = LINUX or OSX and not os.getenv('CI')
test('format.code should use clang-format if a .clang-format exists', function()
local file = 'file.c'
local dir<close> = test.tmpdir{
['.clang-format'] = 'BasedOnStyle: LLVM', --
[file] = 'int main(){return 0;}'
}
io.open_file(dir / file)
format.code()
test.assert_equal(buffer:get_text(), 'int main() { return 0; }')
end)
if not have_clang_format then skip('clang-format is not available') end
test('format.code should not format on save if format.on_save is disabled', function()
local _<close> = test.mock(format, 'on_save', false)
local file = 'file.c'
local code = test.lines{'int main(){return 0;}', ''}
local dir<close> = test.tmpdir{['.clang-format'] = 'BasedOnStyle: LLVM', file}
io.open_file(dir / file)
buffer:append_text(code)
buffer:save()
test.assert_equal(buffer:get_text(), code)
end)
if not have_clang_format then skip('clang-format is not available') end
test('format.on_save should ignore selected text', function()
local _<close> = test.mock(io, 'ensure_final_newline', false)
local file = 'file.c'
local dir<close> = test.tmpdir{['.clang-format'] = 'BasedOnStyle: LLVM', file}
io.open_file(dir / file)
buffer:append_text('int main(){return 0;}')
buffer:word_right_end_extend()
buffer:save()
test.assert_equal(buffer:get_text(), 'int main() { return 0; }')
test.assert_equal(buffer:get_sel_text(), 'int')
end)
if not have_clang_format then skip('clang-format is not available') end
test('format.code should ignore saving files matching format.ignore_file_patterns', function()
local subdir = 'subdir'
local subfile = 'subfile.c'
local code = test.lines{'int main(){return 0;}', ''}
local dir<close> = test.tmpdir{
['.clang-format'] = 'BasedOnStyle: LLVM', --
[subdir] = {[subfile] = code}
}
local _<close> = test.mock(format, 'ignore_file_patterns', {'/' .. subdir .. '/'})
io.open_file(dir / (subdir .. '/' .. subfile))
buffer:save()
test.assert_equal(buffer:get_text(), code)
end)
if not have_clang_format then skip('clang-format is not available') end
test('format.paragraph should reformat the current paragraph', function()
local _<close> = test.mock(format, 'line_length', 10)
buffer:append_text('this is a really long line')
format.paragraph()
test.assert_equal(buffer:get_text(), test.lines{
'this is', --
'a really', --
'long line', --
''
})
end)
if not have_fmt then skip('fmt is not available') end
test('format.paragraph should only reformat selected lines', function()
local _<close> = test.mock(format, 'line_length', 10)
buffer:append_text(test.lines{
'this is a', --
'really long', --
'line'
})
buffer:line_down_extend()
buffer:char_right_extend()
format.paragraph()
test.assert_equal(buffer:get_text(), test.lines{
'this is', --
'a really', --
'long', --
'line'
})
end)
if not have_fmt then skip('fmt is not available') end
test('format.paragraph should only reformat the current style (e.g. Lua comments)', function()
local _<close> = test.mock(format, 'line_length', 10)
local _<close> = test.tmpfile('.lua', test.lines{
'local x = 1', --
'-- This is a really long comment', --
'local y = 2'
}, true)
buffer:line_down()
format.paragraph()
test.assert_equal(buffer:get_text(), test.lines{
'local x = 1', --
'-- This', --
'-- is a', --
'-- really', --
'-- long', --
'-- comment', --
'local y = 2'
})
end)
if not have_fmt then skip('fmt is not available') end
test('format.paragraph should ignore header and footer lines (e.g. Doxygen comments)', function()
local _<close> = test.mock(format, 'line_length', 10)
local _<close> = test.tmpfile('.c', test.lines{
'/**', --
' * This is really long', --
' */', --
'int x;'
}, true)
buffer:line_down()
format.paragraph()
test.assert_equal(buffer:get_text(), test.lines{
'/**', --
' * This', --
' * is', --
' * really', --
' * long', --
' */', --
'int x;'
})
end)
if not have_fmt then skip('fmt is not available') end
test('format.paragraph should allow prefix mapping', function()
local _<close> = test.mock(format, 'line_length', 10)
local _<close> = test.tmpfile('.lua', test.lines{
'--- This is a really long comment', --
'local M = {}'
}, true)
format.paragraph()
test.assert_equal(buffer:get_text(), test.lines{
'--- This', --
'-- is a', --
'-- really', --
'-- long', --
'-- comment', --
'local M = {}'
})
end)
if not have_fmt then skip('fmt is not available') end
-- Coverage tests.
test('format.code should find files like .clang-format in subdirectories', function()
local subdir = 'subdir'
local subfile = 'subfile.c'
local dir<close> = test.tmpdir{
['.clang-format'] = 'BasedOnStyle: LLVM', --
[subdir] = {[subfile] = test.lines{'int main(){return 0;}', ''}}
}
io.open_file(dir / (subdir .. '/' .. subfile))
buffer:save()
test.assert_equal(buffer:get_text(), test.lines{'int main() { return 0; }', ''})
end)
if not have_clang_format then skip('clang-format is not available') end
test('format.code should not format without a file like .clang-format', function()
local file = 'file.c'
local code = 'int main(){return 0;}'
local dir<close> = test.tmpdir{[file] = code}
io.open_file(dir / file)
format.code()
test.assert_equal(buffer:get_text(), code)
end)