blob: 62f588afb8e6b646fe0fd0f96935aec37ff95404 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for :global and :vglobal
2
Bram Moolenaar07188fc2020-06-05 20:03:16 +02003source check.vim
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02004
5func Test_yank_put_clipboard()
6 new
7 call setline(1, ['a', 'b', 'c'])
8 set clipboard=unnamed
9 g/^/normal yyp
10 call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6))
Bram Moolenaar515545e2020-03-22 14:08:59 +010011 set clipboard=unnamed,unnamedplus
12 call setline(1, ['a', 'b', 'c'])
13 g/^/normal yyp
14 call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6))
Bram Moolenaar3fcfa352017-03-29 19:20:41 +020015 set clipboard&
16 bwipe!
17endfunc
Bram Moolenaarf84b1222017-06-10 14:29:52 +020018
Bram Moolenaar07188fc2020-06-05 20:03:16 +020019func Test_global_set_clipboard()
20 CheckFeature clipboard_working
21 new
22 set clipboard=unnamedplus
23 let @+='clipboard' | g/^/set cb= | let @" = 'unnamed' | put
24 call assert_equal(['','unnamed'], getline(1, '$'))
25 set clipboard&
26 bwipe!
27endfunc
28
Bram Moolenaarf84b1222017-06-10 14:29:52 +020029func Test_nested_global()
30 new
31 call setline(1, ['nothing', 'found', 'found bad', 'bad'])
Bram Moolenaare2e40752020-09-04 21:18:46 +020032 call assert_fails('g/found/3v/bad/s/^/++/', 'E147:')
Bram Moolenaarf84b1222017-06-10 14:29:52 +020033 g/found/v/bad/s/^/++/
34 call assert_equal(['nothing', '++found', 'found bad', 'bad'], getline(1, 4))
35 bwipe!
36endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010037
38func Test_global_error()
39 call assert_fails('g\\a', 'E10:')
40 call assert_fails('g', 'E148:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020041 call assert_fails('g/\(/y', 'E54:')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010042endfunc
43
Bram Moolenaarea3db912020-02-02 15:32:13 +010044" Test for printing lines using :g with different search patterns
45func Test_global_print()
46 new
47 call setline(1, ['foo', 'bar', 'foo', 'foo'])
48 let @/ = 'foo'
49 let t = execute("g/")->trim()->split("\n")
50 call assert_equal(['foo', 'foo', 'foo'], t)
51
52 " Test for Vi compatible patterns
53 let @/ = 'bar'
54 let t = execute('g\/')->trim()->split("\n")
55 call assert_equal(['bar'], t)
56
57 normal gg
58 s/foo/foo/
59 let t = execute('g\&')->trim()->split("\n")
60 call assert_equal(['foo', 'foo', 'foo'], t)
61
62 let @/ = 'bar'
63 let t = execute('g?')->trim()->split("\n")
64 call assert_equal(['bar'], t)
65
66 " Test for the 'Pattern found in every line' message
67 let v:statusmsg = ''
68 v/foo\|bar/p
69 call assert_notequal('', v:statusmsg)
70
Bram Moolenaar24d9c052022-03-04 21:34:31 +000071 " In Vim9 script this is an error
72 let caught = 'no'
73 try
74 vim9cmd v/foo\|bar/p
75 catch /E538/
76 let caught = 'yes'
77 call assert_match('E538: Pattern found in every line: foo\|bar', v:exception)
78 endtry
79 call assert_equal('yes', caught)
80
81 " In Vim9 script not matching is an error
82 let caught = 'no'
83 try
84 vim9cmd g/foobarnotfound/p
85 catch /E486/
86 let caught = 'yes'
87 call assert_match('E486: Pattern not found: foobarnotfound', v:exception)
88 endtry
89 call assert_equal('yes', caught)
90
Bram Moolenaarea3db912020-02-02 15:32:13 +010091 close!
92endfunc
93
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010094" Test for global command with newline character
95func Test_global_newline()
96 new
97 call setline(1, ['foo'])
98 exe "g/foo/s/f/h/\<NL>s/o$/w/"
99 call assert_equal('how', getline(1))
100 call setline(1, ["foo\<NL>bar"])
101 exe "g/foo/s/foo\\\<NL>bar/xyz/"
102 call assert_equal('xyz', getline(1))
103 close!
104endfunc
105
Bram Moolenaar419a40a2021-06-21 21:55:18 +0200106func Test_wrong_delimiter()
107 call assert_fails('g x^bxd', 'E146:')
108endfunc
109
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100110" vim: shiftwidth=2 sts=2 expandtab