Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1 | " Test for :global and :vglobal |
| 2 | |
Bram Moolenaar | 3fcfa35 | 2017-03-29 19:20:41 +0200 | [diff] [blame] | 3 | func Test_yank_put_clipboard() |
| 4 | new |
| 5 | call setline(1, ['a', 'b', 'c']) |
| 6 | set clipboard=unnamed |
| 7 | g/^/normal yyp |
| 8 | call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6)) |
Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 9 | set clipboard=unnamed,unnamedplus |
| 10 | call setline(1, ['a', 'b', 'c']) |
| 11 | g/^/normal yyp |
| 12 | call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6)) |
Bram Moolenaar | 3fcfa35 | 2017-03-29 19:20:41 +0200 | [diff] [blame] | 13 | set clipboard& |
| 14 | bwipe! |
| 15 | endfunc |
Bram Moolenaar | f84b122 | 2017-06-10 14:29:52 +0200 | [diff] [blame] | 16 | |
Bram Moolenaar | 07188fc | 2020-06-05 20:03:16 +0200 | [diff] [blame] | 17 | func Test_global_set_clipboard() |
| 18 | CheckFeature clipboard_working |
| 19 | new |
| 20 | set clipboard=unnamedplus |
| 21 | let @+='clipboard' | g/^/set cb= | let @" = 'unnamed' | put |
| 22 | call assert_equal(['','unnamed'], getline(1, '$')) |
| 23 | set clipboard& |
| 24 | bwipe! |
| 25 | endfunc |
| 26 | |
Bram Moolenaar | f84b122 | 2017-06-10 14:29:52 +0200 | [diff] [blame] | 27 | func Test_nested_global() |
| 28 | new |
| 29 | call setline(1, ['nothing', 'found', 'found bad', 'bad']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 30 | call assert_fails('g/found/3v/bad/s/^/++/', 'E147:') |
Bram Moolenaar | f84b122 | 2017-06-10 14:29:52 +0200 | [diff] [blame] | 31 | g/found/v/bad/s/^/++/ |
| 32 | call assert_equal(['nothing', '++found', 'found bad', 'bad'], getline(1, 4)) |
| 33 | bwipe! |
| 34 | endfunc |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 35 | |
| 36 | func Test_global_error() |
| 37 | call assert_fails('g\\a', 'E10:') |
| 38 | call assert_fails('g', 'E148:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 39 | call assert_fails('g/\(/y', 'E54:') |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 40 | endfunc |
| 41 | |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 42 | " Test for printing lines using :g with different search patterns |
| 43 | func Test_global_print() |
| 44 | new |
| 45 | call setline(1, ['foo', 'bar', 'foo', 'foo']) |
| 46 | let @/ = 'foo' |
| 47 | let t = execute("g/")->trim()->split("\n") |
| 48 | call assert_equal(['foo', 'foo', 'foo'], t) |
| 49 | |
| 50 | " Test for Vi compatible patterns |
| 51 | let @/ = 'bar' |
| 52 | let t = execute('g\/')->trim()->split("\n") |
| 53 | call assert_equal(['bar'], t) |
| 54 | |
| 55 | normal gg |
| 56 | s/foo/foo/ |
| 57 | let t = execute('g\&')->trim()->split("\n") |
| 58 | call assert_equal(['foo', 'foo', 'foo'], t) |
| 59 | |
| 60 | let @/ = 'bar' |
| 61 | let t = execute('g?')->trim()->split("\n") |
| 62 | call assert_equal(['bar'], t) |
| 63 | |
| 64 | " Test for the 'Pattern found in every line' message |
| 65 | let v:statusmsg = '' |
| 66 | v/foo\|bar/p |
| 67 | call assert_notequal('', v:statusmsg) |
| 68 | |
Bram Moolenaar | 24d9c05 | 2022-03-04 21:34:31 +0000 | [diff] [blame] | 69 | " In Vim9 script this is an error |
| 70 | let caught = 'no' |
| 71 | try |
| 72 | vim9cmd v/foo\|bar/p |
| 73 | catch /E538/ |
| 74 | let caught = 'yes' |
| 75 | call assert_match('E538: Pattern found in every line: foo\|bar', v:exception) |
| 76 | endtry |
| 77 | call assert_equal('yes', caught) |
| 78 | |
| 79 | " In Vim9 script not matching is an error |
| 80 | let caught = 'no' |
| 81 | try |
| 82 | vim9cmd g/foobarnotfound/p |
| 83 | catch /E486/ |
| 84 | let caught = 'yes' |
| 85 | call assert_match('E486: Pattern not found: foobarnotfound', v:exception) |
| 86 | endtry |
| 87 | call assert_equal('yes', caught) |
| 88 | |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 89 | close! |
| 90 | endfunc |
| 91 | |
Rob Pilling | e86190e | 2022-12-23 19:06:04 +0000 | [diff] [blame] | 92 | func Test_global_empty_pattern() |
| 93 | " populate history |
| 94 | silent g/hello/ |
| 95 | |
| 96 | redir @a |
| 97 | g// |
| 98 | redir END |
| 99 | |
| 100 | call assert_match('Pattern not found: hello', @a) |
| 101 | " ^~~~~ this was previously empty |
| 102 | endfunc |
| 103 | |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 104 | " Test for global command with newline character |
| 105 | func Test_global_newline() |
| 106 | new |
| 107 | call setline(1, ['foo']) |
| 108 | exe "g/foo/s/f/h/\<NL>s/o$/w/" |
| 109 | call assert_equal('how', getline(1)) |
| 110 | call setline(1, ["foo\<NL>bar"]) |
| 111 | exe "g/foo/s/foo\\\<NL>bar/xyz/" |
| 112 | call assert_equal('xyz', getline(1)) |
| 113 | close! |
| 114 | endfunc |
| 115 | |
zeertzjq | 3074137 | 2024-05-24 07:37:36 +0200 | [diff] [blame] | 116 | " Test :g with ? as delimiter. |
| 117 | func Test_global_question_delimiter() |
| 118 | new |
| 119 | call setline(1, ['aaaaa', 'b?bbb', 'ccccc', 'ddd?d', 'eeeee']) |
| 120 | g?\??delete |
| 121 | call assert_equal(['aaaaa', 'ccccc', 'eeeee'], getline(1, '$')) |
| 122 | bwipe! |
| 123 | endfunc |
| 124 | |
| 125 | func Test_global_wrong_delimiter() |
Bram Moolenaar | 419a40a | 2021-06-21 21:55:18 +0200 | [diff] [blame] | 126 | call assert_fails('g x^bxd', 'E146:') |
| 127 | endfunc |
| 128 | |
zeertzjq | 3cfae39 | 2022-07-26 17:48:13 +0100 | [diff] [blame] | 129 | " Test for interrupting :global using Ctrl-C |
| 130 | func Test_interrupt_global() |
| 131 | CheckRunVimInTerminal |
Bram Moolenaar | 859ea4b | 2022-09-27 18:05:38 +0100 | [diff] [blame] | 132 | |
zeertzjq | 3cfae39 | 2022-07-26 17:48:13 +0100 | [diff] [blame] | 133 | let lines =<< trim END |
| 134 | cnoremap ; <Cmd>sleep 10<CR> |
| 135 | call setline(1, repeat(['foo'], 5)) |
| 136 | END |
Bram Moolenaar | 572a443 | 2022-09-28 21:07:03 +0100 | [diff] [blame] | 137 | call writefile(lines, 'Xtest_interrupt_global', 'D') |
zeertzjq | 3cfae39 | 2022-07-26 17:48:13 +0100 | [diff] [blame] | 138 | let buf = RunVimInTerminal('-S Xtest_interrupt_global', {'rows': 6}) |
| 139 | |
| 140 | call term_sendkeys(buf, ":g/foo/norm :\<C-V>;\<CR>") |
| 141 | " Wait for :sleep to start |
Bram Moolenaar | 859ea4b | 2022-09-27 18:05:38 +0100 | [diff] [blame] | 142 | call TermWait(buf, 100) |
zeertzjq | 3cfae39 | 2022-07-26 17:48:13 +0100 | [diff] [blame] | 143 | call term_sendkeys(buf, "\<C-C>") |
| 144 | call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 6))}, 1000) |
| 145 | |
| 146 | " Also test in Ex mode |
| 147 | call term_sendkeys(buf, "gQg/foo/norm :\<C-V>;\<CR>") |
| 148 | " Wait for :sleep to start |
Bram Moolenaar | 859ea4b | 2022-09-27 18:05:38 +0100 | [diff] [blame] | 149 | call TermWait(buf, 100) |
zeertzjq | 3cfae39 | 2022-07-26 17:48:13 +0100 | [diff] [blame] | 150 | call term_sendkeys(buf, "\<C-C>") |
| 151 | call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 5))}, 1000) |
| 152 | |
| 153 | call StopVimInTerminal(buf) |
zeertzjq | 3cfae39 | 2022-07-26 17:48:13 +0100 | [diff] [blame] | 154 | endfunc |
| 155 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 156 | " vim: shiftwidth=2 sts=2 expandtab |