1 // parser_test.h written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
10 #include "../../src/sourcefile.h"
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);
19 void test_parser_verify_parse(SourceFile *sf, const char *language,
20 const char *code, const char *comments,
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);
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,
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);
53 } TestParserEntityUData;
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);
64 void test_parser_verify_entity(SourceFile *sf, const char *entity,
65 const char *expected) {
66 TestParserEntityUData *udata = malloc(sizeof(TestParserEntityUData));
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);
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"
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)
160 else if (strcmp(entity, "lcomment") == 0)
162 else if (strcmp(entity, "lblank") == 0)
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,
168 line2[strlen(language) + strlen(entity) + 2 + (end - start)] = '\0';
169 assert(strcmp(line, line2) == 0);
172 char *test_parser_filenames[] = { "", 0 };
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);
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);
186 DIR *d = opendir(src_dir);
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';
197 FILE *f = fopen((const char *)expected, "r");
199 SourceFile *sf = ohcount_sourcefile_new((const char *)src);
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);
210 ohcount_sourcefile_set_filenames(sf, test_parser_filenames);
212 TestParserUData *udata = malloc(sizeof(TestParserUData));
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
220 const char *language = ohcount_sourcefile_get_language(sf);
221 fseek(f, 0, SEEK_END);
226 ohcount_sourcefile_parse_with_callback(sf, test_parser_callback,
229 ohcount_sourcefile_free(sf);
238 void all_parser_tests() {
239 test_parser_verify_parses();
240 all_actionscript_tests();
242 all_assembler_tests();
243 all_autoconf_tests();
244 all_automake_tests();
248 all_blitzmax_tests();
251 all_clearsilver_template_tests();
252 all_clearsilver_tests();
261 all_emacs_lisp_tests();
273 all_javascript_tests();
279 all_metafont_tests();
280 all_metapost_tests();
283 all_objective_j_tests();
298 all_smalltalk_tests();
300 all_stratego_tests();
306 all_visualbasic_tests();
309 all_xmlschema_tests();