-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathansify_spec.lua
More file actions
403 lines (382 loc) · 25.1 KB
/
ansify_spec.lua
File metadata and controls
403 lines (382 loc) · 25.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
local ansify = require("ansify")
local unpack = table.unpack or unpack
local fg = ansify.foregrounds
local bg = ansify.backgrounds
local mod = ansify.modifiers
local function stringify(t)
local out = {}
for name, v in pairs(t) do
out[name] = tostring(v)
end
return out
end
describe("ansify", function()
it("exports modifiers table", function()
-- stylua: ignore
assert.are_same({
bold = "\27[1m",
dim = "\27[2m",
italic = "\27[3m",
underline = "\27[4m",
inverse = "\27[7m",
strikethrough = "\27[9m",
reset = "\27[0m",
}, stringify(mod))
end)
it("exports foregrounds table", function()
-- stylua: ignore
assert.are_same( {
black = "\27[30m",
red = "\27[31m",
green = "\27[32m",
yellow = "\27[33m",
blue = "\27[34m",
magenta = "\27[35m",
cyan = "\27[36m",
white = "\27[37m",
bright_black = "\27[90m",
bright_red = "\27[91m",
bright_green = "\27[92m",
bright_yellow = "\27[93m",
bright_blue = "\27[94m",
bright_magenta = "\27[95m",
bright_cyan = "\27[96m",
bright_white = "\27[97m",
}, stringify(fg))
end)
it("exports backgrounds table", function()
-- stylua: ignore
assert.are_same({
black = "\27[40m",
red = "\27[41m",
green = "\27[42m",
yellow = "\27[43m",
blue = "\27[44m",
magenta = "\27[45m",
cyan = "\27[46m",
white = "\27[47m",
bright_black = "\27[100m",
bright_red = "\27[101m",
bright_green = "\27[102m",
bright_yellow = "\27[103m",
bright_blue = "\27[104m",
bright_magenta = "\27[105m",
bright_cyan = "\27[106m",
bright_white = "\27[107m",
}, stringify(bg))
end)
it("uses the style value name in callable argument errors", function()
assert.has_error(function()
fg.red(false)
end, "bad argument #1 to 'red' (string expected, got boolean)")
end)
-- stylua: ignore
local tests = {
strip = {
{ "error" , { ansify.style({ "red" }, "error") } },
{ "plain text", { "plain text" } },
},
style = {
-- Foreground colors
{ "\27[31mTEXT\27[0m", { { fg.red }, "TEXT" } },
{ "\27[32mTEXT\27[0m", { { fg.green }, "TEXT" } },
{ "\27[34mTEXT\27[0m", { { fg.blue }, "TEXT" } },
{ "\27[36mTEXT\27[0m", { { fg.cyan }, "TEXT" } },
{ "\27[35mTEXT\27[0m", { { fg.magenta }, "TEXT" } },
{ "\27[33mTEXT\27[0m", { { fg.yellow }, "TEXT" } },
{ "\27[30mTEXT\27[0m", { { fg.black }, "TEXT" } },
{ "\27[37mTEXT\27[0m", { { fg.white }, "TEXT" } },
-- Bright foreground colors
{ "\27[90mTEXT\27[0m", { { fg.bright_black }, "TEXT" } },
{ "\27[91mTEXT\27[0m", { { fg.bright_red }, "TEXT" } },
{ "\27[92mTEXT\27[0m", { { fg.bright_green }, "TEXT" } },
{ "\27[93mTEXT\27[0m", { { fg.bright_yellow }, "TEXT" } },
{ "\27[94mTEXT\27[0m", { { fg.bright_blue }, "TEXT" } },
{ "\27[95mTEXT\27[0m", { { fg.bright_magenta }, "TEXT" } },
{ "\27[96mTEXT\27[0m", { { fg.bright_cyan }, "TEXT" } },
{ "\27[97mTEXT\27[0m", { { fg.bright_white }, "TEXT" } },
-- Background colors
{ "\27[41mTEXT\27[0m", { { bg.red }, "TEXT" } },
{ "\27[42mTEXT\27[0m", { { bg.green }, "TEXT" } },
{ "\27[44mTEXT\27[0m", { { bg.blue }, "TEXT" } },
{ "\27[46mTEXT\27[0m", { { bg.cyan }, "TEXT" } },
{ "\27[45mTEXT\27[0m", { { bg.magenta }, "TEXT" } },
{ "\27[43mTEXT\27[0m", { { bg.yellow }, "TEXT" } },
{ "\27[40mTEXT\27[0m", { { bg.black }, "TEXT" } },
{ "\27[47mTEXT\27[0m", { { bg.white }, "TEXT" } },
-- Bright background colors
{ "\27[100mTEXT\27[0m", { { bg.bright_black }, "TEXT" } },
{ "\27[101mTEXT\27[0m", { { bg.bright_red }, "TEXT" } },
{ "\27[102mTEXT\27[0m", { { bg.bright_green }, "TEXT" } },
{ "\27[103mTEXT\27[0m", { { bg.bright_yellow }, "TEXT" } },
{ "\27[104mTEXT\27[0m", { { bg.bright_blue }, "TEXT" } },
{ "\27[105mTEXT\27[0m", { { bg.bright_magenta }, "TEXT" } },
{ "\27[106mTEXT\27[0m", { { bg.bright_cyan }, "TEXT" } },
{ "\27[107mTEXT\27[0m", { { bg.bright_white }, "TEXT" } },
-- Modifiers
{ "\27[1mTEXT\27[0m", { { mod.bold }, "TEXT" } },
{ "\27[3mTEXT\27[0m", { { mod.italic }, "TEXT" } },
{ "\27[4mTEXT\27[0m", { { mod.underline }, "TEXT" } },
{ "\27[9mTEXT\27[0m", { { mod.strikethrough }, "TEXT" } },
{ "\27[2mTEXT\27[0m", { { mod.dim }, "TEXT" } },
{ "\27[7mTEXT\27[0m", { { mod.inverse }, "TEXT" } },
-- Style combinations and deduplication
{ "TEXT" , { { }, "TEXT" } },
{ "\27[1;3;4;31mTEXT\27[0m", { { "red" , "bold", "italic", "underline" }, "TEXT" } },
{ "\27[1;31mTEXT\27[0m" , { { "bold", "red" , "bold" }, "TEXT" } },
{ "\27[1;31mTEXT\27[0m" , { { "bold", "red" }, "TEXT" } },
{ "\27[1;34mTEXT\27[0m" , { { "blue", "bold" }, "TEXT" } },
{ "\27[91mTEXT\27[0m" , { { "bright_red" }, "TEXT" } },
{ "\27[31mTEXT\27[0m" , { { "red", fg.red }, "TEXT" } },
{ "\27[34mTEXT\27[0m" , { { "red", "blue" }, "TEXT" } },
{ "\27[44mTEXT\27[0m" , { { "on_red", "on_blue" }, "TEXT" } },
{ "\27[1;31;104mTEXT\27[0m", { { "bold", "red", "red", "on_bright_blue", "on_bright_blue" }, "TEXT" } },
-- Mixed styles and colors in a single list.
{ "\27[1;2;3;4;7;9;91;104mcombo\27[0m", { { "bold" , "dim" , "italic" , "underline" , "inverse" , "strikethrough" , "bright_red" , "on_bright_blue" }, "combo" } },
{ "\27[1;2;3;4;7;9;91;104mcombo\27[0m", { { mod.bold, mod.dim , mod.italic, mod.underline , mod.inverse, mod.strikethrough, fg.bright_red, bg.bright_blue }, "combo" } },
{ "\27[1;2;3;4;7;9;91;104mcombo\27[0m", { { "bold" , mod.dim , "italic" , mod.underline , "inverse" , mod.strikethrough, "bright_red" , bg.bright_blue }, "combo" } },
},
format = {
{ "plain text" , { "plain text" } },
{ "\27[1;31mstyled text\27[0m normal text", { "{bold}{red}styled text{normal} normal text" } },
{ "\27[1;31mERROR\27[0m: missing" , { "{bold}{red}ERROR{normal}: missing" } },
{ "plain \27[31mred \27[34mblue\27[0m" , { "plain {red}red {blue}blue" } },
{ "\27[31mx\27[0m" , { "{ red }x" } },
{ "error}" , { "error}" } },
{ "{red" , { "{red" } },
{ "red}" , { "red}" } },
{ "{red}" , { "{{red}}" } },
{ "{red} plain" , { "{{red}} plain" } },
{ "{{red}} plain" , { "{{{red}}} plain" } },
{ "{{}}" , { "{{{}}}" } },
{ "\27[31mred\27[0m plain" , { "{red}red{normal} plain" } },
{ "\27[1;31mx\27[0m" , { "{red}{bold}x" } },
{ "\27[31mx\27[0m" , { "{red}{red}x" } },
{ "\27[1mx\27[0m" , { "{bold}{bold}x" } },
{ "\27[1;34mx\27[0m" , { "{red}{blue}{bold}{bold}x" } },
{ "" , { "{missing}error" } },
{ "" , { "{bold red}error" } },
{ "" , { "{}" } },
},
append = {
{ "\27[1;31mERROR: config missing\27[0m", { ansify.style({ "bold", "red" }, "ERROR"), ": config missing" } },
{ "error: config missing" , { "error" , ": config missing" } },
{ "x!\27[0m\27[0m" , { "x" .. mod.reset .. mod.reset, "!" } },
},
prepend = {
{ "\27[1;31mERROR: config missing\27[0m", { ansify.style({ "bold", "red" }, "config missing"), "ERROR: " } },
{ "ERROR: config missing" , { "config missing" , "ERROR: " } },
{ "\27[1m\27[31m>x\27[0m" , { mod.bold .. fg.red .. "x" .. mod.reset, ">" } },
},
slice = {
{ "fatal error in config loader", { "fatal error in config loader" } },
{ "error" , { "fatal error in config loader" , 7, 11 } },
{ "error in config loader" , { "fatal error in config loader" , 7 } },
{ "\27[31merror\27[0m" , { ansify.style({ "red" }, "fatal error in config loader") , 7, 11 } },
{ "\27[31mb\27[0m\27[34mc\27[0m", { ansify.style({ fg.red }, "ab") .. ansify.style({ fg.blue }, "cd"), 2, 3 } },
{ "" , { "plain text" , 20, 30 } },
{ "" , { "plain text" , 4, 3 } },
{ "bcd" , { "abcdef" , 2.9, 4.9 } },
{ "a" , { "abcdef" , 0, 0 } },
{ "a" , { "abcdef" , -2, -1 } },
},
has_ansi = {
{ true , { ansify.style({ "red" }, "error") } },
{ false, { "plain text" } },
},
-- Wrapped lines are expected to be self-contained ANSI fragments, so each
-- line closes its active style before "\n" and reopens it on the next line.
wrap = {
{ "one two\nthree\nfour" , { "one two three\nfour" , 7 } },
{ "\27[31mabc\27[0m\n\27[31mdef\27[0m" , { ansify.style({ "red" }, "abcdef") , 3 } },
{ "one\ntwo" , { "one\ntwo" , 4 } },
{ "one\n\ntwo" , { "one\n\ntwo" , 4 } },
{ "\27[31mone\27[0m\n\27[31mtwo\27[0m" , { ansify.style({ "red" }, "one\ntwo") , 4 } },
{ "\27[31mone\27[0m \27[31mtwo\27[0m\n\27[31mthree\27[0m", { ansify.style({ "red" }, "one two three") , 7 } },
{ "\27[31mab\27[0m\n\27[34mcd\27[0m" , { ansify.style({ fg.red }, "ab") .. ansify.style({ fg.blue }, "cd"), 2 } },
{ "a\nbcd\nef" , { "a bcdef" , 3 } },
{ "one\n" , { "one\n" , 4 } },
{ "abc\ndef" , { "abcdef" , 3.8 } },
{ "a\nb" , { "ab" , 0 } },
{ "a\nb" , { "ab" , -2 } },
},
len = {
{ 5 , { ansify.style({ "red" }, "error") } },
{ 10, { "plain text" } },
},
align = {
{ "ok " , { "ok" , 5 , "left" } },
{ "\27[31mok\27[0m " , { ansify.style({ "red" }, "ok"), 5 , "left" } },
{ "ok" , { "ok" , -5 , "left" } },
{ "ok " , { "ok" , 4.9, "left" } },
{ "ok" , { "ok" , 0 , "left" } },
{ " ok " , { "ok" , 5 , "center" } },
{ " ok " , { "ok" , 4.9, "center" } },
{ "ok" , { "ok" , 0 , "center" } },
{ "ok" , { "ok" , -5 , "center" } },
{ " \27[31mok\27[0m " , { ansify.style({ "red" }, "ok"), 5 , "center" } },
{ " ok" , { "ok" , 4.9, "right" } },
{ "ok" , { "ok" , 0 , "right" } },
{ "ok" , { "ok" , -5 , "right" } },
{ " ok" , { "ok" , 5 , "right" } },
{ " \27[31mok\27[0m" , { ansify.style({ "red" }, "ok"), 5 , "right" } },
},
escape = {
{ "\\27[31mline\\n\\tend" , { "\27[31mline\n\tend" } },
{ "\\t\\27[31mred\\27[0m\\nplain" , { "\t" .. ansify.style({ "red" }, "red") .. "\nplain" } },
{ "\\27[1;31merror\\27[0m\\n\\t\\27[34mnext\\27[0m", { ansify.style({ "bold", "red" }, "error") .. "\n\t" .. ansify.style({ "blue" }, "next") } },
{ "a\\tb\\nc\\27[0m\\t" , { "a\tb\nc" .. mod.reset .. "\t" } },
{ "plain" , { "plain" } },
},
truncate = {
{ "pla" , { "plain" , 3 } },
{ "pla" , { "plain" , 3.9 } },
{ "\27[31merr\27[0m" , { ansify.style({ "red" }, "error") , 3 } },
{ "" , { ansify.style({ "red" }, "error") , 0 } },
{ "" , { ansify.style({ "red" }, "error") , -1 } },
{ "\27[31mab\27[0m\27[34mc\27[0m", { ansify.style({ fg.red }, "ab") .. ansify.style({ fg.blue }, "cd"), 3 } },
},
words = {
{ { "\27[31mbuild\27[0m", "\27[31mcommand\27[0m", "\27[31mfailed\27[0m" }, { ansify.style({ "red" } , "build command failed") } },
{ { "\27[1;31mfatal\27[0m", "\27[1;31mconfig\27[0m", "missing" } , { ansify.style({ "bold", "red" }, "fatal config") .. " missing" } },
{ { "\27[31ma\27[0m", "\27[31mb\27[0m" } , { ansify.style({ "red" } , "a\nb") } },
{ {} , { ansify.style({ "red" } , " ") } },
{ { "plain", "text" } , { "plain text" } },
{ {} , { " \n\t" } },
},
lines = {
{ { "one", "two" } , { "one\ntwo" } },
{ { "one", "two" } , { "one\n\ntwo" } },
{ { "\27[31mone\27[0m", "\27[31mtwo\27[0m" } , { ansify.style({ "red" }, "one\ntwo") } },
{ { "one" } , { "one\n" } },
{ {} , { "\n\n\n" } },
{ {} , { "" } },
},
}
for fname, t in pairs(tests) do
for i = 1, #t do
it(fname .. "()", function()
local expected, args = unpack(t[i], 1, 2)
---@diagnostic disable-next-line: param-type-mismatch
assert.are_same(expected, ansify[fname](unpack(args)))
end)
end
end
-- stylua: ignore
local errors = {
callable_styles = {
-- modifiers
{ "bad argument #1 to 'bold' (string expected, got boolean)" , function() mod.bold(false) end},
{ "bad argument #1 to 'dim' (string expected, got boolean)" , function() mod.dim(false) end},
{ "bad argument #1 to 'italic' (string expected, got boolean)" , function() mod.italic(false) end},
{ "bad argument #1 to 'underline' (string expected, got boolean)" , function() mod.underline(false) end},
{ "bad argument #1 to 'inverse' (string expected, got boolean)" , function() mod.inverse(false) end},
{ "bad argument #1 to 'strikethrough' (string expected, got boolean)", function() mod.strikethrough(false) end},
{ "bad argument #1 to 'reset' (string expected, got boolean)" , function() mod.reset(false) end},
-- foregrounds
{ "bad argument #1 to 'red' (string expected, got boolean)" , function() fg.red(false) end },
{ "bad argument #1 to 'green' (string expected, got boolean)" , function() fg.green(false) end },
{ "bad argument #1 to 'blue' (string expected, got boolean)" , function() fg.blue(false) end },
{ "bad argument #1 to 'cyan' (string expected, got boolean)" , function() fg.cyan(false) end },
{ "bad argument #1 to 'magenta' (string expected, got boolean)" , function() fg.magenta(false) end },
{ "bad argument #1 to 'yellow' (string expected, got boolean)" , function() fg.yellow(false) end },
{ "bad argument #1 to 'black' (string expected, got boolean)" , function() fg.black(false) end },
{ "bad argument #1 to 'white' (string expected, got boolean)" , function() fg.white(false) end },
{ "bad argument #1 to 'bright_red' (string expected, got boolean)" , function() fg.bright_red(false) end },
{ "bad argument #1 to 'bright_green' (string expected, got boolean)" , function() fg.bright_green(false) end },
{ "bad argument #1 to 'bright_blue' (string expected, got boolean)" , function() fg.bright_blue(false) end },
{ "bad argument #1 to 'bright_cyan' (string expected, got boolean)" , function() fg.bright_cyan(false) end },
{ "bad argument #1 to 'bright_magenta' (string expected, got boolean)", function() fg.bright_magenta(false) end },
{ "bad argument #1 to 'bright_yellow' (string expected, got boolean)" , function() fg.bright_yellow(false) end },
{ "bad argument #1 to 'bright_black' (string expected, got boolean)" , function() fg.bright_black(false) end },
{ "bad argument #1 to 'bright_white' (string expected, got boolean)" , function() fg.bright_white(false) end },
-- backgrounds
{ "bad argument #1 to 'on_red' (string expected, got boolean)" , function() bg.red(false) end },
{ "bad argument #1 to 'on_green' (string expected, got boolean)" , function() bg.green(false) end },
{ "bad argument #1 to 'on_blue' (string expected, got boolean)" , function() bg.blue(false) end },
{ "bad argument #1 to 'on_cyan' (string expected, got boolean)" , function() bg.cyan(false) end },
{ "bad argument #1 to 'on_magenta' (string expected, got boolean)" , function() bg.magenta(false) end },
{ "bad argument #1 to 'on_yellow' (string expected, got boolean)" , function() bg.yellow(false) end },
{ "bad argument #1 to 'on_black' (string expected, got boolean)" , function() bg.black(false) end },
{ "bad argument #1 to 'on_white' (string expected, got boolean)" , function() bg.white(false) end },
{ "bad argument #1 to 'on_bright_red' (string expected, got boolean)" , function() bg.bright_red(false) end },
{ "bad argument #1 to 'on_bright_green' (string expected, got boolean)" , function() bg.bright_green(false) end },
{ "bad argument #1 to 'on_bright_blue' (string expected, got boolean)" , function() bg.bright_blue(false) end },
{ "bad argument #1 to 'on_bright_cyan' (string expected, got boolean)" , function() bg.bright_cyan(false) end },
{ "bad argument #1 to 'on_bright_magenta' (string expected, got boolean)", function() bg.bright_magenta(false) end },
{ "bad argument #1 to 'on_bright_yellow' (string expected, got boolean)" , function() bg.bright_yellow(false) end },
{ "bad argument #1 to 'on_bright_black' (string expected, got boolean)" , function() bg.bright_black(false) end },
{ "bad argument #1 to 'on_bright_white' (string expected, got boolean)" , function() bg.bright_white(false) end },
},
style = {
{ "bad argument #1 to 'style' (table expected, got no value)", { } },
{ "bad argument #2 to 'style' (string expected, got boolean)", { { "red" }, true } },
{ "invalid value (number) at index 2 in table for 'style'" , { { "red" , 123 }, "error" } },
{ "unknown style: bright" , { { "bright", "bold" }, "error" } },
{ "unknown style: missing" , { { "missing" }, "error" } },
{ "unknown style: reset" , { { "reset" }, "error" } },
},
format = {
{ "bad argument #1 to 'format' (string expected, got number)", { 42 } },
},
append = {
{ "bad argument #1 to 'append' (string expected, got number)", { 42 , "!" } },
{ "bad argument #2 to 'append' (string expected, got number)", { ansify.style({ "red" }, "error"), 42 } },
},
prepend = {
{ "bad argument #1 to 'prepend' (string expected, got boolean)", { false, "ERROR: " } },
{ "bad argument #2 to 'prepend' (string expected, got boolean)", { "ok" , false } },
},
slice = {
{ "bad argument #1 to 'slice' (string expected, got number)", { 42 , 1 , 2 } },
{ "bad argument #2 to 'slice' (number expected, got string)", { "ok", "1", 2 } },
{ "bad argument #3 to 'slice' (number expected, got string)", { "ok", 1 , "2" } },
},
align = {
{ "bad argument #1 to 'align' (string expected, got number)", { 42 , 5 , "left" } },
{ "bad argument #2 to 'align' (number expected, got string)", { "ok", "5", "left" } },
{ "bad argument #3 to 'align' (string expected, got table)" , { "ok", 5 , {} } },
{ "bad argument #3 to 'align' (invalid alignment: diagonal)", { "ok", 5 , "diagonal" } },
},
truncate = {
{ "bad argument #1 to 'truncate' (string expected, got number)", { 42 , 5 } },
{ "bad argument #2 to 'truncate' (number expected, got string)", { "ok", "5" } },
},
wrap = {
{ "bad argument #1 to 'wrap' (string expected, got number)", { 42 , 5 } },
{ "bad argument #2 to 'wrap' (number expected, got string)", { "ok", "5" } },
},
has_ansi = {
{ "bad argument #1 to 'has_ansi' (string expected, got number)", { 42 } },
},
len = {
{ "bad argument #1 to 'len' (string expected, got number)", { 42 } },
},
escape = {
{ "bad argument #1 to 'escape' (string expected, got number)", { 42 } },
},
words = {
{ "bad argument #1 to 'words' (string expected, got number)", { 42 } },
},
lines = {
{ "bad argument #1 to 'lines' (string expected, got number)", { 42 } },
},
strip = {
{ "bad argument #1 to 'strip' (string expected, got number)", { 42 } }
},
}
for fname, t in pairs(errors) do
for _, v in ipairs(t) do
local errmsg, args = v[1], v[2]
it(fname .. "()", function()
if type(args) == "function" then
assert.has_error(args, errmsg)
else
assert.has_error(function()
ansify[fname](unpack(args))
end, errmsg)
end
end)
end
end
end)