blob: 4dddd0fb9cb721adbf665cd63362a90ef17c4d09 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for :global and :vglobal
2
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02003func 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 Moolenaar515545e2020-03-22 14:08:59 +01009 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 Moolenaar3fcfa352017-03-29 19:20:41 +020013 set clipboard&
14 bwipe!
15endfunc
Bram Moolenaarf84b1222017-06-10 14:29:52 +020016
Bram Moolenaar07188fc2020-06-05 20:03:16 +020017func 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!
25endfunc
26
Bram Moolenaarf84b1222017-06-10 14:29:52 +020027func Test_nested_global()
28 new
29 call setline(1, ['nothing', 'found', 'found bad', 'bad'])
Bram Moolenaare2e40752020-09-04 21:18:46 +020030 call assert_fails('g/found/3v/bad/s/^/++/', 'E147:')
Bram Moolenaarf84b1222017-06-10 14:29:52 +020031 g/found/v/bad/s/^/++/
32 call assert_equal(['nothing', '++found', 'found bad', 'bad'], getline(1, 4))
33 bwipe!
34endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010035
36func Test_global_error()
37 call assert_fails('g\\a', 'E10:')
38 call assert_fails('g', 'E148:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020039 call assert_fails('g/\(/y', 'E54:')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010040endfunc
41
Bram Moolenaarea3db912020-02-02 15:32:13 +010042" Test for printing lines using :g with different search patterns
43func 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 Moolenaar24d9c052022-03-04 21:34:31 +000069 " 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 Moolenaarea3db912020-02-02 15:32:13 +010089 close!
90endfunc
91
Rob Pillinge86190e2022-12-23 19:06:04 +000092func 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
102endfunc
103
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100104" Test for global command with newline character
105func 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!
114endfunc
115
zeertzjq30741372024-05-24 07:37:36 +0200116" Test :g with ? as delimiter.
117func 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!
123endfunc
124
125func Test_global_wrong_delimiter()
Bram Moolenaar419a40a2021-06-21 21:55:18 +0200126 call assert_fails('g x^bxd', 'E146:')
127endfunc
128
zeertzjq3cfae392022-07-26 17:48:13 +0100129" Test for interrupting :global using Ctrl-C
130func Test_interrupt_global()
131 CheckRunVimInTerminal
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100132
zeertzjq3cfae392022-07-26 17:48:13 +0100133 let lines =<< trim END
134 cnoremap ; <Cmd>sleep 10<CR>
135 call setline(1, repeat(['foo'], 5))
136 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100137 call writefile(lines, 'Xtest_interrupt_global', 'D')
zeertzjq3cfae392022-07-26 17:48:13 +0100138 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 Moolenaar859ea4b2022-09-27 18:05:38 +0100142 call TermWait(buf, 100)
zeertzjq3cfae392022-07-26 17:48:13 +0100143 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 Moolenaar859ea4b2022-09-27 18:05:38 +0100149 call TermWait(buf, 100)
zeertzjq3cfae392022-07-26 17:48:13 +0100150 call term_sendkeys(buf, "\<C-C>")
151 call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 5))}, 1000)
152
153 call StopVimInTerminal(buf)
zeertzjq3cfae392022-07-26 17:48:13 +0100154endfunc
155
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100156" vim: shiftwidth=2 sts=2 expandtab