Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 1 | " Test for checking the source code style. |
| 2 | |
| 3 | def Test_source_files() |
| 4 | for fname in glob('../*.[ch]', 0, 1) |
Bram Moolenaar | bf63011 | 2023-05-19 21:41:02 +0100 | [diff] [blame] | 5 | bwipe! |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 6 | exe 'edit ' .. fname |
| 7 | |
| 8 | cursor(1, 1) |
| 9 | var lnum = search(' \t') |
| 10 | assert_equal(0, lnum, fname .. ': space before tab') |
| 11 | |
| 12 | cursor(1, 1) |
| 13 | lnum = search('\s$') |
| 14 | assert_equal(0, lnum, fname .. ': trailing white space') |
| 15 | |
| 16 | # some files don't stick to the Vim style rules |
| 17 | if fname =~ 'iscygpty.c' |
| 18 | continue |
| 19 | endif |
| 20 | |
| 21 | # Examples in comments use "condition) {", skip them. |
| 22 | # Skip if a double quote or digit comes after the "{". |
| 23 | # Skip specific string used in os_unix.c. |
| 24 | # Also skip fold markers. |
| 25 | var skip = 'getline(".") =~ "condition) {" || getline(".") =~ "vimglob_func" || getline(".") =~ "{\"" || getline(".") =~ "{\\d" || getline(".") =~ "{{{"' |
| 26 | cursor(1, 1) |
| 27 | lnum = search(')\s*{', '', 0, 0, skip) |
| 28 | assert_equal(0, lnum, fname .. ': curly after closing paren') |
| 29 | |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 30 | # Examples in comments use double quotes. |
| 31 | skip = "getline('.') =~ '\"'" |
Bram Moolenaar | bf63011 | 2023-05-19 21:41:02 +0100 | [diff] [blame] | 32 | |
| 33 | cursor(1, 1) |
| 34 | lnum = search('}\s*else', '', 0, 0, skip) |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 35 | assert_equal(0, lnum, fname .. ': curly before "else"') |
| 36 | |
| 37 | cursor(1, 1) |
| 38 | lnum = search('else\s*{', '', 0, 0, skip) |
| 39 | assert_equal(0, lnum, fname .. ': curly after "else"') |
Bram Moolenaar | c9471b1 | 2023-05-09 15:00:00 +0100 | [diff] [blame] | 40 | |
| 41 | cursor(1, 1) |
| 42 | lnum = search('\<\(if\|while\|for\)(', '', 0, 0, skip) |
| 43 | assert_equal(0, lnum, fname .. ': missing white space after "if"/"while"/"for"') |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 44 | endfor |
| 45 | |
| 46 | bwipe! |
| 47 | enddef |
| 48 | |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 49 | def Test_test_files() |
| 50 | for fname in glob('*.vim', 0, 1) |
| 51 | exe 'edit ' .. fname |
| 52 | |
| 53 | # some files intentionally have misplaced white space |
| 54 | if fname =~ 'test_cindent.vim' || fname =~ 'test_join.vim' |
| 55 | continue |
| 56 | endif |
| 57 | |
| 58 | # skip files that are known to have a space before a tab |
| 59 | if fname !~ 'test_comments.vim' |
| 60 | && fname !~ 'test_listchars.vim' |
| 61 | && fname !~ 'test_visual.vim' |
| 62 | cursor(1, 1) |
| 63 | var lnum = search(fname =~ "test_regexp_latin" ? '[^รก] \t' : ' \t') |
| 64 | assert_equal(0, lnum, 'testdir/' .. fname .. ': space before tab') |
| 65 | endif |
| 66 | |
| 67 | # skip files that are known to have trailing white space |
| 68 | if fname !~ 'test_cmdline.vim' |
| 69 | && fname !~ 'test_let.vim' |
| 70 | && fname !~ 'test_tagjump.vim' |
| 71 | && fname !~ 'test_vim9_cmd.vim' |
| 72 | cursor(1, 1) |
| 73 | var lnum = search( |
| 74 | fname =~ 'test_vim9_assign.vim' ? '[^=]\s$' |
| 75 | : fname =~ 'test_vim9_class.vim' ? '[^)]\s$' |
| 76 | : fname =~ 'test_vim9_script.vim' ? '[^,:3]\s$' |
| 77 | : fname =~ 'test_visual.vim' ? '[^/]\s$' |
| 78 | : '[^\\]\s$') |
| 79 | assert_equal(0, lnum, 'testdir/' .. fname .. ': trailing white space') |
| 80 | endif |
| 81 | endfor |
| 82 | |
| 83 | bwipe! |
| 84 | enddef |
| 85 | |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 86 | def Test_help_files() |
| 87 | var lnum: number |
| 88 | set nowrapscan |
| 89 | |
| 90 | for fpath in glob('../../runtime/doc/*.txt', 0, 1) |
| 91 | exe 'edit ' .. fpath |
| 92 | |
| 93 | var fname = fnamemodify(fpath, ":t") |
| 94 | |
| 95 | # todo.txt is for developers, it's not need a strictly check |
| 96 | # version*.txt is a history and large size, so it's not checked |
| 97 | if fname == 'todo.txt' || fname =~ 'version.*\.txt' |
| 98 | continue |
| 99 | endif |
| 100 | |
| 101 | # Check for mixed tabs and spaces |
| 102 | cursor(1, 1) |
| 103 | while 1 |
| 104 | lnum = search('[^/] \t') |
| 105 | if fname == 'visual.txt' && getline(lnum) =~ "STRING \tjkl" |
| 106 | || fname == 'usr_27.txt' && getline(lnum) =~ "\[^\? \t\]" |
| 107 | continue |
| 108 | endif |
| 109 | assert_equal(0, lnum, fpath .. ': space before tab') |
| 110 | if lnum == 0 |
| 111 | break |
| 112 | endif |
| 113 | endwhile |
| 114 | |
| 115 | # Check for unnecessary whitespace at the end of a line |
| 116 | cursor(1, 1) |
| 117 | while 1 |
| 118 | lnum = search('[^/~\\]\s$') |
| 119 | # skip line that are known to have trailing white space |
| 120 | if fname == 'map.txt' && getline(lnum) =~ "unmap @@ $" |
| 121 | || fname == 'usr_12.txt' && getline(lnum) =~ "^\t/ \t$" |
| 122 | || fname == 'usr_41.txt' && getline(lnum) =~ "map <F4> o#include $" |
| 123 | || fname == 'change.txt' && getline(lnum) =~ "foobar bla $" |
| 124 | continue |
| 125 | endif |
| 126 | assert_equal(0, lnum, fpath .. ': trailing white space') |
| 127 | if lnum == 0 |
| 128 | break |
| 129 | endif |
| 130 | endwhile |
| 131 | |
| 132 | # TODO: Do check and fix help files |
| 133 | # # Check over 80 columns |
| 134 | # cursor(1, 1) |
| 135 | # while 1 |
| 136 | # lnum = search('\%>80v.*$') |
| 137 | # assert_equal(0, lnum, fpath .. ': line over 80 columns') |
| 138 | # if lnum == 0 |
| 139 | # break |
| 140 | # endif |
| 141 | # endwhile |
| 142 | |
| 143 | endfor |
| 144 | |
| 145 | set wrapscan&vim |
| 146 | bwipe! |
| 147 | enddef |
| 148 | |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 149 | |
| 150 | " vim: shiftwidth=2 sts=2 expandtab |