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