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