Bram Moolenaar | 8822744 | 2017-01-17 22:16:00 +0100 | [diff] [blame] | 1 | " Test :retab |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | 8822744 | 2017-01-17 22:16:00 +0100 | [diff] [blame] | 3 | func SetUp() |
| 4 | new |
| 5 | call setline(1, "\ta \t b c ") |
| 6 | endfunc |
| 7 | |
| 8 | func TearDown() |
| 9 | bwipe! |
| 10 | endfunc |
| 11 | |
| 12 | func Retab(bang, n) |
| 13 | let l:old_tabstop = &tabstop |
| 14 | let l:old_line = getline(1) |
| 15 | exe "retab" . a:bang . a:n |
| 16 | let l:line = getline(1) |
| 17 | call setline(1, l:old_line) |
| 18 | if a:n > 0 |
| 19 | " :retab changes 'tabstop' to n with argument n > 0. |
| 20 | call assert_equal(a:n, &tabstop) |
| 21 | exe 'set tabstop=' . l:old_tabstop |
| 22 | else |
| 23 | " :retab does not change 'tabstop' with empty or n <= 0. |
| 24 | call assert_equal(l:old_tabstop, &tabstop) |
| 25 | endif |
| 26 | return l:line |
| 27 | endfunc |
| 28 | |
| 29 | func Test_retab() |
| 30 | set tabstop=8 noexpandtab |
| 31 | call assert_equal("\ta\t b c ", Retab('', '')) |
| 32 | call assert_equal("\ta\t b c ", Retab('', 0)) |
| 33 | call assert_equal("\ta\t b c ", Retab('', 8)) |
| 34 | call assert_equal("\ta\t b\t c\t ", Retab('!', '')) |
| 35 | call assert_equal("\ta\t b\t c\t ", Retab('!', 0)) |
| 36 | call assert_equal("\ta\t b\t c\t ", Retab('!', 8)) |
| 37 | |
| 38 | call assert_equal("\t\ta\t\t\tb c ", Retab('', 4)) |
| 39 | call assert_equal("\t\ta\t\t\tb\t\t c\t ", Retab('!', 4)) |
| 40 | |
| 41 | call assert_equal(" a\t\tb c ", Retab('', 10)) |
| 42 | call assert_equal(" a\t\tb c ", Retab('!', 10)) |
| 43 | |
| 44 | set tabstop=8 expandtab |
| 45 | call assert_equal(" a b c ", Retab('', '')) |
| 46 | call assert_equal(" a b c ", Retab('', 0)) |
| 47 | call assert_equal(" a b c ", Retab('', 8)) |
| 48 | call assert_equal(" a b c ", Retab('!', '')) |
| 49 | call assert_equal(" a b c ", Retab('!', 0)) |
| 50 | call assert_equal(" a b c ", Retab('!', 8)) |
| 51 | |
| 52 | call assert_equal(" a b c ", Retab(' ', 4)) |
| 53 | call assert_equal(" a b c ", Retab('!', 4)) |
| 54 | |
| 55 | call assert_equal(" a b c ", Retab(' ', 10)) |
| 56 | call assert_equal(" a b c ", Retab('!', 10)) |
| 57 | |
| 58 | set tabstop=4 noexpandtab |
| 59 | call assert_equal("\ta\t\tb c ", Retab('', '')) |
| 60 | call assert_equal("\ta\t\tb\t\t c\t ", Retab('!', '')) |
| 61 | call assert_equal("\t a\t\t\tb c ", Retab('', 3)) |
| 62 | call assert_equal("\t a\t\t\tb\t\t\tc\t ", Retab('!', 3)) |
| 63 | call assert_equal(" a\t b c ", Retab('', 5)) |
| 64 | call assert_equal(" a\t b\t\t c\t ", Retab('!', 5)) |
| 65 | |
| 66 | set tabstop=4 expandtab |
| 67 | call assert_equal(" a b c ", Retab('', '')) |
| 68 | call assert_equal(" a b c ", Retab('!', '')) |
| 69 | call assert_equal(" a b c ", Retab('', 3)) |
| 70 | call assert_equal(" a b c ", Retab('!', 3)) |
| 71 | call assert_equal(" a b c ", Retab('', 5)) |
| 72 | call assert_equal(" a b c ", Retab('!', 5)) |
Bram Moolenaar | 6e28703 | 2022-02-12 15:42:18 +0000 | [diff] [blame] | 73 | |
| 74 | set tabstop& expandtab& |
Bram Moolenaar | 8822744 | 2017-01-17 22:16:00 +0100 | [diff] [blame] | 75 | endfunc |
| 76 | |
| 77 | func Test_retab_error() |
| 78 | call assert_fails('retab -1', 'E487:') |
| 79 | call assert_fails('retab! -1', 'E487:') |
Bram Moolenaar | b7081e1 | 2021-09-04 18:47:28 +0200 | [diff] [blame] | 80 | call assert_fails('ret -1000', 'E487:') |
| 81 | call assert_fails('ret 10000', 'E475:') |
| 82 | call assert_fails('ret 80000000000000000000', 'E475:') |
Bram Moolenaar | 8822744 | 2017-01-17 22:16:00 +0100 | [diff] [blame] | 83 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 84 | |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 85 | func RetabLoop() |
| 86 | while 1 |
| 87 | set ts=4000 |
| 88 | retab 4 |
| 89 | endwhile |
| 90 | endfunc |
| 91 | |
| 92 | func Test_retab_endless() |
Bram Moolenaar | 308660b | 2022-06-16 12:10:48 +0100 | [diff] [blame] | 93 | " inside try/catch we can catch the error message |
Bram Moolenaar | 6e28703 | 2022-02-12 15:42:18 +0000 | [diff] [blame] | 94 | call setline(1, "\t0\t") |
| 95 | let caught = 'no' |
| 96 | try |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 97 | call RetabLoop() |
| 98 | catch /E1240:/ |
Bram Moolenaar | 9397423 | 2022-06-12 23:05:07 +0100 | [diff] [blame] | 99 | let caught = v:exception |
Bram Moolenaar | 6e28703 | 2022-02-12 15:42:18 +0000 | [diff] [blame] | 100 | endtry |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 101 | call assert_match('E1240:', caught) |
Bram Moolenaar | 83497f8 | 2022-06-15 22:11:45 +0100 | [diff] [blame] | 102 | |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 103 | set tabstop& |
| 104 | endfunc |
| 105 | |
| 106 | func Test_nocatch_retab_endless() |
Bram Moolenaar | 308660b | 2022-06-16 12:10:48 +0100 | [diff] [blame] | 107 | " when not inside try/catch an interrupt is generated to get out of loops |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 108 | call setline(1, "\t0\t") |
| 109 | call assert_fails('call RetabLoop()', ['E1240:', 'Interrupted']) |
| 110 | |
Bram Moolenaar | 6e28703 | 2022-02-12 15:42:18 +0000 | [diff] [blame] | 111 | set tabstop& |
| 112 | endfunc |
| 113 | |
| 114 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 115 | " vim: shiftwidth=2 sts=2 expandtab |