1" Tests for the "sort()" function and for the ":sort" command. 2 3source check.vim 4 5func Compare1(a, b) abort 6 call sort(range(3), 'Compare2') 7 return a:a - a:b 8endfunc 9 10func Compare2(a, b) abort 11 return a:a - a:b 12endfunc 13 14func Test_sort_strings() 15 " numbers compared as strings 16 call assert_equal([1, 2, 3], sort([3, 2, 1])) 17 call assert_equal([13, 28, 3], sort([3, 28, 13])) 18 19 call assert_equal(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'Œ', 'œ'], 20 \ sort(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'Œ'])) 21 22 call assert_equal(['A', 'a', 'o', 'O', 'p', 'P', 'Ä', 'Ô', 'ä', 'ô', 'Œ', 'œ'], 23 \ sort(['A', 'a', 'o', 'O', 'œ', 'Œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'i')) 24 25 " This does not appear to work correctly on Mac. 26 if !has('mac') 27 if v:collate =~? '^\(en\|fr\)_ca.utf-\?8$' 28 " with Canadian English capitals come before lower case. 29 " 'Œ' is omitted because it can sort before or after 'œ' 30 call assert_equal(['A', 'a', 'Ä', 'ä', 'O', 'o', 'Ô', 'ô', 'œ', 'P', 'p'], 31 \ sort(['A', 'a', 'o', 'O', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l')) 32 elseif v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$' 33 " With the following locales, the accentuated letters are ordered 34 " similarly to the non-accentuated letters... 35 call assert_equal(['a', 'A', 'ä', 'Ä', 'o', 'O', 'ô', 'Ô', 'œ', 'Œ', 'p', 'P'], 36 \ sort(['A', 'a', 'o', 'O', 'œ', 'Œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l')) 37 elseif v:collate =~? '^sv.*utf-\?8$' 38 " ... whereas with a Swedish locale, the accentuated letters are ordered 39 " after Z. 40 call assert_equal(['a', 'A', 'o', 'O', 'p', 'P', 'ä', 'Ä', 'œ', 'œ', 'ô', 'Ô'], 41 \ sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l')) 42 endif 43 endif 44endfunc 45 46func Test_sort_null_string() 47 " null strings are sorted as empty strings. 48 call assert_equal(['', 'a', 'b'], sort(['b', test_null_string(), 'a'])) 49endfunc 50 51func Test_sort_numeric() 52 call assert_equal([1, 2, 3], sort([3, 2, 1], 'n')) 53 call assert_equal([3, 13, 28], sort([13, 28, 3], 'n')) 54 " strings are not sorted 55 call assert_equal(['13', '28', '3'], sort(['13', '28', '3'], 'n')) 56endfunc 57 58func Test_sort_numbers() 59 call assert_equal([3, 13, 28], sort([13, 28, 3], 'N')) 60 call assert_equal(['3', '13', '28'], sort(['13', '28', '3'], 'N')) 61 call assert_equal([3997, 4996], sort([4996, 3997], 'Compare1')) 62endfunc 63 64func Test_sort_float() 65 CheckFeature float 66 call assert_equal([0.28, 3, 13.5], sort([13.5, 0.28, 3], 'f')) 67endfunc 68 69func Test_sort_nested() 70 " test ability to call sort() from a compare function 71 call assert_equal([1, 3, 5], sort([3, 1, 5], 'Compare1')) 72endfunc 73 74func Test_sort_default() 75 CheckFeature float 76 77 " docs say omitted, empty or zero argument sorts on string representation. 78 call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"])) 79 call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], '')) 80 call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 0)) 81 call assert_equal(['2', 'A', 'a', 'AA', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 1)) 82 call assert_fails('call sort([3.3, 1, "2"], 3)', "E474:") 83endfunc 84 85" Tests for the ":sort" command. 86func Test_sort_cmd() 87 let tests = [ 88 \ { 89 \ 'name' : 'Alphabetical sort', 90 \ 'cmd' : '%sort', 91 \ 'input' : [ 92 \ 'abc', 93 \ 'ab', 94 \ 'a', 95 \ 'a321', 96 \ 'a123', 97 \ 'a122', 98 \ 'b321', 99 \ 'b123', 100 \ 'c123d', 101 \ ' 123b', 102 \ 'c321d', 103 \ 'b322b', 104 \ 'b321', 105 \ 'b321b' 106 \ ], 107 \ 'expected' : [ 108 \ ' 123b', 109 \ 'a', 110 \ 'a122', 111 \ 'a123', 112 \ 'a321', 113 \ 'ab', 114 \ 'abc', 115 \ 'b123', 116 \ 'b321', 117 \ 'b321', 118 \ 'b321b', 119 \ 'b322b', 120 \ 'c123d', 121 \ 'c321d' 122 \ ] 123 \ }, 124 \ { 125 \ 'name' : 'Numeric sort', 126 \ 'cmd' : '%sort n', 127 \ 'input' : [ 128 \ 'abc', 129 \ 'ab', 130 \ 'a321', 131 \ 'a123', 132 \ 'a122', 133 \ 'a', 134 \ 'x-22', 135 \ 'b321', 136 \ 'b123', 137 \ '', 138 \ 'c123d', 139 \ '-24', 140 \ ' 123b', 141 \ 'c321d', 142 \ '0', 143 \ 'b322b', 144 \ 'b321', 145 \ 'b321b' 146 \ ], 147 \ 'expected' : [ 148 \ 'abc', 149 \ 'ab', 150 \ 'a', 151 \ '', 152 \ '-24', 153 \ 'x-22', 154 \ '0', 155 \ 'a122', 156 \ 'a123', 157 \ 'b123', 158 \ 'c123d', 159 \ ' 123b', 160 \ 'a321', 161 \ 'b321', 162 \ 'c321d', 163 \ 'b321', 164 \ 'b321b', 165 \ 'b322b' 166 \ ] 167 \ }, 168 \ { 169 \ 'name' : 'Hexadecimal sort', 170 \ 'cmd' : '%sort x', 171 \ 'input' : [ 172 \ 'abc', 173 \ 'ab', 174 \ 'a', 175 \ 'a321', 176 \ 'a123', 177 \ 'a122', 178 \ 'b321', 179 \ 'b123', 180 \ 'c123d', 181 \ ' 123b', 182 \ 'c321d', 183 \ 'b322b', 184 \ 'b321', 185 \ 'b321b' 186 \ ], 187 \ 'expected' : [ 188 \ 'a', 189 \ 'ab', 190 \ 'abc', 191 \ ' 123b', 192 \ 'a122', 193 \ 'a123', 194 \ 'a321', 195 \ 'b123', 196 \ 'b321', 197 \ 'b321', 198 \ 'b321b', 199 \ 'b322b', 200 \ 'c123d', 201 \ 'c321d' 202 \ ] 203 \ }, 204 \ { 205 \ 'name' : 'Alphabetical unique sort', 206 \ 'cmd' : '%sort u', 207 \ 'input' : [ 208 \ 'abc', 209 \ 'ab', 210 \ 'a', 211 \ 'a321', 212 \ 'a123', 213 \ 'a122', 214 \ 'b321', 215 \ 'b123', 216 \ 'c123d', 217 \ ' 123b', 218 \ 'c321d', 219 \ 'b322b', 220 \ 'b321', 221 \ 'b321b' 222 \ ], 223 \ 'expected' : [ 224 \ ' 123b', 225 \ 'a', 226 \ 'a122', 227 \ 'a123', 228 \ 'a321', 229 \ 'ab', 230 \ 'abc', 231 \ 'b123', 232 \ 'b321', 233 \ 'b321b', 234 \ 'b322b', 235 \ 'c123d', 236 \ 'c321d' 237 \ ] 238 \ }, 239 \ { 240 \ 'name' : 'Alphabetical reverse sort', 241 \ 'cmd' : '%sort!', 242 \ 'input' : [ 243 \ 'abc', 244 \ 'ab', 245 \ 'a', 246 \ 'a321', 247 \ 'a123', 248 \ 'a122', 249 \ 'b321', 250 \ 'b123', 251 \ 'c123d', 252 \ ' 123b', 253 \ 'c321d', 254 \ 'b322b', 255 \ 'b321', 256 \ 'b321b' 257 \ ], 258 \ 'expected' : [ 259 \ 'c321d', 260 \ 'c123d', 261 \ 'b322b', 262 \ 'b321b', 263 \ 'b321', 264 \ 'b321', 265 \ 'b123', 266 \ 'abc', 267 \ 'ab', 268 \ 'a321', 269 \ 'a123', 270 \ 'a122', 271 \ 'a', 272 \ ' 123b', 273 \ ] 274 \ }, 275 \ { 276 \ 'name' : 'Numeric reverse sort', 277 \ 'cmd' : '%sort! n', 278 \ 'input' : [ 279 \ 'abc', 280 \ 'ab', 281 \ 'a', 282 \ 'a321', 283 \ 'a123', 284 \ 'a122', 285 \ 'b321', 286 \ 'b123', 287 \ 'c123d', 288 \ ' 123b', 289 \ 'c321d', 290 \ 'b322b', 291 \ 'b321', 292 \ 'b321b' 293 \ ], 294 \ 'expected' : [ 295 \ 'b322b', 296 \ 'b321b', 297 \ 'b321', 298 \ 'c321d', 299 \ 'b321', 300 \ 'a321', 301 \ ' 123b', 302 \ 'c123d', 303 \ 'b123', 304 \ 'a123', 305 \ 'a122', 306 \ 'a', 307 \ 'ab', 308 \ 'abc' 309 \ ] 310 \ }, 311 \ { 312 \ 'name' : 'Unique reverse sort', 313 \ 'cmd' : 'sort! u', 314 \ 'input' : [ 315 \ 'abc', 316 \ 'ab', 317 \ 'a', 318 \ 'a321', 319 \ 'a123', 320 \ 'a122', 321 \ 'b321', 322 \ 'b123', 323 \ 'c123d', 324 \ ' 123b', 325 \ 'c321d', 326 \ 'b322b', 327 \ 'b321', 328 \ 'b321b' 329 \ ], 330 \ 'expected' : [ 331 \ 'c321d', 332 \ 'c123d', 333 \ 'b322b', 334 \ 'b321b', 335 \ 'b321', 336 \ 'b123', 337 \ 'abc', 338 \ 'ab', 339 \ 'a321', 340 \ 'a123', 341 \ 'a122', 342 \ 'a', 343 \ ' 123b', 344 \ ] 345 \ }, 346 \ { 347 \ 'name' : 'Octal sort', 348 \ 'cmd' : 'sort o', 349 \ 'input' : [ 350 \ 'abc', 351 \ 'ab', 352 \ 'a', 353 \ 'a321', 354 \ 'a123', 355 \ 'a122', 356 \ 'b321', 357 \ 'b123', 358 \ 'c123d', 359 \ ' 123b', 360 \ 'c321d', 361 \ 'b322b', 362 \ 'b321', 363 \ 'b321b', 364 \ '', 365 \ '' 366 \ ], 367 \ 'expected' : [ 368 \ 'abc', 369 \ 'ab', 370 \ 'a', 371 \ '', 372 \ '', 373 \ 'a122', 374 \ 'a123', 375 \ 'b123', 376 \ 'c123d', 377 \ ' 123b', 378 \ 'a321', 379 \ 'b321', 380 \ 'c321d', 381 \ 'b321', 382 \ 'b321b', 383 \ 'b322b' 384 \ ] 385 \ }, 386 \ { 387 \ 'name' : 'Reverse hexadecimal sort', 388 \ 'cmd' : 'sort! x', 389 \ 'input' : [ 390 \ 'abc', 391 \ 'ab', 392 \ 'a', 393 \ 'a321', 394 \ 'a123', 395 \ 'a122', 396 \ 'b321', 397 \ 'b123', 398 \ 'c123d', 399 \ ' 123b', 400 \ 'c321d', 401 \ 'b322b', 402 \ 'b321', 403 \ 'b321b', 404 \ '', 405 \ '' 406 \ ], 407 \ 'expected' : [ 408 \ 'c321d', 409 \ 'c123d', 410 \ 'b322b', 411 \ 'b321b', 412 \ 'b321', 413 \ 'b321', 414 \ 'b123', 415 \ 'a321', 416 \ 'a123', 417 \ 'a122', 418 \ ' 123b', 419 \ 'abc', 420 \ 'ab', 421 \ 'a', 422 \ '', 423 \ '' 424 \ ] 425 \ }, 426 \ { 427 \ 'name' : 'Alpha (skip first character) sort', 428 \ 'cmd' : 'sort/./', 429 \ 'input' : [ 430 \ 'abc', 431 \ 'ab', 432 \ 'a', 433 \ 'a321', 434 \ 'a123', 435 \ 'a122', 436 \ 'b321', 437 \ 'b123', 438 \ 'c123d', 439 \ ' 123b', 440 \ 'c321d', 441 \ 'b322b', 442 \ 'b321', 443 \ 'b321b', 444 \ '', 445 \ '' 446 \ ], 447 \ 'expected' : [ 448 \ 'a', 449 \ '', 450 \ '', 451 \ 'a122', 452 \ 'a123', 453 \ 'b123', 454 \ ' 123b', 455 \ 'c123d', 456 \ 'a321', 457 \ 'b321', 458 \ 'b321', 459 \ 'b321b', 460 \ 'c321d', 461 \ 'b322b', 462 \ 'ab', 463 \ 'abc' 464 \ ] 465 \ }, 466 \ { 467 \ 'name' : 'Alpha (skip first 2 characters) sort', 468 \ 'cmd' : 'sort/../', 469 \ 'input' : [ 470 \ 'abc', 471 \ 'ab', 472 \ 'a', 473 \ 'a321', 474 \ 'a123', 475 \ 'a122', 476 \ 'b321', 477 \ 'b123', 478 \ 'c123d', 479 \ ' 123b', 480 \ 'c321d', 481 \ 'b322b', 482 \ 'b321', 483 \ 'b321b', 484 \ '', 485 \ '' 486 \ ], 487 \ 'expected' : [ 488 \ 'ab', 489 \ 'a', 490 \ '', 491 \ '', 492 \ 'a321', 493 \ 'b321', 494 \ 'b321', 495 \ 'b321b', 496 \ 'c321d', 497 \ 'a122', 498 \ 'b322b', 499 \ 'a123', 500 \ 'b123', 501 \ ' 123b', 502 \ 'c123d', 503 \ 'abc' 504 \ ] 505 \ }, 506 \ { 507 \ 'name' : 'alpha, unique, skip first 2 characters', 508 \ 'cmd' : 'sort/../u', 509 \ 'input' : [ 510 \ 'abc', 511 \ 'ab', 512 \ 'a', 513 \ 'a321', 514 \ 'a123', 515 \ 'a122', 516 \ 'b321', 517 \ 'b123', 518 \ 'c123d', 519 \ ' 123b', 520 \ 'c321d', 521 \ 'b322b', 522 \ 'b321', 523 \ 'b321b', 524 \ '', 525 \ '' 526 \ ], 527 \ 'expected' : [ 528 \ 'ab', 529 \ 'a', 530 \ '', 531 \ 'a321', 532 \ 'b321', 533 \ 'b321b', 534 \ 'c321d', 535 \ 'a122', 536 \ 'b322b', 537 \ 'a123', 538 \ 'b123', 539 \ ' 123b', 540 \ 'c123d', 541 \ 'abc' 542 \ ] 543 \ }, 544 \ { 545 \ 'name' : 'numeric, skip first character', 546 \ 'cmd' : 'sort/./n', 547 \ 'input' : [ 548 \ 'abc', 549 \ 'ab', 550 \ 'a', 551 \ 'a321', 552 \ 'a123', 553 \ 'a122', 554 \ 'b321', 555 \ 'b123', 556 \ 'c123d', 557 \ ' 123b', 558 \ 'c321d', 559 \ 'b322b', 560 \ 'b321', 561 \ 'b321b', 562 \ '', 563 \ '' 564 \ ], 565 \ 'expected' : [ 566 \ 'abc', 567 \ 'ab', 568 \ 'a', 569 \ '', 570 \ '', 571 \ 'a122', 572 \ 'a123', 573 \ 'b123', 574 \ 'c123d', 575 \ ' 123b', 576 \ 'a321', 577 \ 'b321', 578 \ 'c321d', 579 \ 'b321', 580 \ 'b321b', 581 \ 'b322b' 582 \ ] 583 \ }, 584 \ { 585 \ 'name' : 'alpha, sort on first character', 586 \ 'cmd' : 'sort/./r', 587 \ 'input' : [ 588 \ 'abc', 589 \ 'ab', 590 \ 'a', 591 \ 'a321', 592 \ 'a123', 593 \ 'a122', 594 \ 'b321', 595 \ 'b123', 596 \ 'c123d', 597 \ ' 123b', 598 \ 'c321d', 599 \ 'b322b', 600 \ 'b321', 601 \ 'b321b', 602 \ '', 603 \ '' 604 \ ], 605 \ 'expected' : [ 606 \ '', 607 \ '', 608 \ ' 123b', 609 \ 'abc', 610 \ 'ab', 611 \ 'a', 612 \ 'a321', 613 \ 'a123', 614 \ 'a122', 615 \ 'b321', 616 \ 'b123', 617 \ 'b322b', 618 \ 'b321', 619 \ 'b321b', 620 \ 'c123d', 621 \ 'c321d' 622 \ ] 623 \ }, 624 \ { 625 \ 'name' : 'alpha, sort on first 2 characters', 626 \ 'cmd' : 'sort/../r', 627 \ 'input' : [ 628 \ 'abc', 629 \ 'ab', 630 \ 'a', 631 \ 'a321', 632 \ 'a123', 633 \ 'a122', 634 \ 'b321', 635 \ 'b123', 636 \ 'c123d', 637 \ ' 123b', 638 \ 'c321d', 639 \ 'b322b', 640 \ 'b321', 641 \ 'b321b', 642 \ '', 643 \ '' 644 \ ], 645 \ 'expected' : [ 646 \ 'a', 647 \ '', 648 \ '', 649 \ ' 123b', 650 \ 'a123', 651 \ 'a122', 652 \ 'a321', 653 \ 'abc', 654 \ 'ab', 655 \ 'b123', 656 \ 'b321', 657 \ 'b322b', 658 \ 'b321', 659 \ 'b321b', 660 \ 'c123d', 661 \ 'c321d' 662 \ ] 663 \ }, 664 \ { 665 \ 'name' : 'numeric, sort on first character', 666 \ 'cmd' : 'sort/./rn', 667 \ 'input' : [ 668 \ 'abc', 669 \ 'ab', 670 \ 'a', 671 \ 'a321', 672 \ 'a123', 673 \ 'a122', 674 \ 'b321', 675 \ 'b123', 676 \ 'c123d', 677 \ ' 123b', 678 \ 'c321d', 679 \ 'b322b', 680 \ 'b321', 681 \ 'b321b', 682 \ '', 683 \ '' 684 \ ], 685 \ 'expected' : [ 686 \ 'abc', 687 \ 'ab', 688 \ 'a', 689 \ 'a321', 690 \ 'a123', 691 \ 'a122', 692 \ 'b321', 693 \ 'b123', 694 \ 'c123d', 695 \ ' 123b', 696 \ 'c321d', 697 \ 'b322b', 698 \ 'b321', 699 \ 'b321b', 700 \ '', 701 \ '' 702 \ ] 703 \ }, 704 \ { 705 \ 'name' : 'alpha, skip past first digit', 706 \ 'cmd' : 'sort/\d/', 707 \ 'input' : [ 708 \ 'abc', 709 \ 'ab', 710 \ 'a', 711 \ 'a321', 712 \ 'a123', 713 \ 'a122', 714 \ 'b321', 715 \ 'b123', 716 \ 'c123d', 717 \ ' 123b', 718 \ 'c321d', 719 \ 'b322b', 720 \ 'b321', 721 \ 'b321b', 722 \ '', 723 \ '' 724 \ ], 725 \ 'expected' : [ 726 \ 'abc', 727 \ 'ab', 728 \ 'a', 729 \ '', 730 \ '', 731 \ 'a321', 732 \ 'b321', 733 \ 'b321', 734 \ 'b321b', 735 \ 'c321d', 736 \ 'a122', 737 \ 'b322b', 738 \ 'a123', 739 \ 'b123', 740 \ ' 123b', 741 \ 'c123d' 742 \ ] 743 \ }, 744 \ { 745 \ 'name' : 'alpha, sort on first digit', 746 \ 'cmd' : 'sort/\d/r', 747 \ 'input' : [ 748 \ 'abc', 749 \ 'ab', 750 \ 'a', 751 \ 'a321', 752 \ 'a123', 753 \ 'a122', 754 \ 'b321', 755 \ 'b123', 756 \ 'c123d', 757 \ ' 123b', 758 \ 'c321d', 759 \ 'b322b', 760 \ 'b321', 761 \ 'b321b', 762 \ '', 763 \ '' 764 \ ], 765 \ 'expected' : [ 766 \ 'abc', 767 \ 'ab', 768 \ 'a', 769 \ '', 770 \ '', 771 \ 'a123', 772 \ 'a122', 773 \ 'b123', 774 \ 'c123d', 775 \ ' 123b', 776 \ 'a321', 777 \ 'b321', 778 \ 'c321d', 779 \ 'b322b', 780 \ 'b321', 781 \ 'b321b' 782 \ ] 783 \ }, 784 \ { 785 \ 'name' : 'numeric, skip past first digit', 786 \ 'cmd' : 'sort/\d/n', 787 \ 'input' : [ 788 \ 'abc', 789 \ 'ab', 790 \ 'a', 791 \ 'a321', 792 \ 'a123', 793 \ 'a122', 794 \ 'b321', 795 \ 'b123', 796 \ 'c123d', 797 \ ' 123b', 798 \ 'c321d', 799 \ 'b322b', 800 \ 'b321', 801 \ 'b321b', 802 \ '', 803 \ '' 804 \ ], 805 \ 'expected' : [ 806 \ 'abc', 807 \ 'ab', 808 \ 'a', 809 \ '', 810 \ '', 811 \ 'a321', 812 \ 'b321', 813 \ 'c321d', 814 \ 'b321', 815 \ 'b321b', 816 \ 'a122', 817 \ 'b322b', 818 \ 'a123', 819 \ 'b123', 820 \ 'c123d', 821 \ ' 123b' 822 \ ] 823 \ }, 824 \ { 825 \ 'name' : 'numeric, sort on first digit', 826 \ 'cmd' : 'sort/\d/rn', 827 \ 'input' : [ 828 \ 'abc', 829 \ 'ab', 830 \ 'a', 831 \ 'a321', 832 \ 'a123', 833 \ 'a122', 834 \ 'b321', 835 \ 'b123', 836 \ 'c123d', 837 \ ' 123b', 838 \ 'c321d', 839 \ 'b322b', 840 \ 'b321', 841 \ 'b321b', 842 \ '', 843 \ '' 844 \ ], 845 \ 'expected' : [ 846 \ 'abc', 847 \ 'ab', 848 \ 'a', 849 \ '', 850 \ '', 851 \ 'a123', 852 \ 'a122', 853 \ 'b123', 854 \ 'c123d', 855 \ ' 123b', 856 \ 'a321', 857 \ 'b321', 858 \ 'c321d', 859 \ 'b322b', 860 \ 'b321', 861 \ 'b321b' 862 \ ] 863 \ }, 864 \ { 865 \ 'name' : 'alpha, skip past first 2 digits', 866 \ 'cmd' : 'sort/\d\d/', 867 \ 'input' : [ 868 \ 'abc', 869 \ 'ab', 870 \ 'a', 871 \ 'a321', 872 \ 'a123', 873 \ 'a122', 874 \ 'b321', 875 \ 'b123', 876 \ 'c123d', 877 \ ' 123b', 878 \ 'c321d', 879 \ 'b322b', 880 \ 'b321', 881 \ 'b321b', 882 \ '', 883 \ '' 884 \ ], 885 \ 'expected' : [ 886 \ 'abc', 887 \ 'ab', 888 \ 'a', 889 \ '', 890 \ '', 891 \ 'a321', 892 \ 'b321', 893 \ 'b321', 894 \ 'b321b', 895 \ 'c321d', 896 \ 'a122', 897 \ 'b322b', 898 \ 'a123', 899 \ 'b123', 900 \ ' 123b', 901 \ 'c123d' 902 \ ] 903 \ }, 904 \ { 905 \ 'name' : 'numeric, skip past first 2 digits', 906 \ 'cmd' : 'sort/\d\d/n', 907 \ 'input' : [ 908 \ 'abc', 909 \ 'ab', 910 \ 'a', 911 \ 'a321', 912 \ 'a123', 913 \ 'a122', 914 \ 'b321', 915 \ 'b123', 916 \ 'c123d', 917 \ ' 123b', 918 \ 'c321d', 919 \ 'b322b', 920 \ 'b321', 921 \ 'b321b', 922 \ '', 923 \ '' 924 \ ], 925 \ 'expected' : [ 926 \ 'abc', 927 \ 'ab', 928 \ 'a', 929 \ '', 930 \ '', 931 \ 'a321', 932 \ 'b321', 933 \ 'c321d', 934 \ 'b321', 935 \ 'b321b', 936 \ 'a122', 937 \ 'b322b', 938 \ 'a123', 939 \ 'b123', 940 \ 'c123d', 941 \ ' 123b' 942 \ ] 943 \ }, 944 \ { 945 \ 'name' : 'hexadecimal, skip past first 2 digits', 946 \ 'cmd' : 'sort/\d\d/x', 947 \ 'input' : [ 948 \ 'abc', 949 \ 'ab', 950 \ 'a', 951 \ 'a321', 952 \ 'a123', 953 \ 'a122', 954 \ 'b321', 955 \ 'b123', 956 \ 'c123d', 957 \ ' 123b', 958 \ 'c321d', 959 \ 'b322b', 960 \ 'b321', 961 \ 'b321b', 962 \ '', 963 \ '' 964 \ ], 965 \ 'expected' : [ 966 \ 'abc', 967 \ 'ab', 968 \ 'a', 969 \ '', 970 \ '', 971 \ 'a321', 972 \ 'b321', 973 \ 'b321', 974 \ 'a122', 975 \ 'a123', 976 \ 'b123', 977 \ 'b321b', 978 \ 'c321d', 979 \ 'b322b', 980 \ ' 123b', 981 \ 'c123d' 982 \ ] 983 \ }, 984 \ { 985 \ 'name' : 'alpha, sort on first 2 digits', 986 \ 'cmd' : 'sort/\d\d/r', 987 \ 'input' : [ 988 \ 'abc', 989 \ 'ab', 990 \ 'a', 991 \ 'a321', 992 \ 'a123', 993 \ 'a122', 994 \ 'b321', 995 \ 'b123', 996 \ 'c123d', 997 \ ' 123b', 998 \ 'c321d', 999 \ 'b322b', 1000 \ 'b321', 1001 \ 'b321b', 1002 \ '', 1003 \ '' 1004 \ ], 1005 \ 'expected' : [ 1006 \ 'abc', 1007 \ 'ab', 1008 \ 'a', 1009 \ '', 1010 \ '', 1011 \ 'a123', 1012 \ 'a122', 1013 \ 'b123', 1014 \ 'c123d', 1015 \ ' 123b', 1016 \ 'a321', 1017 \ 'b321', 1018 \ 'c321d', 1019 \ 'b322b', 1020 \ 'b321', 1021 \ 'b321b' 1022 \ ] 1023 \ }, 1024 \ { 1025 \ 'name' : 'numeric, sort on first 2 digits', 1026 \ 'cmd' : 'sort/\d\d/rn', 1027 \ 'input' : [ 1028 \ 'abc', 1029 \ 'ab', 1030 \ 'a', 1031 \ 'a321', 1032 \ 'a123', 1033 \ 'a122', 1034 \ 'b321', 1035 \ 'b123', 1036 \ 'c123d', 1037 \ ' 123b', 1038 \ 'c321d', 1039 \ 'b322b', 1040 \ 'b321', 1041 \ 'b321b', 1042 \ '', 1043 \ '' 1044 \ ], 1045 \ 'expected' : [ 1046 \ 'abc', 1047 \ 'ab', 1048 \ 'a', 1049 \ '', 1050 \ '', 1051 \ 'a123', 1052 \ 'a122', 1053 \ 'b123', 1054 \ 'c123d', 1055 \ ' 123b', 1056 \ 'a321', 1057 \ 'b321', 1058 \ 'c321d', 1059 \ 'b322b', 1060 \ 'b321', 1061 \ 'b321b' 1062 \ ] 1063 \ }, 1064 \ { 1065 \ 'name' : 'hexadecimal, sort on first 2 digits', 1066 \ 'cmd' : 'sort/\d\d/rx', 1067 \ 'input' : [ 1068 \ 'abc', 1069 \ 'ab', 1070 \ 'a', 1071 \ 'a321', 1072 \ 'a123', 1073 \ 'a122', 1074 \ 'b321', 1075 \ 'b123', 1076 \ 'c123d', 1077 \ ' 123b', 1078 \ 'c321d', 1079 \ 'b322b', 1080 \ 'b321', 1081 \ 'b321b', 1082 \ '', 1083 \ '' 1084 \ ], 1085 \ 'expected' : [ 1086 \ 'abc', 1087 \ 'ab', 1088 \ 'a', 1089 \ '', 1090 \ '', 1091 \ 'a123', 1092 \ 'a122', 1093 \ 'b123', 1094 \ 'c123d', 1095 \ ' 123b', 1096 \ 'a321', 1097 \ 'b321', 1098 \ 'c321d', 1099 \ 'b322b', 1100 \ 'b321', 1101 \ 'b321b' 1102 \ ] 1103 \ }, 1104 \ { 1105 \ 'name' : 'binary', 1106 \ 'cmd' : 'sort b', 1107 \ 'input' : [ 1108 \ '0b111000', 1109 \ '0b101100', 1110 \ '0b101001', 1111 \ '0b101001', 1112 \ '0b101000', 1113 \ '0b000000', 1114 \ '0b001000', 1115 \ '0b010000', 1116 \ '0b101000', 1117 \ '0b100000', 1118 \ '0b101010', 1119 \ '0b100010', 1120 \ '0b100100', 1121 \ '0b100010', 1122 \ '', 1123 \ '' 1124 \ ], 1125 \ 'expected' : [ 1126 \ '', 1127 \ '', 1128 \ '0b000000', 1129 \ '0b001000', 1130 \ '0b010000', 1131 \ '0b100000', 1132 \ '0b100010', 1133 \ '0b100010', 1134 \ '0b100100', 1135 \ '0b101000', 1136 \ '0b101000', 1137 \ '0b101001', 1138 \ '0b101001', 1139 \ '0b101010', 1140 \ '0b101100', 1141 \ '0b111000' 1142 \ ] 1143 \ }, 1144 \ { 1145 \ 'name' : 'binary with leading characters', 1146 \ 'cmd' : 'sort b', 1147 \ 'input' : [ 1148 \ '0b100010', 1149 \ '0b010000', 1150 \ ' 0b101001', 1151 \ 'b0b101100', 1152 \ '0b100010', 1153 \ ' 0b100100', 1154 \ 'a0b001000', 1155 \ '0b101000', 1156 \ '0b101000', 1157 \ 'a0b101001', 1158 \ 'ab0b100000', 1159 \ '0b101010', 1160 \ '0b000000', 1161 \ 'b0b111000', 1162 \ '', 1163 \ '' 1164 \ ], 1165 \ 'expected' : [ 1166 \ '', 1167 \ '', 1168 \ '0b000000', 1169 \ 'a0b001000', 1170 \ '0b010000', 1171 \ 'ab0b100000', 1172 \ '0b100010', 1173 \ '0b100010', 1174 \ ' 0b100100', 1175 \ '0b101000', 1176 \ '0b101000', 1177 \ ' 0b101001', 1178 \ 'a0b101001', 1179 \ '0b101010', 1180 \ 'b0b101100', 1181 \ 'b0b111000' 1182 \ ] 1183 \ }, 1184 \ { 1185 \ 'name' : 'alphabetical, sorted input', 1186 \ 'cmd' : 'sort', 1187 \ 'input' : [ 1188 \ 'a', 1189 \ 'b', 1190 \ 'c', 1191 \ ], 1192 \ 'expected' : [ 1193 \ 'a', 1194 \ 'b', 1195 \ 'c', 1196 \ ] 1197 \ }, 1198 \ { 1199 \ 'name' : 'alphabetical, sorted input, unique at end', 1200 \ 'cmd' : 'sort u', 1201 \ 'input' : [ 1202 \ 'aa', 1203 \ 'bb', 1204 \ 'cc', 1205 \ 'cc', 1206 \ ], 1207 \ 'expected' : [ 1208 \ 'aa', 1209 \ 'bb', 1210 \ 'cc', 1211 \ ] 1212 \ }, 1213 \ { 1214 \ 'name' : 'sort one line buffer', 1215 \ 'cmd' : 'sort', 1216 \ 'input' : [ 1217 \ 'single line' 1218 \ ], 1219 \ 'expected' : [ 1220 \ 'single line' 1221 \ ] 1222 \ }, 1223 \ { 1224 \ 'name' : 'sort ignoring case', 1225 \ 'cmd' : '%sort i', 1226 \ 'input' : [ 1227 \ 'BB', 1228 \ 'Cc', 1229 \ 'aa' 1230 \ ], 1231 \ 'expected' : [ 1232 \ 'aa', 1233 \ 'BB', 1234 \ 'Cc' 1235 \ ] 1236 \ }, 1237 \ ] 1238 1239 " This does not appear to work correctly on Mac. 1240 if !has('mac') 1241 if v:collate =~? '^\(en\|fr\)_ca.utf-\?8$' 1242 " en_CA.utf-8 sorts capitals before lower case 1243 " 'Œ' is omitted because it can sort before or after 'œ' 1244 let tests += [ 1245 \ { 1246 \ 'name' : 'sort with locale ' .. v:collate, 1247 \ 'cmd' : '%sort l', 1248 \ 'input' : [ 1249 \ 'A', 1250 \ 'E', 1251 \ 'O', 1252 \ 'À', 1253 \ 'È', 1254 \ 'É', 1255 \ 'Ô', 1256 \ 'Z', 1257 \ 'a', 1258 \ 'e', 1259 \ 'o', 1260 \ 'à', 1261 \ 'è', 1262 \ 'é', 1263 \ 'ô', 1264 \ 'œ', 1265 \ 'z' 1266 \ ], 1267 \ 'expected' : [ 1268 \ 'A', 1269 \ 'a', 1270 \ 'À', 1271 \ 'à', 1272 \ 'E', 1273 \ 'e', 1274 \ 'É', 1275 \ 'é', 1276 \ 'È', 1277 \ 'è', 1278 \ 'O', 1279 \ 'o', 1280 \ 'Ô', 1281 \ 'ô', 1282 \ 'œ', 1283 \ 'Z', 1284 \ 'z' 1285 \ ] 1286 \ }, 1287 \ ] 1288 elseif v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$' 1289 " With these locales, the accentuated letters are ordered 1290 " similarly to the non-accentuated letters. 1291 let tests += [ 1292 \ { 1293 \ 'name' : 'sort with locale ' .. v:collate, 1294 \ 'cmd' : '%sort l', 1295 \ 'input' : [ 1296 \ 'A', 1297 \ 'E', 1298 \ 'O', 1299 \ 'À', 1300 \ 'È', 1301 \ 'É', 1302 \ 'Ô', 1303 \ 'Œ', 1304 \ 'Z', 1305 \ 'a', 1306 \ 'e', 1307 \ 'o', 1308 \ 'à', 1309 \ 'è', 1310 \ 'é', 1311 \ 'ô', 1312 \ 'œ', 1313 \ 'z' 1314 \ ], 1315 \ 'expected' : [ 1316 \ 'a', 1317 \ 'A', 1318 \ 'à', 1319 \ 'À', 1320 \ 'e', 1321 \ 'E', 1322 \ 'é', 1323 \ 'É', 1324 \ 'è', 1325 \ 'È', 1326 \ 'o', 1327 \ 'O', 1328 \ 'ô', 1329 \ 'Ô', 1330 \ 'œ', 1331 \ 'Œ', 1332 \ 'z', 1333 \ 'Z' 1334 \ ] 1335 \ }, 1336 \ ] 1337 endif 1338 endif 1339 if has('float') 1340 let tests += [ 1341 \ { 1342 \ 'name' : 'float', 1343 \ 'cmd' : 'sort f', 1344 \ 'input' : [ 1345 \ '1.234', 1346 \ '0.88', 1347 \ ' + 123.456', 1348 \ '1.15e-6', 1349 \ '-1.1e3', 1350 \ '-1.01e3', 1351 \ '', 1352 \ '' 1353 \ ], 1354 \ 'expected' : [ 1355 \ '', 1356 \ '', 1357 \ '-1.1e3', 1358 \ '-1.01e3', 1359 \ '1.15e-6', 1360 \ '0.88', 1361 \ '1.234', 1362 \ ' + 123.456' 1363 \ ] 1364 \ }, 1365 \ ] 1366 endif 1367 1368 for t in tests 1369 enew! 1370 call append(0, t.input) 1371 $delete _ 1372 setlocal nomodified 1373 execute t.cmd 1374 1375 call assert_equal(t.expected, getline(1, '$'), t.name) 1376 1377 " Previously, the ":sort" command would set 'modified' even if the buffer 1378 " contents did not change. Here, we check that this problem is fixed. 1379 if t.input == t.expected 1380 call assert_false(&modified, t.name . ': &mod is not correct') 1381 else 1382 call assert_true(&modified, t.name . ': &mod is not correct') 1383 endif 1384 endfor 1385 1386 " Needs at least two lines for this test 1387 call setline(1, ['line1', 'line2']) 1388 call assert_fails('sort no', 'E474:') 1389 call assert_fails('sort c', 'E475:') 1390 call assert_fails('sort #pat%', 'E654:') 1391 call assert_fails('sort /\%(/', 'E53:') 1392 1393 enew! 1394endfunc 1395 1396func Test_sort_large_num() 1397 new 1398 a 1399-2147483648 1400-2147483647 1401 1402-1 14030 14041 1405-2147483646 14062147483646 14072147483647 14082147483647 1409-2147483648 1410abc 1411 1412. 1413 " Numerical sort. Non-numeric lines are ordered before numerical lines. 1414 " Ordering of non-numerical is stable. 1415 sort n 1416 call assert_equal(['', 1417 \ 'abc', 1418 \ '', 1419 \ '-2147483648', 1420 \ '-2147483648', 1421 \ '-2147483647', 1422 \ '-2147483646', 1423 \ '-1', 1424 \ '0', 1425 \ '1', 1426 \ '2147483646', 1427 \ '2147483647', 1428 \ '2147483647'], getline(1, '$')) 1429 bwipe! 1430 1431 new 1432 a 1433-9223372036854775808 1434-9223372036854775807 1435 1436-1 14370 14381 1439-9223372036854775806 14409223372036854775806 14419223372036854775807 14429223372036854775807 1443-9223372036854775808 1444abc 1445 1446. 1447 sort n 1448 call assert_equal(['', 1449 \ 'abc', 1450 \ '', 1451 \ '-9223372036854775808', 1452 \ '-9223372036854775808', 1453 \ '-9223372036854775807', 1454 \ '-9223372036854775806', 1455 \ '-1', 1456 \ '0', 1457 \ '1', 1458 \ '9223372036854775806', 1459 \ '9223372036854775807', 1460 \ '9223372036854775807'], getline(1, '$')) 1461 bwipe! 1462endfunc 1463 1464 1465func Test_sort_cmd_report() 1466 enew! 1467 call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3)) 1468 $delete _ 1469 setlocal nomodified 1470 let res = execute('%sort u') 1471 1472 call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0')) 1473 call assert_match("6 fewer lines", res) 1474 enew! 1475 call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3)) 1476 $delete _ 1477 setlocal nomodified report=10 1478 let res = execute('%sort u') 1479 1480 call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0')) 1481 call assert_equal("", res) 1482 enew! 1483 call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3)) 1484 $delete _ 1485 setl report&vim 1486 setlocal nomodified 1487 let res = execute('1g/^/%sort u') 1488 1489 call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0')) 1490 " the output comes from the :g command, not from the :sort 1491 call assert_match("6 fewer lines", res) 1492 enew! 1493endfunc 1494 1495" Test for a :sort command followed by another command 1496func Test_sort_followed_by_cmd() 1497 new 1498 let var = '' 1499 call setline(1, ['cc', 'aa', 'bb']) 1500 %sort | let var = "sortcmdtest" 1501 call assert_equal(var, "sortcmdtest") 1502 call assert_equal(['aa', 'bb', 'cc'], getline(1, '$')) 1503 " Test for :sort followed by a comment 1504 call setline(1, ['3b', '1c', '2a']) 1505 %sort /\d\+/ " sort alphabetically 1506 call assert_equal(['2a', '3b', '1c'], getline(1, '$')) 1507 close! 1508endfunc 1509 1510" Test for :sort using last search pattern 1511func Test_sort_last_search_pat() 1512 new 1513 let @/ = '\d\+' 1514 call setline(1, ['3b', '1c', '2a']) 1515 sort // 1516 call assert_equal(['2a', '3b', '1c'], getline(1, '$')) 1517 close! 1518endfunc 1519 1520" Test for :sort with no last search pattern 1521func Test_sort_with_no_last_search_pat() 1522 let lines =<< trim [SCRIPT] 1523 call setline(1, ['3b', '1c', '2a']) 1524 call assert_fails('sort //', 'E35:') 1525 call writefile(v:errors, 'Xresult') 1526 qall! 1527 [SCRIPT] 1528 call writefile(lines, 'Xscript') 1529 if RunVim([], [], '--clean -S Xscript') 1530 call assert_equal([], readfile('Xresult')) 1531 endif 1532 call delete('Xscript') 1533 call delete('Xresult') 1534endfunc 1535 1536" Test for retaining marks across a :sort 1537func Test_sort_with_marks() 1538 new 1539 call setline(1, ['cc', 'aa', 'bb']) 1540 call setpos("'c", [0, 1, 0, 0]) 1541 call setpos("'a", [0, 2, 0, 0]) 1542 call setpos("'b", [0, 3, 0, 0]) 1543 %sort 1544 call assert_equal(['aa', 'bb', 'cc'], getline(1, '$')) 1545 call assert_equal(2, line("'a")) 1546 call assert_equal(3, line("'b")) 1547 call assert_equal(1, line("'c")) 1548 close! 1549endfunc 1550 1551" vim: shiftwidth=2 sts=2 expandtab 1552