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