robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.6 KiB715 linesraw
1-- Tests for :sort command.
2
3local t = require('test.testutil')
4local describe, it, before_each = t.describe, t.it, t.before_each
5local pcall_err = t.pcall_err
6local n = require('test.functional.testnvim')()
7
8local insert, command, clear, expect, eq, poke_eventloop =
9  n.insert, n.command, n.clear, n.expect, t.eq, n.poke_eventloop
10
11describe(':sort', function()
12  local text = [[
13    abc
14    ab
15    a
16    a321
17    a123
18    a122
19    b321
20    b123
21    c123d
22     123b
23    c321d
24    b322b
25    b321
26    b321b
27    ]]
28  before_each(clear)
29
30  it('alphabetical', function()
31    insert(text)
32    poke_eventloop()
33    command('sort')
34    expect([[
35
36       123b
37      a
38      a122
39      a123
40      a321
41      ab
42      abc
43      b123
44      b321
45      b321
46      b321b
47      b322b
48      c123d
49      c321d]])
50  end)
51
52  it('numerical', function()
53    insert([[
54      abc
55      ab
56      a321
57      a123
58      a122
59      a
60      x-22
61      b321
62      b123
63      c123d
64      -24
65       123b
66      c321d
67      0
68      b322b
69      b321
70      b321b
71      ]])
72    poke_eventloop()
73    command('sort n')
74    expect([[
75      abc
76      ab
77      a
78
79      -24
80      x-22
81      0
82      a122
83      a123
84      b123
85      c123d
86       123b
87      a321
88      b321
89      c321d
90      b321
91      b321b
92      b322b]])
93  end)
94
95  it('hexadecimal', function()
96    insert(text)
97    poke_eventloop()
98    command('sort x')
99    expect([[
100
101      a
102      ab
103      abc
104       123b
105      a122
106      a123
107      a321
108      b123
109      b321
110      b321
111      b321b
112      b322b
113      c123d
114      c321d]])
115  end)
116
117  it('alphabetical, unique', function()
118    insert(text)
119    poke_eventloop()
120    command('sort u')
121    expect([[
122
123       123b
124      a
125      a122
126      a123
127      a321
128      ab
129      abc
130      b123
131      b321
132      b321b
133      b322b
134      c123d
135      c321d]])
136  end)
137
138  it('alphabetical, reverse', function()
139    insert(text)
140    poke_eventloop()
141    command('sort!')
142    expect([[
143      c321d
144      c123d
145      b322b
146      b321b
147      b321
148      b321
149      b123
150      abc
151      ab
152      a321
153      a123
154      a122
155      a
156       123b
157      ]])
158  end)
159
160  it('numerical, reverse', function()
161    insert(text)
162    poke_eventloop()
163    command('sort! n')
164    expect([[
165      b322b
166      b321b
167      b321
168      c321d
169      b321
170      a321
171       123b
172      c123d
173      b123
174      a123
175      a122
176
177      a
178      ab
179      abc]])
180  end)
181
182  it('unique, reverse', function()
183    insert(text)
184    poke_eventloop()
185    command('sort! u')
186    expect([[
187      c321d
188      c123d
189      b322b
190      b321b
191      b321
192      b123
193      abc
194      ab
195      a321
196      a123
197      a122
198      a
199       123b
200      ]])
201  end)
202
203  it('octal', function()
204    insert(text)
205    poke_eventloop()
206    command('sort o')
207    expect([[
208      abc
209      ab
210      a
211
212      a122
213      a123
214      b123
215      c123d
216       123b
217      a321
218      b321
219      c321d
220      b321
221      b321b
222      b322b]])
223  end)
224
225  it('reverse, hexadecimal', function()
226    insert(text)
227    poke_eventloop()
228    command('sort! x')
229    expect([[
230      c321d
231      c123d
232      b322b
233      b321b
234      b321
235      b321
236      b123
237      a321
238      a123
239      a122
240       123b
241      abc
242      ab
243      a
244      ]])
245  end)
246
247  it('alphabetical, skip first character', function()
248    insert(text)
249    poke_eventloop()
250    command('sort/./')
251    expect([[
252      a
253
254      a122
255      a123
256      b123
257       123b
258      c123d
259      a321
260      b321
261      b321
262      b321b
263      c321d
264      b322b
265      ab
266      abc]])
267  end)
268
269  it('alphabetical, skip first 2 characters', function()
270    insert(text)
271    poke_eventloop()
272    command('sort/../')
273    expect([[
274      ab
275      a
276
277      a321
278      b321
279      b321
280      b321b
281      c321d
282      a122
283      b322b
284      a123
285      b123
286       123b
287      c123d
288      abc]])
289  end)
290
291  it('alphabetical, unique, skip first 2 characters', function()
292    insert(text)
293    poke_eventloop()
294    command('sort/../u')
295    expect([[
296      ab
297      a
298
299      a321
300      b321
301      b321b
302      c321d
303      a122
304      b322b
305      a123
306      b123
307       123b
308      c123d
309      abc]])
310  end)
311
312  it('numerical, skip first character', function()
313    insert(text)
314    poke_eventloop()
315    command('sort/./n')
316    expect([[
317      abc
318      ab
319      a
320
321      a122
322      a123
323      b123
324      c123d
325       123b
326      a321
327      b321
328      c321d
329      b321
330      b321b
331      b322b]])
332  end)
333
334  it('alphabetical, sort on first character', function()
335    insert(text)
336    poke_eventloop()
337    command('sort/./r')
338    expect([[
339
340       123b
341      abc
342      ab
343      a
344      a321
345      a123
346      a122
347      b321
348      b123
349      b322b
350      b321
351      b321b
352      c123d
353      c321d]])
354  end)
355
356  it('alphabetical, sort on first 2 characters', function()
357    insert(text)
358    poke_eventloop()
359    command('sort/../r')
360    expect([[
361      a
362
363       123b
364      a123
365      a122
366      a321
367      abc
368      ab
369      b123
370      b321
371      b322b
372      b321
373      b321b
374      c123d
375      c321d]])
376  end)
377
378  it('numerical, sort on first character', function()
379    insert(text)
380    poke_eventloop()
381    command('sort/./rn')
382    expect([[
383      abc
384      ab
385      a
386      a321
387      a123
388      a122
389      b321
390      b123
391      c123d
392       123b
393      c321d
394      b322b
395      b321
396      b321b
397      ]])
398  end)
399
400  it('alphabetical, skip past first digit', function()
401    insert(text)
402    poke_eventloop()
403    command([[sort/\d/]])
404    expect([[
405      abc
406      ab
407      a
408
409      a321
410      b321
411      b321
412      b321b
413      c321d
414      a122
415      b322b
416      a123
417      b123
418       123b
419      c123d]])
420  end)
421
422  it('alphabetical, sort on first digit', function()
423    insert(text)
424    poke_eventloop()
425    command([[sort/\d/r]])
426    expect([[
427      abc
428      ab
429      a
430
431      a123
432      a122
433      b123
434      c123d
435       123b
436      a321
437      b321
438      c321d
439      b322b
440      b321
441      b321b]])
442  end)
443
444  it('numerical, skip past first digit', function()
445    insert(text)
446    poke_eventloop()
447    command([[sort/\d/n]])
448    expect([[
449      abc
450      ab
451      a
452
453      a321
454      b321
455      c321d
456      b321
457      b321b
458      a122
459      b322b
460      a123
461      b123
462      c123d
463       123b]])
464  end)
465
466  it('numerical, sort on first digit', function()
467    insert(text)
468    poke_eventloop()
469    command([[sort/\d/rn]])
470    expect([[
471      abc
472      ab
473      a
474
475      a123
476      a122
477      b123
478      c123d
479       123b
480      a321
481      b321
482      c321d
483      b322b
484      b321
485      b321b]])
486  end)
487
488  it('alphabetical, skip past first 2 digits', function()
489    insert(text)
490    poke_eventloop()
491    command([[sort/\d\d/]])
492    expect([[
493      abc
494      ab
495      a
496
497      a321
498      b321
499      b321
500      b321b
501      c321d
502      a122
503      b322b
504      a123
505      b123
506       123b
507      c123d]])
508  end)
509
510  it('numerical, skip past first 2 digits', function()
511    insert(text)
512    poke_eventloop()
513    command([[sort/\d\d/n]])
514    expect([[
515      abc
516      ab
517      a
518
519      a321
520      b321
521      c321d
522      b321
523      b321b
524      a122
525      b322b
526      a123
527      b123
528      c123d
529       123b]])
530  end)
531
532  it('hexadecimal, skip past first 2 digits', function()
533    insert(text)
534    poke_eventloop()
535    command([[sort/\d\d/x]])
536    expect([[
537      abc
538      ab
539      a
540
541      a321
542      b321
543      b321
544      a122
545      a123
546      b123
547      b321b
548      c321d
549      b322b
550       123b
551      c123d]])
552  end)
553
554  it('alpha, on first 2 digits', function()
555    insert(text)
556    poke_eventloop()
557    command([[sort/\d\d/r]])
558    expect([[
559      abc
560      ab
561      a
562
563      a123
564      a122
565      b123
566      c123d
567       123b
568      a321
569      b321
570      c321d
571      b322b
572      b321
573      b321b]])
574  end)
575
576  it('numeric, on first 2 digits', function()
577    insert(text)
578    poke_eventloop()
579    command([[sort/\d\d/rn]])
580    expect([[
581      abc
582      ab
583      a
584
585      a123
586      a122
587      b123
588      c123d
589       123b
590      a321
591      b321
592      c321d
593      b322b
594      b321
595      b321b]])
596  end)
597
598  it('hexadecimal, on first 2 digits', function()
599    insert(text)
600    poke_eventloop()
601    command([[sort/\d\d/rx]])
602    expect([[
603      abc
604      ab
605      a
606
607      a123
608      a122
609      b123
610      c123d
611       123b
612      a321
613      b321
614      c321d
615      b322b
616      b321
617      b321b]])
618  end)
619
620  it('fails with wrong arguments', function()
621    insert(text)
622    -- This should fail with "E474: Invalid argument".
623    eq('Vim(sort):E474: Invalid argument', pcall_err(command, 'sort no'))
624    expect(text)
625  end)
626
627  it('binary', function()
628    insert([[
629      0b111000
630      0b101100
631      0b101001
632      0b101001
633      0b101000
634      0b000000
635      0b001000
636      0b010000
637      0b101000
638      0b100000
639      0b101010
640      0b100010
641      0b100100
642      0b100010]])
643    poke_eventloop()
644    command([[sort b]])
645    expect([[
646      0b000000
647      0b001000
648      0b010000
649      0b100000
650      0b100010
651      0b100010
652      0b100100
653      0b101000
654      0b101000
655      0b101001
656      0b101001
657      0b101010
658      0b101100
659      0b111000]])
660  end)
661
662  it('binary with leading characters', function()
663    insert([[
664      0b100010
665      0b010000
666       0b101001
667      b0b101100
668      0b100010
669       0b100100
670      a0b001000
671      0b101000
672      0b101000
673      a0b101001
674      ab0b100000
675      0b101010
676      0b000000
677      b0b111000]])
678    poke_eventloop()
679    command([[sort b]])
680    expect([[
681      0b000000
682      a0b001000
683      0b010000
684      ab0b100000
685      0b100010
686      0b100010
687       0b100100
688      0b101000
689      0b101000
690       0b101001
691      a0b101001
692      0b101010
693      b0b101100
694      b0b111000]])
695  end)
696
697  it('float', function()
698    insert([[
699      1.234
700      0.88
701      123.456
702      1.15e-6
703      -1.1e3
704      -1.01e3]])
705    poke_eventloop()
706    command([[sort f]])
707    expect([[
708      -1.1e3
709      -1.01e3
710      1.15e-6
711      0.88
712      1.234
713      123.456]])
714  end)
715end)