Initial support for Forth
[ohcount] / test / unit / parser_test.h
1 // parser_test.h written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
3
4 #include <assert.h>
5 #include <dirent.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "../../src/sourcefile.h"
11
12 SourceFile *test_parser_sourcefile(const char *language, const char *contents) {
13   SourceFile *sf = ohcount_sourcefile_new("foo");
14   ohcount_sourcefile_set_contents(sf, contents);
15   ohcount_sourcefile_set_language(sf, language);
16   return sf;
17 }
18
19 void test_parser_verify_parse(SourceFile *sf, const char *language,
20                               const char *code, const char *comments,
21                               int blanks) {
22   ohcount_sourcefile_parse(sf);
23   ParsedLanguageList *list = ohcount_sourcefile_get_parsed_language_list(sf);
24   assert(strcmp(list->head->pl->name, language) == 0);
25   assert(strcmp(list->head->pl->code, code) == 0);
26   assert(strcmp(list->head->pl->comments, comments) == 0);
27   assert(list->head->pl->blanks_count == blanks);
28   ohcount_sourcefile_free(sf);
29 }
30
31 void test_parser_verify_parse2(SourceFile *sf, const char *language,
32                                const char *code, const char *comments,
33                                int blanks, const char *language2,
34                                const char *code2, const char *comments2,
35                                int blanks2) {
36   ohcount_sourcefile_parse(sf);
37   ParsedLanguageList *list = ohcount_sourcefile_get_parsed_language_list(sf);
38   assert(strcmp(list->head->pl->name, language) == 0);
39   assert(strcmp(list->head->pl->code, code) == 0);
40   assert(strcmp(list->head->pl->comments, comments) == 0);
41   assert(list->head->pl->blanks_count == blanks);
42   assert(strcmp(list->head->next->pl->name, language2) == 0);
43   assert(strcmp(list->head->next->pl->code, code2) == 0);
44   assert(strcmp(list->head->next->pl->comments, comments2) == 0);
45   assert(list->head->next->pl->blanks_count == blanks2);
46   ohcount_sourcefile_free(sf);
47 }
48
49 typedef struct {
50   SourceFile *sf;
51   const char *entity;
52   const char *expected;
53 } TestParserEntityUData;
54
55 void test_parser_entity_callback(const char *language, const char *entity,
56                                  int start, int end, void *userdata) {
57   TestParserEntityUData *udata = (TestParserEntityUData *)userdata;
58   if (strcmp(entity, udata->entity) == 0) {
59     char *buffer = ohcount_sourcefile_get_contents(udata->sf);
60     assert(strncmp(udata->expected, buffer + start, end - start) == 0);
61   }
62 }
63
64 void test_parser_verify_entity(SourceFile *sf, const char *entity,
65                                const char *expected) {
66   TestParserEntityUData *udata = malloc(sizeof(TestParserEntityUData));
67   udata->sf = sf;
68   udata->entity = entity;
69   udata->expected = expected;
70   ohcount_sourcefile_parse_entities_with_callback(sf,
71     test_parser_entity_callback, udata);
72   ohcount_sourcefile_free(sf);
73   free(udata);
74 }
75
76 #include "parsers/test_actionscript.h"
77 #include "parsers/test_ada.h"
78 #include "parsers/test_assembler.h"
79 #include "parsers/test_autoconf.h"
80 #include "parsers/test_automake.h"
81 #include "parsers/test_awk.h"
82 #include "parsers/test_basic.h"
83 #include "parsers/test_bat.h"
84 #include "parsers/test_blitzmax.h"
85 #include "parsers/test_boo.h"
86 #include "parsers/test_brainfuck.h"
87 #include "parsers/test_bfpp.h"
88 #include "parsers/test_c.h"
89 #include "parsers/test_chaiscript.h"
90 #include "parsers/test_clearsilvertemplate.h"
91 #include "parsers/test_clearsilver.h"
92 #include "parsers/test_clojure.h"
93 #include "parsers/test_cs_aspx.h"
94 #include "parsers/test_csharp.h"
95 #include "parsers/test_css.h"
96 #include "parsers/test_d.h"
97 #include "parsers/test_dcl.h"
98 #include "parsers/test_dylan.h"
99 #include "parsers/test_ebuild.h"
100 #include "parsers/test_eiffel.h"
101 #include "parsers/test_emacs_lisp.h"
102 #include "parsers/test_erlang.h"
103 #include "parsers/test_exheres.h"
104 #include "parsers/test_factor.h"
105 #include "parsers/test_forth.h"
106 #include "parsers/test_fortran.h"
107 #include "parsers/test_fsharp.h"
108 #include "parsers/test_glsl.h"
109 #include "parsers/test_golang.h"
110 #include "parsers/test_groovy.h"
111 #include "parsers/test_haml.h"
112 #include "parsers/test_haskell.h"
113 #include "parsers/test_haxe.h"
114 #include "parsers/test_html.h"
115 #include "parsers/test_idl_pvwave.h"
116 #include "parsers/test_jam.h"
117 #include "parsers/test_java.h"
118 #include "parsers/test_javascript.h"
119 #include "parsers/test_jsp.h"
120 #include "parsers/test_lisp.h"
121 #include "parsers/test_logtalk.h"
122 #include "parsers/test_lua.h"
123 #include "parsers/test_make.h"
124 #include "parsers/test_matlab.h"
125 #include "parsers/test_metafont.h"
126 #include "parsers/test_metapost.h"
127 #include "parsers/test_mxml.h"
128 #include "parsers/test_nix.h"
129 #include "parsers/test_nsis.h"
130 #include "parsers/test_objective_j.h"
131 #include "parsers/test_ocaml.h"
132 #include "parsers/test_octave.h"
133 #include "parsers/test_pascal.h"
134 #include "parsers/test_perl.h"
135 #include "parsers/test_pike.h"
136 #include "parsers/test_puppet.h"
137 #include "parsers/test_python.h"
138 #include "parsers/test_qml.h"
139 #include "parsers/test_r.h"
140 #include "parsers/test_racket.h"
141 #include "parsers/test_rebol.h"
142 #include "parsers/test_rexx.h"
143 #include "parsers/test_rhtml.h"
144 #include "parsers/test_ruby.h"
145 #include "parsers/test_scala.h"
146 #include "parsers/test_scheme.h"
147 #include "parsers/test_scilab.h"
148 #include "parsers/test_shell.h"
149 #include "parsers/test_smalltalk.h"
150 #include "parsers/test_sql.h"
151 #include "parsers/test_stratego.h"
152 #include "parsers/test_tcl.h"
153 #include "parsers/test_tex.h"
154 #include "parsers/test_vala.h"
155 #include "parsers/test_vb_aspx.h"
156 #include "parsers/test_vhdl.h"
157 #include "parsers/test_visualbasic.h"
158 #include "parsers/test_xaml.h"
159 #include "parsers/test_xml.h"
160 #include "parsers/test_xmlschema.h"
161 #include "parsers/test_xslt.h"
162
163 typedef struct {
164   SourceFile *sf;
165   FILE *f;
166 } TestParserUData;
167
168 void test_parser_callback(const char *language, const char *entity,
169                                  int start, int end, void *userdata) {
170   TestParserUData *udata = (TestParserUData *)userdata;
171   char line[512], line2[512];
172   assert(fgets(line, sizeof(line), udata->f) != NULL);
173   if (strcmp(entity, "lcode") == 0)
174     entity = "code";
175   else if (strcmp(entity, "lcomment") == 0)
176     entity = "comment";
177   else if (strcmp(entity, "lblank") == 0)
178     entity = "blank";
179   sprintf(line2, "%s\t%s\t", language, entity);
180   char *buffer = ohcount_sourcefile_get_contents(udata->sf);
181   strncpy(line2 + strlen(language) + strlen(entity) + 2, buffer + start,
182           end - start);
183   line2[strlen(language) + strlen(entity) + 2 + (end - start)] = '\0';
184         if (strcmp(line, line2) != 0) {
185                 fprintf(stderr, "FAIL: Parser test failure in %s:\nExpected: %sGot:      %s", udata->sf->filename, line, line2);
186                 assert(strcmp(line, line2) == 0);
187         }
188 }
189
190 char *test_parser_filenames[] = { "", 0 };
191
192 void test_parser_verify_parses() {
193   const char *src_dir = "../src_dir/";
194   char src[FILENAME_MAX];
195   strncpy(src, src_dir, strlen(src_dir));
196   char *s_p = src + strlen(src_dir);
197
198   const char *expected_dir = "../expected_dir/";
199   char expected[FILENAME_MAX];
200   strncpy(expected, expected_dir, strlen(expected_dir));
201   char *e_p = expected + strlen(expected_dir);
202
203   struct dirent *file;
204   DIR *d = opendir(src_dir);
205   if (d) {
206     while ((file = readdir(d))) {
207       if (strcmp((const char *)file->d_name, ".") != 0 &&
208           strcmp((const char *)file->d_name, "..") != 0) {
209         int length = strlen(file->d_name);
210         strncpy(s_p, (const char *)file->d_name, length);
211         *(s_p + length) = '\0';
212         strncpy(e_p, (const char *)file->d_name, length);
213         *(e_p + length) = '\0';
214
215         FILE *f = fopen((const char *)expected, "rb");
216         if (f) {
217           SourceFile *sf = ohcount_sourcefile_new((const char *)src);
218
219           // Disambiguators in the detector may access the directory contents of
220           // the file in question. This could lead to mis-detections for these
221           // unit tests. By default the directory contents are set to "". If
222           // this is not desired, add your cases here.
223           if (strcmp(s_p, "visual_basic.bas") == 0)
224             // This file needs frx1.frx in the directory contents to be
225             // detected as Visual Basic.
226                                                 sf->filenames = test_basic_vb_filenames;
227           else
228                                                 sf->filenames = test_parser_filenames;
229
230           TestParserUData *udata = malloc(sizeof(TestParserUData));
231           udata->sf = sf;
232           udata->f = f;
233
234           // If the expected file not empty, then the source file should be
235           // detected as something. Un-detected files are not parsed so no
236           // failing assertions would be made, resulting in erroneously passed
237           // tests.
238           const char *language = ohcount_sourcefile_get_language(sf);
239           fseek(f, 0, SEEK_END);
240           if (ftell(f) > 0)
241             assert(language);
242           rewind(f);
243
244           ohcount_sourcefile_parse_with_callback(sf, test_parser_callback,
245                                                  (void *)udata);
246           fclose(f);
247           ohcount_sourcefile_free(sf);
248           free(udata);
249         }
250       }
251     }
252     closedir(d);
253   }
254 }
255
256 void all_parser_tests() {
257   test_parser_verify_parses();
258   all_actionscript_tests();
259   all_ada_tests();
260   all_assembler_tests();
261   all_autoconf_tests();
262   all_automake_tests();
263   all_awk_tests();
264   all_basic_tests();
265   all_bat_tests();
266   all_blitzmax_tests();
267   all_boo_tests();
268   all_brainfuck_tests();
269   all_bfpp_tests();
270   all_c_tests();
271   all_chaiscript_tests();
272   all_clearsilver_template_tests();
273   all_clearsilver_tests();
274   all_clojure_tests();
275   all_cs_aspx_tests();
276   all_csharp_tests();
277   all_css_tests();
278   all_dmd_tests();
279   all_dcl_tests();
280   all_dylan_tests();
281   all_ebuild_tests();
282   all_eiffel_tests();
283   all_emacs_lisp_tests();
284   all_erlang_tests();
285   all_exheres_tests();
286   all_factor_tests();
287   all_forth_tests();
288   all_fortran_tests();
289   all_fsharp_tests();
290   all_glsl_tests();
291   all_groovy_tests();
292   all_haml_tests();
293   all_haskell_tests();
294   all_haxe_tests();
295   all_html_tests();
296   all_idl_pvwave_tests();
297   all_jam_tests();
298   all_java_tests();
299   all_javascript_tests();
300   all_jsp_tests();
301   all_lisp_tests();
302   all_logtalk_tests();
303   all_lua_tests();
304   all_make_tests();
305   all_matlab_tests();
306   all_metafont_tests();
307   all_metapost_tests();
308   all_mxml_tests();
309   all_nix_tests();
310   all_nsis_tests();
311   all_objective_j_tests();
312   all_ocaml_tests();
313   all_octave_tests();
314   all_pascal_tests();
315   all_perl_tests();
316   all_pike_tests();
317   all_python_tests();
318   all_r_tests();
319   all_racket_tests();
320   all_rebol_tests();
321   all_rexx_tests();
322   all_rhtml_tests();
323   all_ruby_tests();
324   all_scala_tests();
325   all_scheme_tests();
326   all_scilab_tests();
327   all_shell_tests();
328   all_smalltalk_tests();
329   all_sql_tests();
330   all_stratego_tests();
331   all_tcl_tests();
332   all_tex_tests();
333   all_vala_tests();
334   all_vb_aspx_tests();
335   all_vhdl_tests();
336   all_visualbasic_tests();
337   all_xaml_tests();
338   all_xml_tests();
339   all_xmlschema_tests();
340   all_xslt_tests();
341 }