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_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"
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)
175 else if (strcmp(entity, "lcomment") == 0)
177 else if (strcmp(entity, "lblank") == 0)
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,
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);
190 char *test_parser_filenames[] = { "", 0 };
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);
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);
204 DIR *d = opendir(src_dir);
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';
215 FILE *f = fopen((const char *)expected, "rb");
217 SourceFile *sf = ohcount_sourcefile_new((const char *)src);
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;
228 sf->filenames = test_parser_filenames;
230 TestParserUData *udata = malloc(sizeof(TestParserUData));
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
238 const char *language = ohcount_sourcefile_get_language(sf);
239 fseek(f, 0, SEEK_END);
244 ohcount_sourcefile_parse_with_callback(sf, test_parser_callback,
247 ohcount_sourcefile_free(sf);
256 void all_parser_tests() {
257 test_parser_verify_parses();
258 all_actionscript_tests();
260 all_assembler_tests();
261 all_autoconf_tests();
262 all_automake_tests();
266 all_blitzmax_tests();
268 all_brainfuck_tests();
271 all_chaiscript_tests();
272 all_clearsilver_template_tests();
273 all_clearsilver_tests();
283 all_emacs_lisp_tests();
296 all_idl_pvwave_tests();
299 all_javascript_tests();
306 all_metafont_tests();
307 all_metapost_tests();
311 all_objective_j_tests();
328 all_smalltalk_tests();
330 all_stratego_tests();
336 all_visualbasic_tests();
339 all_xmlschema_tests();