robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
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 = 9n . insert , n . command , n . clear , n . expect , t . eq , n . poke_eventloop 10 11describe ( ':sort' , function () 12local text =[[ 13abc 14ab 15a 16a321 17a123 18a122 19b321 20b123 21c123d 22123b 23c321d 24b322b 25b321 26b321b 27]] 28before_each ( clear ) 29 30it ( 'alphabetical' , function () 31insert ( text ) 32poke_eventloop () 33command ( 'sort' ) 34expect ( [[ 35 36123b 37a 38a122 39a123 40a321 41ab 42abc 43b123 44b321 45b321 46b321b 47b322b 48c123d 49c321d]] ) 50end ) 51 52it ( 'numerical' , function () 53insert ( [[ 54abc 55ab 56a321 57a123 58a122 59a 60x-22 61b321 62b123 63c123d 64-24 65123b 66c321d 670 68b322b 69b321 70b321b 71]] ) 72poke_eventloop () 73command ( 'sort n' ) 74expect ( [[ 75abc 76ab 77a 78 79-24 80x-22 810 82a122 83a123 84b123 85c123d 86123b 87a321 88b321 89c321d 90b321 91b321b 92b322b]] ) 93end ) 94 95it ( 'hexadecimal' , function () 96insert ( text ) 97poke_eventloop () 98command ( 'sort x' ) 99expect ( [[ 100 101a 102ab 103abc 104123b 105a122 106a123 107a321 108b123 109b321 110b321 111b321b 112b322b 113c123d 114c321d]] ) 115end ) 116 117it ( 'alphabetical, unique' , function () 118insert ( text ) 119poke_eventloop () 120command ( 'sort u' ) 121expect ( [[ 122 123123b 124a 125a122 126a123 127a321 128ab 129abc 130b123 131b321 132b321b 133b322b 134c123d 135c321d]] ) 136end ) 137 138it ( 'alphabetical, reverse' , function () 139insert ( text ) 140poke_eventloop () 141command ( 'sort!' ) 142expect ( [[ 143c321d 144c123d 145b322b 146b321b 147b321 148b321 149b123 150abc 151ab 152a321 153a123 154a122 155a 156123b 157]] ) 158end ) 159 160it ( 'numerical, reverse' , function () 161insert ( text ) 162poke_eventloop () 163command ( 'sort! n' ) 164expect ( [[ 165b322b 166b321b 167b321 168c321d 169b321 170a321 171123b 172c123d 173b123 174a123 175a122 176 177a 178ab 179abc]] ) 180end ) 181 182it ( 'unique, reverse' , function () 183insert ( text ) 184poke_eventloop () 185command ( 'sort! u' ) 186expect ( [[ 187c321d 188c123d 189b322b 190b321b 191b321 192b123 193abc 194ab 195a321 196a123 197a122 198a 199123b 200]] ) 201end ) 202 203it ( 'octal' , function () 204insert ( text ) 205poke_eventloop () 206command ( 'sort o' ) 207expect ( [[ 208abc 209ab 210a 211 212a122 213a123 214b123 215c123d 216123b 217a321 218b321 219c321d 220b321 221b321b 222b322b]] ) 223end ) 224 225it ( 'reverse, hexadecimal' , function () 226insert ( text ) 227poke_eventloop () 228command ( 'sort! x' ) 229expect ( [[ 230c321d 231c123d 232b322b 233b321b 234b321 235b321 236b123 237a321 238a123 239a122 240123b 241abc 242ab 243a 244]] ) 245end ) 246 247it ( 'alphabetical, skip first character' , function () 248insert ( text ) 249poke_eventloop () 250command ( 'sort/./' ) 251expect ( [[ 252a 253 254a122 255a123 256b123 257123b 258c123d 259a321 260b321 261b321 262b321b 263c321d 264b322b 265ab 266abc]] ) 267end ) 268 269it ( 'alphabetical, skip first 2 characters' , function () 270insert ( text ) 271poke_eventloop () 272command ( 'sort/../' ) 273expect ( [[ 274ab 275a 276 277a321 278b321 279b321 280b321b 281c321d 282a122 283b322b 284a123 285b123 286123b 287c123d 288abc]] ) 289end ) 290 291it ( 'alphabetical, unique, skip first 2 characters' , function () 292insert ( text ) 293poke_eventloop () 294command ( 'sort/../u' ) 295expect ( [[ 296ab 297a 298 299a321 300b321 301b321b 302c321d 303a122 304b322b 305a123 306b123 307123b 308c123d 309abc]] ) 310end ) 311 312it ( 'numerical, skip first character' , function () 313insert ( text ) 314poke_eventloop () 315command ( 'sort/./n' ) 316expect ( [[ 317abc 318ab 319a 320 321a122 322a123 323b123 324c123d 325123b 326a321 327b321 328c321d 329b321 330b321b 331b322b]] ) 332end ) 333 334it ( 'alphabetical, sort on first character' , function () 335insert ( text ) 336poke_eventloop () 337command ( 'sort/./r' ) 338expect ( [[ 339 340123b 341abc 342ab 343a 344a321 345a123 346a122 347b321 348b123 349b322b 350b321 351b321b 352c123d 353c321d]] ) 354end ) 355 356it ( 'alphabetical, sort on first 2 characters' , function () 357insert ( text ) 358poke_eventloop () 359command ( 'sort/../r' ) 360expect ( [[ 361a 362 363123b 364a123 365a122 366a321 367abc 368ab 369b123 370b321 371b322b 372b321 373b321b 374c123d 375c321d]] ) 376end ) 377 378it ( 'numerical, sort on first character' , function () 379insert ( text ) 380poke_eventloop () 381command ( 'sort/./rn' ) 382expect ( [[ 383abc 384ab 385a 386a321 387a123 388a122 389b321 390b123 391c123d 392123b 393c321d 394b322b 395b321 396b321b 397]] ) 398end ) 399 400it ( 'alphabetical, skip past first digit' , function () 401insert ( text ) 402poke_eventloop () 403command ( [[sort/\d/]] ) 404expect ( [[ 405abc 406ab 407a 408 409a321 410b321 411b321 412b321b 413c321d 414a122 415b322b 416a123 417b123 418123b 419c123d]] ) 420end ) 421 422it ( 'alphabetical, sort on first digit' , function () 423insert ( text ) 424poke_eventloop () 425command ( [[sort/\d/r]] ) 426expect ( [[ 427abc 428ab 429a 430 431a123 432a122 433b123 434c123d 435123b 436a321 437b321 438c321d 439b322b 440b321 441b321b]] ) 442end ) 443 444it ( 'numerical, skip past first digit' , function () 445insert ( text ) 446poke_eventloop () 447command ( [[sort/\d/n]] ) 448expect ( [[ 449abc 450ab 451a 452 453a321 454b321 455c321d 456b321 457b321b 458a122 459b322b 460a123 461b123 462c123d 463123b]] ) 464end ) 465 466it ( 'numerical, sort on first digit' , function () 467insert ( text ) 468poke_eventloop () 469command ( [[sort/\d/rn]] ) 470expect ( [[ 471abc 472ab 473a 474 475a123 476a122 477b123 478c123d 479123b 480a321 481b321 482c321d 483b322b 484b321 485b321b]] ) 486end ) 487 488it ( 'alphabetical, skip past first 2 digits' , function () 489insert ( text ) 490poke_eventloop () 491command ( [[sort/\d\d/]] ) 492expect ( [[ 493abc 494ab 495a 496 497a321 498b321 499b321 500b321b 501c321d 502a122 503b322b 504a123 505b123 506123b 507c123d]] ) 508end ) 509 510it ( 'numerical, skip past first 2 digits' , function () 511insert ( text ) 512poke_eventloop () 513command ( [[sort/\d\d/n]] ) 514expect ( [[ 515abc 516ab 517a 518 519a321 520b321 521c321d 522b321 523b321b 524a122 525b322b 526a123 527b123 528c123d 529123b]] ) 530end ) 531 532it ( 'hexadecimal, skip past first 2 digits' , function () 533insert ( text ) 534poke_eventloop () 535command ( [[sort/\d\d/x]] ) 536expect ( [[ 537abc 538ab 539a 540 541a321 542b321 543b321 544a122 545a123 546b123 547b321b 548c321d 549b322b 550123b 551c123d]] ) 552end ) 553 554it ( 'alpha, on first 2 digits' , function () 555insert ( text ) 556poke_eventloop () 557command ( [[sort/\d\d/r]] ) 558expect ( [[ 559abc 560ab 561a 562 563a123 564a122 565b123 566c123d 567123b 568a321 569b321 570c321d 571b322b 572b321 573b321b]] ) 574end ) 575 576it ( 'numeric, on first 2 digits' , function () 577insert ( text ) 578poke_eventloop () 579command ( [[sort/\d\d/rn]] ) 580expect ( [[ 581abc 582ab 583a 584 585a123 586a122 587b123 588c123d 589123b 590a321 591b321 592c321d 593b322b 594b321 595b321b]] ) 596end ) 597 598it ( 'hexadecimal, on first 2 digits' , function () 599insert ( text ) 600poke_eventloop () 601command ( [[sort/\d\d/rx]] ) 602expect ( [[ 603abc 604ab 605a 606 607a123 608a122 609b123 610c123d 611123b 612a321 613b321 614c321d 615b322b 616b321 617b321b]] ) 618end ) 619 620it ( 'fails with wrong arguments' , function () 621insert ( text ) 622-- This should fail with "E474: Invalid argument". 623eq ( 'Vim(sort):E474: Invalid argument' , pcall_err ( command , 'sort no' )) 624expect ( text ) 625end ) 626 627it ( 'binary' , function () 628insert ( [[ 6290b111000 6300b101100 6310b101001 6320b101001 6330b101000 6340b000000 6350b001000 6360b010000 6370b101000 6380b100000 6390b101010 6400b100010 6410b100100 6420b100010]] ) 643poke_eventloop () 644command ( [[sort b]] ) 645expect ( [[ 6460b000000 6470b001000 6480b010000 6490b100000 6500b100010 6510b100010 6520b100100 6530b101000 6540b101000 6550b101001 6560b101001 6570b101010 6580b101100 6590b111000]] ) 660end ) 661 662it ( 'binary with leading characters' , function () 663insert ( [[ 6640b100010 6650b010000 6660b101001 667b0b101100 6680b100010 6690b100100 670a0b001000 6710b101000 6720b101000 673a0b101001 674ab0b100000 6750b101010 6760b000000 677b0b111000]] ) 678poke_eventloop () 679command ( [[sort b]] ) 680expect ( [[ 6810b000000 682a0b001000 6830b010000 684ab0b100000 6850b100010 6860b100010 6870b100100 6880b101000 6890b101000 6900b101001 691a0b101001 6920b101010 693b0b101100 694b0b111000]] ) 695end ) 696 697it ( 'float' , function () 698insert ( [[ 6991.234 7000.88 701123.456 7021.15e-6 703-1.1e3 704-1.01e3]] ) 705poke_eventloop () 706command ( [[sort f]] ) 707expect ( [[ 708-1.1e3 709-1.01e3 7101.15e-6 7110.88 7121.234 713123.456]] ) 714end ) 715end )