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