blob: ec476c038b546c8749bbeb2bc0ade6ba4e386dc2 [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"')
39 endfor
40
41 bwipe!
42enddef
43
Bram Moolenaar94722c52023-01-28 19:19:03 +000044def Test_test_files()
45 for fname in glob('*.vim', 0, 1)
46 exe 'edit ' .. fname
47
48 # some files intentionally have misplaced white space
49 if fname =~ 'test_cindent.vim' || fname =~ 'test_join.vim'
50 continue
51 endif
52
53 # skip files that are known to have a space before a tab
54 if fname !~ 'test_comments.vim'
55 && fname !~ 'test_listchars.vim'
56 && fname !~ 'test_visual.vim'
57 cursor(1, 1)
58 var lnum = search(fname =~ "test_regexp_latin" ? '[^รก] \t' : ' \t')
59 assert_equal(0, lnum, 'testdir/' .. fname .. ': space before tab')
60 endif
61
62 # skip files that are known to have trailing white space
63 if fname !~ 'test_cmdline.vim'
64 && fname !~ 'test_let.vim'
65 && fname !~ 'test_tagjump.vim'
66 && fname !~ 'test_vim9_cmd.vim'
67 cursor(1, 1)
68 var lnum = search(
69 fname =~ 'test_vim9_assign.vim' ? '[^=]\s$'
70 : fname =~ 'test_vim9_class.vim' ? '[^)]\s$'
71 : fname =~ 'test_vim9_script.vim' ? '[^,:3]\s$'
72 : fname =~ 'test_visual.vim' ? '[^/]\s$'
73 : '[^\\]\s$')
74 assert_equal(0, lnum, 'testdir/' .. fname .. ': trailing white space')
75 endif
76 endfor
77
78 bwipe!
79enddef
80
h-eastd9509842023-02-21 13:33:17 +000081def Test_help_files()
82 var lnum: number
83 set nowrapscan
84
85 for fpath in glob('../../runtime/doc/*.txt', 0, 1)
86 exe 'edit ' .. fpath
87
88 var fname = fnamemodify(fpath, ":t")
89
90 # todo.txt is for developers, it's not need a strictly check
91 # version*.txt is a history and large size, so it's not checked
92 if fname == 'todo.txt' || fname =~ 'version.*\.txt'
93 continue
94 endif
95
96 # Check for mixed tabs and spaces
97 cursor(1, 1)
98 while 1
99 lnum = search('[^/] \t')
100 if fname == 'visual.txt' && getline(lnum) =~ "STRING \tjkl"
101 || fname == 'usr_27.txt' && getline(lnum) =~ "\[^\? \t\]"
102 continue
103 endif
104 assert_equal(0, lnum, fpath .. ': space before tab')
105 if lnum == 0
106 break
107 endif
108 endwhile
109
110 # Check for unnecessary whitespace at the end of a line
111 cursor(1, 1)
112 while 1
113 lnum = search('[^/~\\]\s$')
114 # skip line that are known to have trailing white space
115 if fname == 'map.txt' && getline(lnum) =~ "unmap @@ $"
116 || fname == 'usr_12.txt' && getline(lnum) =~ "^\t/ \t$"
117 || fname == 'usr_41.txt' && getline(lnum) =~ "map <F4> o#include $"
118 || fname == 'change.txt' && getline(lnum) =~ "foobar bla $"
119 continue
120 endif
121 assert_equal(0, lnum, fpath .. ': trailing white space')
122 if lnum == 0
123 break
124 endif
125 endwhile
126
127 # TODO: Do check and fix help files
128# # Check over 80 columns
129# cursor(1, 1)
130# while 1
131# lnum = search('\%>80v.*$')
132# assert_equal(0, lnum, fpath .. ': line over 80 columns')
133# if lnum == 0
134# break
135# endif
136# endwhile
137
138 endfor
139
140 set wrapscan&vim
141 bwipe!
142enddef
143
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000144
145" vim: shiftwidth=2 sts=2 expandtab