robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #38101 from zeertzjq/vim-9.2.00747e65c44cfb

master
3.6 KiB140 linesraw
1" Test for :global and :vglobal
2
3source check.vim
4source term_util.vim
5
6func 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))
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))
16  set clipboard&
17  bwipe!
18endfunc
19
20func 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!
28endfunc
29
30func Test_nested_global()
31  new
32  call setline(1, ['nothing', 'found', 'found bad', 'bad'])
33  call assert_fails('g/found/3v/bad/s/^/++/', 'E147')
34  g/found/v/bad/s/^/++/
35  call assert_equal(['nothing', '++found', 'found bad', 'bad'], getline(1, 4))
36  bwipe!
37endfunc
38
39func Test_global_error()
40  call assert_fails('g\\a', 'E10:')
41  call assert_fails('g', 'E148:')
42  call assert_fails('g/\(/y', 'E54:')
43endfunc
44
45" Test for printing lines using :g with different search patterns
46func 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
72  bw!
73endfunc
74
75func Test_global_empty_pattern()
76  " populate history
77  silent g/hello/
78
79  redir @a
80  g//
81  redir END
82
83  call assert_match('Pattern not found: hello', @a)
84  "                                     ^~~~~ this was previously empty
85endfunc
86
87" Test for global command with newline character
88func Test_global_newline()
89  new
90  call setline(1, ['foo'])
91  exe "g/foo/s/f/h/\<NL>s/o$/w/"
92  call assert_equal('how', getline(1))
93  call setline(1, ["foo\<NL>bar"])
94  exe "g/foo/s/foo\\\<NL>bar/xyz/"
95  call assert_equal('xyz', getline(1))
96  bw!
97endfunc
98
99" Test :g with ? as delimiter.
100func Test_global_question_delimiter()
101  new
102  call setline(1, ['aaaaa', 'b?bbb', 'ccccc', 'ddd?d', 'eeeee'])
103  g?\??delete
104  call assert_equal(['aaaaa', 'ccccc', 'eeeee'], getline(1, '$'))
105  bwipe!
106endfunc
107
108func Test_global_wrong_delimiter()
109  call assert_fails('g x^bxd', 'E146:')
110endfunc
111
112" Test for interrupting :global using Ctrl-C
113func Test_interrupt_global()
114  CheckRunVimInTerminal
115
116  let lines =<< trim END
117    cnoremap ; <Cmd>sleep 10<CR>
118    call setline(1, repeat(['foo'], 5))
119  END
120  call writefile(lines, 'Xtest_interrupt_global')
121  let buf = RunVimInTerminal('-S Xtest_interrupt_global', {'rows': 6})
122
123  call term_sendkeys(buf, ":g/foo/norm :\<C-V>;\<CR>")
124  " Wait for :sleep to start
125  call TermWait(buf, 100)
126  call term_sendkeys(buf, "\<C-C>")
127  call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 6))}, 1000)
128
129  " Also test in Ex mode
130  call term_sendkeys(buf, "gQg/foo/norm :\<C-V>;\<CR>")
131  " Wait for :sleep to start
132  call TermWait(buf, 100)
133  call term_sendkeys(buf, "\<C-C>")
134  call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 5))}, 1000)
135
136  call StopVimInTerminal(buf)
137  call delete('Xtest_interrupt_global')
138endfunc
139
140" vim: shiftwidth=2 sts=2 expandtab