123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551 |
- " Tests for the "sort()" function and for the ":sort" command.
- source check.vim
- func Compare1(a, b) abort
- call sort(range(3), 'Compare2')
- return a:a - a:b
- endfunc
- func Compare2(a, b) abort
- return a:a - a:b
- endfunc
- func Test_sort_strings()
- " numbers compared as strings
- call assert_equal([1, 2, 3], sort([3, 2, 1]))
- call assert_equal([13, 28, 3], sort([3, 28, 13]))
- call assert_equal(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'Œ', 'œ'],
- \ sort(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'Œ']))
- call assert_equal(['A', 'a', 'o', 'O', 'p', 'P', 'Ä', 'Ô', 'ä', 'ô', 'Œ', 'œ'],
- \ sort(['A', 'a', 'o', 'O', 'œ', 'Œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'i'))
- " This does not appear to work correctly on Mac.
- if !has('mac')
- if v:collate =~? '^\(en\|fr\)_ca.utf-\?8$'
- " with Canadian English capitals come before lower case.
- " 'Œ' is omitted because it can sort before or after 'œ'
- call assert_equal(['A', 'a', 'Ä', 'ä', 'O', 'o', 'Ô', 'ô', 'œ', 'P', 'p'],
- \ sort(['A', 'a', 'o', 'O', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
- elseif v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$'
- " With the following locales, the accentuated letters are ordered
- " similarly to the non-accentuated letters...
- call assert_equal(['a', 'A', 'ä', 'Ä', 'o', 'O', 'ô', 'Ô', 'œ', 'Œ', 'p', 'P'],
- \ sort(['A', 'a', 'o', 'O', 'œ', 'Œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
- elseif v:collate =~? '^sv.*utf-\?8$'
- " ... whereas with a Swedish locale, the accentuated letters are ordered
- " after Z.
- call assert_equal(['a', 'A', 'o', 'O', 'p', 'P', 'ä', 'Ä', 'œ', 'œ', 'ô', 'Ô'],
- \ sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l'))
- endif
- endif
- endfunc
- func Test_sort_null_string()
- " null strings are sorted as empty strings.
- call assert_equal(['', 'a', 'b'], sort(['b', v:_null_string, 'a']))
- endfunc
- func Test_sort_numeric()
- call assert_equal([1, 2, 3], sort([3, 2, 1], 'n'))
- call assert_equal([3, 13, 28], sort([13, 28, 3], 'n'))
- " strings are not sorted
- call assert_equal(['13', '28', '3'], sort(['13', '28', '3'], 'n'))
- endfunc
- func Test_sort_numbers()
- call assert_equal([3, 13, 28], sort([13, 28, 3], 'N'))
- call assert_equal(['3', '13', '28'], sort(['13', '28', '3'], 'N'))
- call assert_equal([3997, 4996], sort([4996, 3997], 'Compare1'))
- endfunc
- func Test_sort_float()
- CheckFeature float
- call assert_equal([0.28, 3, 13.5], sort([13.5, 0.28, 3], 'f'))
- endfunc
- func Test_sort_nested()
- " test ability to call sort() from a compare function
- call assert_equal([1, 3, 5], sort([3, 1, 5], 'Compare1'))
- endfunc
- func Test_sort_default()
- CheckFeature float
- " docs say omitted, empty or zero argument sorts on string representation.
- call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"]))
- call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], ''))
- call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 0))
- call assert_equal(['2', 'A', 'a', 'AA', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 1))
- call assert_fails('call sort([3.3, 1, "2"], 3)', "E474:")
- endfunc
- " Tests for the ":sort" command.
- func Test_sort_cmd()
- let tests = [
- \ {
- \ 'name' : 'Alphabetical sort',
- \ 'cmd' : '%sort',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ ' 123b',
- \ 'a',
- \ 'a122',
- \ 'a123',
- \ 'a321',
- \ 'ab',
- \ 'abc',
- \ 'b123',
- \ 'b321',
- \ 'b321',
- \ 'b321b',
- \ 'b322b',
- \ 'c123d',
- \ 'c321d'
- \ ]
- \ },
- \ {
- \ 'name' : 'Numeric sort',
- \ 'cmd' : '%sort n',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'a',
- \ 'x-22',
- \ 'b321',
- \ 'b123',
- \ '',
- \ 'c123d',
- \ '-24',
- \ ' 123b',
- \ 'c321d',
- \ '0',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '-24',
- \ 'x-22',
- \ '0',
- \ 'a122',
- \ 'a123',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b321',
- \ 'b321b',
- \ 'b322b'
- \ ]
- \ },
- \ {
- \ 'name' : 'Hexadecimal sort',
- \ 'cmd' : '%sort x',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ 'a',
- \ 'ab',
- \ 'abc',
- \ ' 123b',
- \ 'a122',
- \ 'a123',
- \ 'a321',
- \ 'b123',
- \ 'b321',
- \ 'b321',
- \ 'b321b',
- \ 'b322b',
- \ 'c123d',
- \ 'c321d'
- \ ]
- \ },
- \ {
- \ 'name' : 'Alphabetical unique sort',
- \ 'cmd' : '%sort u',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ ' 123b',
- \ 'a',
- \ 'a122',
- \ 'a123',
- \ 'a321',
- \ 'ab',
- \ 'abc',
- \ 'b123',
- \ 'b321',
- \ 'b321b',
- \ 'b322b',
- \ 'c123d',
- \ 'c321d'
- \ ]
- \ },
- \ {
- \ 'name' : 'Alphabetical reverse sort',
- \ 'cmd' : '%sort!',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ 'c321d',
- \ 'c123d',
- \ 'b322b',
- \ 'b321b',
- \ 'b321',
- \ 'b321',
- \ 'b123',
- \ 'abc',
- \ 'ab',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'a',
- \ ' 123b',
- \ ]
- \ },
- \ {
- \ 'name' : 'Numeric reverse sort',
- \ 'cmd' : '%sort! n',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ 'b322b',
- \ 'b321b',
- \ 'b321',
- \ 'c321d',
- \ 'b321',
- \ 'a321',
- \ ' 123b',
- \ 'c123d',
- \ 'b123',
- \ 'a123',
- \ 'a122',
- \ 'a',
- \ 'ab',
- \ 'abc'
- \ ]
- \ },
- \ {
- \ 'name' : 'Unique reverse sort',
- \ 'cmd' : 'sort! u',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ],
- \ 'expected' : [
- \ 'c321d',
- \ 'c123d',
- \ 'b322b',
- \ 'b321b',
- \ 'b321',
- \ 'b123',
- \ 'abc',
- \ 'ab',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'a',
- \ ' 123b',
- \ ]
- \ },
- \ {
- \ 'name' : 'Octal sort',
- \ 'cmd' : 'sort o',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a122',
- \ 'a123',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b321',
- \ 'b321b',
- \ 'b322b'
- \ ]
- \ },
- \ {
- \ 'name' : 'Reverse hexadecimal sort',
- \ 'cmd' : 'sort! x',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'c321d',
- \ 'c123d',
- \ 'b322b',
- \ 'b321b',
- \ 'b321',
- \ 'b321',
- \ 'b123',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ ' 123b',
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ ''
- \ ]
- \ },
- \ {
- \ 'name' : 'Alpha (skip first character) sort',
- \ 'cmd' : 'sort/./',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'a',
- \ '',
- \ '',
- \ 'a122',
- \ 'a123',
- \ 'b123',
- \ ' 123b',
- \ 'c123d',
- \ 'a321',
- \ 'b321',
- \ 'b321',
- \ 'b321b',
- \ 'c321d',
- \ 'b322b',
- \ 'ab',
- \ 'abc'
- \ ]
- \ },
- \ {
- \ 'name' : 'Alpha (skip first 2 characters) sort',
- \ 'cmd' : 'sort/../',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'b321',
- \ 'b321b',
- \ 'c321d',
- \ 'a122',
- \ 'b322b',
- \ 'a123',
- \ 'b123',
- \ ' 123b',
- \ 'c123d',
- \ 'abc'
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, unique, skip first 2 characters',
- \ 'cmd' : 'sort/../u',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'ab',
- \ 'a',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'b321b',
- \ 'c321d',
- \ 'a122',
- \ 'b322b',
- \ 'a123',
- \ 'b123',
- \ ' 123b',
- \ 'c123d',
- \ 'abc'
- \ ]
- \ },
- \ {
- \ 'name' : 'numeric, skip first character',
- \ 'cmd' : 'sort/./n',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a122',
- \ 'a123',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b321',
- \ 'b321b',
- \ 'b322b'
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, sort on first character',
- \ 'cmd' : 'sort/./r',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ '',
- \ '',
- \ ' 123b',
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ 'c123d',
- \ 'c321d'
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, sort on first 2 characters',
- \ 'cmd' : 'sort/../r',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'a',
- \ '',
- \ '',
- \ ' 123b',
- \ 'a123',
- \ 'a122',
- \ 'a321',
- \ 'abc',
- \ 'ab',
- \ 'b123',
- \ 'b321',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ 'c123d',
- \ 'c321d'
- \ ]
- \ },
- \ {
- \ 'name' : 'numeric, sort on first character',
- \ 'cmd' : 'sort/./rn',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, skip past first digit',
- \ 'cmd' : 'sort/\d/',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'b321',
- \ 'b321b',
- \ 'c321d',
- \ 'a122',
- \ 'b322b',
- \ 'a123',
- \ 'b123',
- \ ' 123b',
- \ 'c123d'
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, sort on first digit',
- \ 'cmd' : 'sort/\d/r',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a123',
- \ 'a122',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ]
- \ },
- \ {
- \ 'name' : 'numeric, skip past first digit',
- \ 'cmd' : 'sort/\d/n',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b321',
- \ 'b321b',
- \ 'a122',
- \ 'b322b',
- \ 'a123',
- \ 'b123',
- \ 'c123d',
- \ ' 123b'
- \ ]
- \ },
- \ {
- \ 'name' : 'numeric, sort on first digit',
- \ 'cmd' : 'sort/\d/rn',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a123',
- \ 'a122',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, skip past first 2 digits',
- \ 'cmd' : 'sort/\d\d/',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'b321',
- \ 'b321b',
- \ 'c321d',
- \ 'a122',
- \ 'b322b',
- \ 'a123',
- \ 'b123',
- \ ' 123b',
- \ 'c123d'
- \ ]
- \ },
- \ {
- \ 'name' : 'numeric, skip past first 2 digits',
- \ 'cmd' : 'sort/\d\d/n',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b321',
- \ 'b321b',
- \ 'a122',
- \ 'b322b',
- \ 'a123',
- \ 'b123',
- \ 'c123d',
- \ ' 123b'
- \ ]
- \ },
- \ {
- \ 'name' : 'hexadecimal, skip past first 2 digits',
- \ 'cmd' : 'sort/\d\d/x',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a321',
- \ 'b321',
- \ 'b321',
- \ 'a122',
- \ 'a123',
- \ 'b123',
- \ 'b321b',
- \ 'c321d',
- \ 'b322b',
- \ ' 123b',
- \ 'c123d'
- \ ]
- \ },
- \ {
- \ 'name' : 'alpha, sort on first 2 digits',
- \ 'cmd' : 'sort/\d\d/r',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a123',
- \ 'a122',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ]
- \ },
- \ {
- \ 'name' : 'numeric, sort on first 2 digits',
- \ 'cmd' : 'sort/\d\d/rn',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a123',
- \ 'a122',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ]
- \ },
- \ {
- \ 'name' : 'hexadecimal, sort on first 2 digits',
- \ 'cmd' : 'sort/\d\d/rx',
- \ 'input' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ 'a321',
- \ 'a123',
- \ 'a122',
- \ 'b321',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ 'abc',
- \ 'ab',
- \ 'a',
- \ '',
- \ '',
- \ 'a123',
- \ 'a122',
- \ 'b123',
- \ 'c123d',
- \ ' 123b',
- \ 'a321',
- \ 'b321',
- \ 'c321d',
- \ 'b322b',
- \ 'b321',
- \ 'b321b'
- \ ]
- \ },
- \ {
- \ 'name' : 'binary',
- \ 'cmd' : 'sort b',
- \ 'input' : [
- \ '0b111000',
- \ '0b101100',
- \ '0b101001',
- \ '0b101001',
- \ '0b101000',
- \ '0b000000',
- \ '0b001000',
- \ '0b010000',
- \ '0b101000',
- \ '0b100000',
- \ '0b101010',
- \ '0b100010',
- \ '0b100100',
- \ '0b100010',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ '',
- \ '',
- \ '0b000000',
- \ '0b001000',
- \ '0b010000',
- \ '0b100000',
- \ '0b100010',
- \ '0b100010',
- \ '0b100100',
- \ '0b101000',
- \ '0b101000',
- \ '0b101001',
- \ '0b101001',
- \ '0b101010',
- \ '0b101100',
- \ '0b111000'
- \ ]
- \ },
- \ {
- \ 'name' : 'binary with leading characters',
- \ 'cmd' : 'sort b',
- \ 'input' : [
- \ '0b100010',
- \ '0b010000',
- \ ' 0b101001',
- \ 'b0b101100',
- \ '0b100010',
- \ ' 0b100100',
- \ 'a0b001000',
- \ '0b101000',
- \ '0b101000',
- \ 'a0b101001',
- \ 'ab0b100000',
- \ '0b101010',
- \ '0b000000',
- \ 'b0b111000',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ '',
- \ '',
- \ '0b000000',
- \ 'a0b001000',
- \ '0b010000',
- \ 'ab0b100000',
- \ '0b100010',
- \ '0b100010',
- \ ' 0b100100',
- \ '0b101000',
- \ '0b101000',
- \ ' 0b101001',
- \ 'a0b101001',
- \ '0b101010',
- \ 'b0b101100',
- \ 'b0b111000'
- \ ]
- \ },
- \ {
- \ 'name' : 'alphabetical, sorted input',
- \ 'cmd' : 'sort',
- \ 'input' : [
- \ 'a',
- \ 'b',
- \ 'c',
- \ ],
- \ 'expected' : [
- \ 'a',
- \ 'b',
- \ 'c',
- \ ]
- \ },
- \ {
- \ 'name' : 'alphabetical, sorted input, unique at end',
- \ 'cmd' : 'sort u',
- \ 'input' : [
- \ 'aa',
- \ 'bb',
- \ 'cc',
- \ 'cc',
- \ ],
- \ 'expected' : [
- \ 'aa',
- \ 'bb',
- \ 'cc',
- \ ]
- \ },
- \ {
- \ 'name' : 'sort one line buffer',
- \ 'cmd' : 'sort',
- \ 'input' : [
- \ 'single line'
- \ ],
- \ 'expected' : [
- \ 'single line'
- \ ]
- \ },
- \ {
- \ 'name' : 'sort ignoring case',
- \ 'cmd' : '%sort i',
- \ 'input' : [
- \ 'BB',
- \ 'Cc',
- \ 'aa'
- \ ],
- \ 'expected' : [
- \ 'aa',
- \ 'BB',
- \ 'Cc'
- \ ]
- \ },
- \ ]
- " This does not appear to work correctly on Mac.
- if !has('mac')
- if v:collate =~? '^\(en\|fr\)_ca.utf-\?8$'
- " en_CA.utf-8 sorts capitals before lower case
- " 'Œ' is omitted because it can sort before or after 'œ'
- let tests += [
- \ {
- \ 'name' : 'sort with locale ' .. v:collate,
- \ 'cmd' : '%sort l',
- \ 'input' : [
- \ 'A',
- \ 'E',
- \ 'O',
- \ 'À',
- \ 'È',
- \ 'É',
- \ 'Ô',
- \ 'Z',
- \ 'a',
- \ 'e',
- \ 'o',
- \ 'à',
- \ 'è',
- \ 'é',
- \ 'ô',
- \ 'œ',
- \ 'z'
- \ ],
- \ 'expected' : [
- \ 'A',
- \ 'a',
- \ 'À',
- \ 'à',
- \ 'E',
- \ 'e',
- \ 'É',
- \ 'é',
- \ 'È',
- \ 'è',
- \ 'O',
- \ 'o',
- \ 'Ô',
- \ 'ô',
- \ 'œ',
- \ 'Z',
- \ 'z'
- \ ]
- \ },
- \ ]
- elseif v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$'
- " With these locales, the accentuated letters are ordered
- " similarly to the non-accentuated letters.
- let tests += [
- \ {
- \ 'name' : 'sort with locale ' .. v:collate,
- \ 'cmd' : '%sort l',
- \ 'input' : [
- \ 'A',
- \ 'E',
- \ 'O',
- \ 'À',
- \ 'È',
- \ 'É',
- \ 'Ô',
- \ 'Œ',
- \ 'Z',
- \ 'a',
- \ 'e',
- \ 'o',
- \ 'à',
- \ 'è',
- \ 'é',
- \ 'ô',
- \ 'œ',
- \ 'z'
- \ ],
- \ 'expected' : [
- \ 'a',
- \ 'A',
- \ 'à',
- \ 'À',
- \ 'e',
- \ 'E',
- \ 'é',
- \ 'É',
- \ 'è',
- \ 'È',
- \ 'o',
- \ 'O',
- \ 'ô',
- \ 'Ô',
- \ 'œ',
- \ 'Œ',
- \ 'z',
- \ 'Z'
- \ ]
- \ },
- \ ]
- endif
- endif
- if has('float')
- let tests += [
- \ {
- \ 'name' : 'float',
- \ 'cmd' : 'sort f',
- \ 'input' : [
- \ '1.234',
- \ '0.88',
- \ ' + 123.456',
- \ '1.15e-6',
- \ '-1.1e3',
- \ '-1.01e3',
- \ '',
- \ ''
- \ ],
- \ 'expected' : [
- \ '',
- \ '',
- \ '-1.1e3',
- \ '-1.01e3',
- \ '1.15e-6',
- \ '0.88',
- \ '1.234',
- \ ' + 123.456'
- \ ]
- \ },
- \ ]
- endif
- for t in tests
- enew!
- call append(0, t.input)
- $delete _
- setlocal nomodified
- execute t.cmd
- call assert_equal(t.expected, getline(1, '$'), t.name)
- " Previously, the ":sort" command would set 'modified' even if the buffer
- " contents did not change. Here, we check that this problem is fixed.
- if t.input == t.expected
- call assert_false(&modified, t.name . ': &mod is not correct')
- else
- call assert_true(&modified, t.name . ': &mod is not correct')
- endif
- endfor
- " Needs at least two lines for this test
- call setline(1, ['line1', 'line2'])
- call assert_fails('sort no', 'E474:')
- call assert_fails('sort c', 'E475:')
- call assert_fails('sort #pat%', 'E654:')
- call assert_fails('sort /\%(/', 'E53:')
- enew!
- endfunc
- func Test_sort_large_num()
- new
- a
- -2147483648
- -2147483647
- -1
- 0
- 1
- -2147483646
- 2147483646
- 2147483647
- 2147483647
- -2147483648
- abc
- .
- " Numerical sort. Non-numeric lines are ordered before numerical lines.
- " Ordering of non-numerical is stable.
- sort n
- call assert_equal(['',
- \ 'abc',
- \ '',
- \ '-2147483648',
- \ '-2147483648',
- \ '-2147483647',
- \ '-2147483646',
- \ '-1',
- \ '0',
- \ '1',
- \ '2147483646',
- \ '2147483647',
- \ '2147483647'], getline(1, '$'))
- bwipe!
- new
- a
- -9223372036854775808
- -9223372036854775807
- -1
- 0
- 1
- -9223372036854775806
- 9223372036854775806
- 9223372036854775807
- 9223372036854775807
- -9223372036854775808
- abc
- .
- sort n
- call assert_equal(['',
- \ 'abc',
- \ '',
- \ '-9223372036854775808',
- \ '-9223372036854775808',
- \ '-9223372036854775807',
- \ '-9223372036854775806',
- \ '-1',
- \ '0',
- \ '1',
- \ '9223372036854775806',
- \ '9223372036854775807',
- \ '9223372036854775807'], getline(1, '$'))
- bwipe!
- endfunc
- func Test_sort_cmd_report()
- enew!
- call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
- $delete _
- setlocal nomodified
- let res = execute('%sort u')
- call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
- call assert_match("6 fewer lines", res)
- enew!
- call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
- $delete _
- setlocal nomodified report=10
- let res = execute('%sort u')
- call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
- call assert_equal("", res)
- enew!
- call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3))
- $delete _
- setl report&vim
- setlocal nomodified
- let res = execute('1g/^/%sort u')
- call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0'))
- " the output comes from the :g command, not from the :sort
- call assert_match("6 fewer lines", res)
- enew!
- endfunc
- " Test for a :sort command followed by another command
- func Test_sort_followed_by_cmd()
- new
- let var = ''
- call setline(1, ['cc', 'aa', 'bb'])
- %sort | let var = "sortcmdtest"
- call assert_equal(var, "sortcmdtest")
- call assert_equal(['aa', 'bb', 'cc'], getline(1, '$'))
- " Test for :sort followed by a comment
- call setline(1, ['3b', '1c', '2a'])
- %sort /\d\+/ " sort alphabetically
- call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
- close!
- endfunc
- " Test for :sort using last search pattern
- func Test_sort_last_search_pat()
- new
- let @/ = '\d\+'
- call setline(1, ['3b', '1c', '2a'])
- sort //
- call assert_equal(['2a', '3b', '1c'], getline(1, '$'))
- close!
- endfunc
- " Test for :sort with no last search pattern
- func Test_sort_with_no_last_search_pat()
- let lines =<< trim [SCRIPT]
- call setline(1, ['3b', '1c', '2a'])
- call assert_fails('sort //', 'E35:')
- call writefile(v:errors, 'Xresult')
- qall!
- [SCRIPT]
- call writefile(lines, 'Xscript', 'D')
- if RunVim([], [], '--clean -S Xscript')
- call assert_equal([], readfile('Xresult'))
- endif
- call delete('Xresult')
- endfunc
- " Test for retaining marks across a :sort
- func Test_sort_with_marks()
- new
- call setline(1, ['cc', 'aa', 'bb'])
- call setpos("'c", [0, 1, 0, 0])
- call setpos("'a", [0, 2, 0, 0])
- call setpos("'b", [0, 3, 0, 0])
- %sort
- call assert_equal(['aa', 'bb', 'cc'], getline(1, '$'))
- call assert_equal(2, line("'a"))
- call assert_equal(3, line("'b"))
- call assert_equal(1, line("'c"))
- close!
- endfunc
- " vim: shiftwidth=2 sts=2 expandtab
|