neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_smartindent.vim (3703B)


      1 " Tests for smartindent
      2 
      3 " Tests for not doing smart indenting when it isn't set.
      4 func 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
     15 endfunc
     16 
     17 func MyIndent()
     18 endfunc
     19 
     20 " When 'indentexpr' is set, setting 'si' has no effect.
     21 func 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!
     34 endfunc
     35 
     36 " Test for inserting '{' and '} with smartindent
     37 func 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!
     56 endfunc
     57 
     58 " Test for adding a new line before and after comments with smartindent
     59 func 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!
     67 endfunc
     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.
     71 func 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!
     79 endfunc
     80 
     81 " Test for indenting a statement after a if condition split across lines
     82 func 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!
     89 endfunc
     90 
     91 " Test for inserting lines before and after a one line comment
     92 func 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!
    101 endfunc
    102 
    103 " Test for smartindent with a comment continued across multiple lines
    104 func 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!
    112 endfunc
    113 
    114 func Test_si_after_completion()
    115  new
    116  setlocal ai smartindent indentexpr=
    117  call setline(1, 'foo foot')
    118  call feedkeys("o  f\<C-X>\<C-N>#", 'tx')
    119  call assert_equal('  foo#', getline(2))
    120 
    121  call setline(2, '')
    122  call feedkeys("1Go  f\<C-X>\<C-N>}", 'tx')
    123  call assert_equal('  foo}', getline(2))
    124 
    125  bwipe!
    126 endfunc
    127 
    128 func Test_no_si_after_completion()
    129  new
    130  call setline(1, 'foo foot')
    131  call feedkeys("o  f\<C-X>\<C-N>#", 'tx')
    132  call assert_equal('  foo#', getline(2))
    133  bwipe!
    134 endfunc
    135 
    136 " vim: shiftwidth=2 sts=2 expandtab