neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

057_sort_spec.lua (9783B)


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