test_smartindent.vim 3.6 KB

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