blob: bf101049849ef8bb6fb6294fc5914500ed1739f0 [file] [log] [blame]
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001" Test editing line in Ex mode (see :help Q and :help gQ).
2
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01003source check.vim
4
Bram Moolenaar59cb0412019-12-18 22:26:31 +01005" Helper function to test editing line in Q Ex mode
6func Ex_Q(cmd)
7 " Is there a simpler way to test editing Ex line?
8 call feedkeys("Q"
9 \ .. "let s:test_ex =<< END\<CR>"
10 \ .. a:cmd .. "\<CR>"
11 \ .. "END\<CR>"
12 \ .. "visual\<CR>", 'tx')
13 return s:test_ex[0]
14endfunc
15
16" Helper function to test editing line in gQ Ex mode
17func Ex_gQ(cmd)
18 call feedkeys("gQ" .. a:cmd .. "\<C-b>\"\<CR>", 'tx')
19 let ret = @:[1:] " Remove leading quote.
20 call feedkeys("visual\<CR>", 'tx')
21 return ret
22endfunc
23
24" Helper function to test editing line with both Q and gQ Ex mode.
25func Ex(cmd)
26 return [Ex_Q(a:cmd), Ex_gQ(a:cmd)]
27endfunc
28
29" Test editing line in Ex mode (both Q and gQ)
30func Test_ex_mode()
31 let encoding_save = &encoding
32 set sw=2
33
34 for e in ['utf8', 'latin1']
35 exe 'set encoding=' . e
36
37 call assert_equal(['bar', 'bar'], Ex("foo bar\<C-u>bar"), e)
38 call assert_equal(["1\<C-u>2", "1\<C-u>2"], Ex("1\<C-v>\<C-u>2"), e)
39 call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e)
40 call assert_equal(['0123', '2013'], Ex("01\<Home>2\<End>3"), e)
41 call assert_equal(['0123', '0213'], Ex("01\<Left>2\<Right>3"), e)
42 call assert_equal(['01234', '0342'], Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e)
43 call assert_equal(["foo bar\<C-w>", 'foo '], Ex("foo bar\<C-w>"), e)
44 call assert_equal(['foo', 'foo'], Ex("fooba\<Del>\<Del>"), e)
45 call assert_equal(["foo\tbar", 'foobar'], Ex("foo\<Tab>bar"), e)
46 call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e)
47 call assert_equal([' 1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e)
48 call assert_equal([' 1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>\<C-d>"), e)
49 call assert_equal([' foo', ' foo'], Ex(" foo\<C-d>"), e)
50 call assert_equal(['foo', ' foo0'], Ex(" foo0\<C-d>"), e)
51 call assert_equal(['foo', ' foo^'], Ex(" foo^\<C-d>"), e)
52 endfor
53
54 set sw&
55 let &encoding = encoding_save
56endfunc
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010057
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010058" Test substitute confirmation prompt :%s/pat/str/c in Ex mode
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010059func Test_Ex_substitute()
60 CheckRunVimInTerminal
61 let buf = RunVimInTerminal('', {'rows': 6})
62
63 call term_sendkeys(buf, ":call setline(1, ['foo foo', 'foo foo', 'foo foo'])\<CR>")
64 call term_sendkeys(buf, ":set number\<CR>")
65 call term_sendkeys(buf, "gQ")
66 call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
67
68 call term_sendkeys(buf, "%s/foo/bar/gc\<CR>")
69 call WaitForAssert({-> assert_match(' 1 foo foo', term_getline(buf, 5))},
70 \ 1000)
71 call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000)
72 call term_sendkeys(buf, "n\<CR>")
73 call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))},
74 \ 1000)
75 call term_sendkeys(buf, "y\<CR>")
76
77 call term_sendkeys(buf, "q\<CR>")
78 call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
79
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010080 " Pressing enter in ex mode should print the current line
81 call term_sendkeys(buf, "\<CR>")
82 call WaitForAssert({-> assert_match(' 3 foo foo',
83 \ term_getline(buf, 5))}, 1000)
84
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010085 call term_sendkeys(buf, ":vi\<CR>")
86 call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000)
87
88 call term_sendkeys(buf, ":q!\n")
89 call StopVimInTerminal(buf)
90endfunc
91
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010092" Test for displaying lines from an empty buffer in Ex mode
93func Test_Ex_emptybuf()
94 new
95 call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E749:')
96 call setline(1, "abc")
97 call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:')
98 call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:')
99 close!
100endfunc
101
102" Test for the :open command
103func Test_open_command()
104 new
105 call setline(1, ['foo foo', 'foo bar', 'foo baz'])
106 call feedkeys("Qopen\<CR>j", 'xt')
107 call assert_equal('foo bar', getline('.'))
108 call feedkeys("Qopen /bar/\<CR>", 'xt')
109 call assert_equal(5, col('.'))
110 call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:')
111 close!
112endfunc
113
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100114" vim: shiftwidth=2 sts=2 expandtab