1" Tests for smartindent 2 3" Tests for not doing smart indenting when it isn't set. 4func Test_nosmartindent() 5 new 6 call append(0, [" some test text", 7 \ " test text", 8 \ "test text", 9 \ " test text"]) 10 set nocindent nosmartindent autoindent 11 exe "normal! gg/some\<CR>" 12 exe "normal! 2cc#test\<Esc>" 13 call assert_equal(" #test", getline(1)) 14 enew! | close 15endfunc 16 17func MyIndent() 18endfunc 19 20" When 'indentexpr' is set, setting 'si' has no effect. 21func Test_smartindent_has_no_effect() 22 new 23 exe "normal! i\<Tab>one\<Esc>" 24 setlocal noautoindent smartindent indentexpr= 25 exe "normal! Gotwo\<Esc>" 26 call assert_equal("\ttwo", getline("$")) 27 28 set indentexpr=MyIndent 29 exe "normal! Gothree\<Esc>" 30 call assert_equal("three", getline("$")) 31 32 delfunction! MyIndent 33 bwipe! 34endfunc 35 36" Test for inserting '{' and '} with smartindent 37func Test_smartindent_braces() 38 new 39 setlocal smartindent shiftwidth=4 40 call setline(1, [' if (a)', "\tif (b)", "\t return 1"]) 41 normal 2ggO{ 42 normal 3ggA { 43 normal 4ggo} 44 normal o} 45 normal 4ggO#define FOO 1 46 call assert_equal([ 47 \ ' if (a)', 48 \ ' {', 49 \ "\tif (b) {", 50 \ '#define FOO 1', 51 \ "\t return 1", 52 \ "\t}", 53 \ ' }' 54 \ ], getline(1, '$')) 55 close! 56endfunc 57 58" Test for adding a new line before and after comments with smartindent 59func Test_si_add_line_around_comment() 60 new 61 setlocal smartindent shiftwidth=4 62 call setline(1, [' A', '# comment1', '# comment2']) 63 exe "normal GoC\<Esc>2GOB" 64 call assert_equal([' A', ' B', '# comment1', '# comment2', ' C'], 65 \ getline(1, '$')) 66 close! 67endfunc 68 69" After a C style comment, indent for a following line should line up with the 70" line containing the start of the comment. 71func Test_si_indent_after_c_comment() 72 new 73 setlocal smartindent shiftwidth=4 fo+=ro 74 exe "normal i\<C-t>/*\ncomment\n/\n#define FOOBAR\n75\<Esc>ggOabc" 75 normal 3jOcont 76 call assert_equal([' abc', ' /*', ' * comment', ' * cont', 77 \ ' */', '#define FOOBAR', ' 75'], getline(1, '$')) 78 close! 79endfunc 80 81" Test for indenting a statement after a if condition split across lines 82func Test_si_if_cond_split_across_lines() 83 new 84 setlocal smartindent shiftwidth=4 85 exe "normal i\<C-t>if (cond1 &&\n\<C-t>cond2) {\ni = 10;\n}" 86 call assert_equal([' if (cond1 &&', "\t cond2) {", "\ti = 10;", 87 \ ' }'], getline(1, '$')) 88 close! 89endfunc 90 91" Test for inserting lines before and after a one line comment 92func Test_si_one_line_comment() 93 new 94 setlocal smartindent shiftwidth=4 95 exe "normal i\<C-t>abc;\n\<C-t>/* comment */" 96 normal oi = 10; 97 normal kOj = 1; 98 call assert_equal([' abc;', "\tj = 1;", "\t/* comment */", "\ti = 10;"], 99 \ getline(1, '$')) 100 close! 101endfunc 102 103" Test for smartindent with a comment continued across multiple lines 104func Test_si_comment_line_continuation() 105 new 106 setlocal smartindent shiftwidth=4 107 call setline(1, ['# com1', '# com2 \', ' contd', '# com3', ' xyz']) 108 normal ggOabc 109 call assert_equal([' abc', '# com1', '# com2 \', ' contd', '# com3', 110 \ ' xyz'], getline(1, '$')) 111 close! 112endfunc 113 114" When 'paste' is set, 'smartindent' should not take effect. 115func Test_si_with_paste() 116 new 117 setlocal smartindent autoindent 118 set paste 119 " insert text that will trigger smartindent 120 exe "norm! i {\nif (x)\ni = 1;\n#define FOO 1\nj = 2;\n}" 121 exe "norm! Ok = 3;" 122 exe "norm! 4G>>" 123 call assert_equal([' {', 'if (x)', 'i = 1;', '#define FOO 1', 124 \ 'j = 2;', 'k = 3;', '}'], getline(1, '$')) 125 call assert_true(&smartindent) 126 set nopaste 127 %d _ 128 exe "norm! i {\nif (x)\ni = 1;\n#define FOO 1\nj = 2;\n}" 129 exe "norm! Ok = 3;" 130 exe "norm! 4G>>" 131 call assert_equal([' {', "\t if (x)", "\t\t i = 1;", 132 \ '#define FOO 1', "\t\t j = 2;", "\t k = 3;", ' }'], 133 \ getline(1, '$')) 134 bw! 135endfunc 136 137" vim: shiftwidth=2 sts=2 expandtab 138