2 * Name : test_tokenizer
3 * Author : Chris Koeritz
4 * Purpose: Puts the variable_tokenizer through some paces.
6 * Copyright (c) 1998-$now By Author. This program is free software; you can *
7 * redistribute it and/or modify it under the terms of the GNU General Public *
8 * License as published by the Free Software Foundation; either version 2 of *
9 * the License or (at your option) any later version. This is online at: *
10 * http://www.fsf.org/copyleft/gpl.html *
11 * Please send any updates to: fred@gruntose.com *
14 #include <application/hoople_main.h>
15 #include <basis/astring.h>
16 #include <basis/byte_array.h>
17 #include <basis/functions.h>
18 #include <basis/guards.h>
19 #include <configuration/variable_tokenizer.h>
20 #include <filesystem/byte_filer.h>
21 #include <structures/static_memory_gremlin.h>
22 #include <structures/string_table.h>
23 #include <textual/parser_bits.h>
24 #include <unit_test/unit_base.h>
26 using namespace application;
27 using namespace basis;
28 using namespace configuration;
29 using namespace filesystem;
30 using namespace loggers;
31 using namespace mathematics;
32 using namespace structures;
33 using namespace textual;
34 using namespace timely;
35 using namespace unit_test;
37 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
39 const int MAX_LINE_SIZE = 1000;
40 // the largest line we will deal with in a file.
42 class test_tokenizer : public virtual unit_base, public virtual application_shell
46 DEFINE_CLASS_NAME("test_tokenizer");
47 virtual int execute();
52 int test_tokenizer::execute()
56 astring test_set_1 = "\n\
60 57 chevy heap=\"16 anagrams of misty immediately\"\n\
62 shouldus havus assignmentum=\n\
63 above better be parsed = 1\n\
64 ;and this comment too yo\n\
67 astring TEST = "First Test: ";
68 astring testing = test_set_1;
69 LOG(astring("file before parsing:") + testing);
70 variable_tokenizer jed("\n\r", "=");
71 ASSERT_TRUE(jed.parse(testing), TEST + "jed should be parseable");
74 variable_tokenizer gorp("\n\r", "=");
75 ASSERT_TRUE(gorp.parse(out), TEST + "gorp should be parseable");
76 LOG(astring("file after parsing:") + out);
77 LOG("and in tabular form:");
78 LOG(jed.table().text_form());
80 //for (int i = 0; i < gorp.table().symbols(); i++) {
81 //astring name, value;
82 //gorp.table().retrieve(i, name, value);
83 //LOG(a_sprintf("item %d: name=\"%s\" value=\"%s\"", i, name.s(), value.s()));
86 ASSERT_TRUE(jed.exists("[frederick]"), TEST + "jed section header was omitted!");
87 ASSERT_EQUAL(jed.find("[frederick]"), astring(""),
88 TEST + "jed section header had unexpected contents!");
89 ASSERT_EQUAL(jed.find("ted"), astring("agent 12"),
90 TEST + "jed's ted is missing or invalid!");
91 ASSERT_FALSE(jed.find("shouldus havus assignmentum").t(),
92 TEST + "jed's shouldus had contents but shouldn't!");
93 astring value = *jed.table().find("shouldus havus assignmentum");
94 ASSERT_EQUAL(value, astring(" "), TEST + "jed shouldus had wrong contents, not special!");
95 ASSERT_TRUE(gorp.exists("[frederick]"), TEST + "gorp section header was omitted!");
96 ASSERT_EQUAL(gorp.find("[frederick]"), astring(""),
97 TEST + "gorp section header had unexpected contents!");
98 ASSERT_EQUAL(gorp.find("ted"), astring("agent 12"),
99 TEST + "gorp's ted is missing or invalid!");
100 ASSERT_FALSE(gorp.find("shouldus havus assignmentum").t(),
101 TEST + "gorp's shouldus had contents but shouldn't!");
102 value = *gorp.table().find("shouldus havus assignmentum");
103 ASSERT_EQUAL(value, astring(" "), TEST + "gorp shouldus had wrong contents, not special!");
106 astring test_set_2 = "Name=SRV, Parent=, Persist=Y, Entry=Y, Required=Y, Desc=Server, Tbl=Server";
108 astring TEST = "Second Test: ";
109 astring testing = test_set_2;
110 LOG(astring("file before parsing:") + testing);
111 variable_tokenizer jed(",", "=");
112 ASSERT_TRUE(jed.parse(testing), TEST + "jed should be parseable");
115 LOG(astring("file after parsing:") + out);
116 LOG("and in tabular form:");
117 LOG(jed.table().text_form());
118 ASSERT_EQUAL(jed.find("Name"), astring("SRV"), TEST + "Name is missing or invalid!");
119 ASSERT_FALSE(jed.find("Parent").t(), TEST + "Parent had contents but shouldn't!");
120 astring value = *jed.table().find("Parent");
121 ASSERT_EQUAL(value, astring(" "), TEST + "Parent had wrong contents, not special!");
122 ASSERT_EQUAL(jed.find("Persist"), astring("Y"), TEST + "Persist is missing or invalid!");
126 astring test_set_3 = "\n\
129 tantalus rex=gumby \"don#t\n\n'play'\nthat\" homey '\n\ndog\n\n yo \"\ncreen\" arf'\n\
130 57 chevy heap=\"16 anagrams of misty immediately\"\n\
132 shouldus havus assignmentum=\n\
133 above better be parsed = 1\n\
134 ;and this comment too yo\n\
137 astring TEST = "Third Test: ";
138 astring testing = test_set_3;
139 LOG(astring("file before parsing:") + testing);
140 variable_tokenizer jed("\n\r", "=", "\'\"");
141 ASSERT_TRUE(jed.parse(testing), TEST + "jed should be parseable");
144 variable_tokenizer gorp("\n\r", "=", "\'\"");
145 ASSERT_TRUE(gorp.parse(out), TEST + "gorp should be parseable");
146 LOG(astring("file after parsing:") + out);
147 LOG("and in tabular form:");
148 LOG(jed.table().text_form());
149 ASSERT_TRUE(jed.exists("[frederick]"), TEST + "jed section header was omitted!");
150 ASSERT_EQUAL(jed.find("[frederick]"), astring(""),
151 TEST + "jed section header had unexpected contents!");
152 ASSERT_EQUAL(jed.find("ted"), astring("agent 12"), TEST + "jed ted is missing or invalid!");
153 ASSERT_FALSE(jed.find("shouldus havus assignmentum").t(),
154 TEST + "jed shouldus had contents but shouldn't!");
155 astring value = *jed.table().find("shouldus havus assignmentum");
156 ASSERT_EQUAL(value, astring(" "), TEST + "jed shouldus had wrong contents, not special!");
157 ASSERT_TRUE(gorp.exists("[frederick]"), TEST + "gorp section header was omitted!");
158 ASSERT_EQUAL(gorp.find("[frederick]"), astring(""),
159 TEST + "gorp section header had unexpected contents!");
160 ASSERT_EQUAL(gorp.find("ted"), astring("agent 12"), TEST + "gorp second ted is missing or invalid!");
161 ASSERT_FALSE(gorp.find("shouldus havus assignmentum").t(),
162 TEST + "gorp shouldus had contents but shouldn't!");
163 value = *gorp.table().find("shouldus havus assignmentum");
164 ASSERT_EQUAL(value, astring(" "), TEST + "gorp shouldus wrong contents, was not special!");
165 ASSERT_TRUE(gorp.exists("tantalus rex"), TEST + "gorp tantalus rex is missing!");
166 ASSERT_EQUAL(gorp.find("tantalus rex"),
167 astring("gumby \"don#t\n\n'play'\nthat\" homey '\n\ndog\n\n yo "
169 TEST + "gorp tantalus rex has incorrect contents!");
172 astring test_set_4 = "\n\
174 treadmill=\"this ain't the place\nwhere'n we been done\nseein' no quotes\"\n\
175 borfulate='similarly \"we\" do not like\nthe \" quote \" type thing here'\n\
178 astring TEST = "Fourth Test: ";
179 astring testing = test_set_4;
180 LOG(astring("file before parsing:\n") + testing);
181 variable_tokenizer jed("\n\r", "=", "\'\"", false);
182 ASSERT_TRUE(jed.parse(testing), TEST + "jed should be parseable");
185 variable_tokenizer gorp("\n\r", "=", "\'\"", false);
186 ASSERT_TRUE(gorp.parse(out), TEST + "gorp should be parseable");
187 LOG(astring("file after parsing:\n") + out);
188 LOG("and in tabular form:");
189 LOG(jed.table().text_form());
190 ASSERT_TRUE(gorp.exists("[garfola]"), TEST + "section header was omitted!");
191 ASSERT_EQUAL(gorp.find("[garfola]"), astring(""),
192 TEST + "section header had unexpected contents!");
193 ASSERT_TRUE(gorp.exists("treadmill"), TEST + "treadmill is missing!");
194 ASSERT_EQUAL(gorp.find("treadmill"),
195 astring("\"this ain't the place\nwhere'n we been done\nseein' no quotes\""),
196 TEST + "treadmill has incorrect contents!");
197 ASSERT_TRUE(gorp.exists("borfulate"), TEST + "borfulate is missing!");
198 ASSERT_EQUAL(gorp.find("borfulate"),
199 astring("'similarly \"we\" do not like\nthe \" quote \" type thing here'"),
200 TEST + "borfulate has incorrect contents!");
203 astring test_set_5 = "\n\
204 x~35; y~92 ;#comment ; d ~83 ; e~ 54 ; ? new comment ;sud ~ xj23-8 ; nigh ~2";
206 astring TEST = "Fifth Test: ";
207 astring testing = test_set_5;
208 LOG(astring("file before parsing:\n") + testing);
209 variable_tokenizer jed(";", "~");
210 jed.set_comment_chars("#?");
211 ASSERT_TRUE(jed.parse(testing), TEST + "jed should be parseable");
214 LOG(astring("file after parsing:\n") + out);
215 LOG("and in tabular form:");
216 LOG(jed.table().text_form());
218 variable_tokenizer gorp(";", "~");
219 gorp.set_comment_chars("#?");
220 ASSERT_TRUE(gorp.parse(out), TEST + "gorp should be parseable");
221 LOG("gorp in tabular form:");
222 LOG(gorp.table().text_form());
223 //hmmm: need equalizable/orderable on table?
224 ASSERT_TRUE(gorp.table() == jed.table(), TEST + "gorp text not same as jed!");
226 ASSERT_EQUAL(jed.find("x"), astring("35"), TEST + "value for x missing or invalid");
227 ASSERT_EQUAL(jed.find("y"), astring("92"), TEST + "value for y missing or invalid");
228 ASSERT_EQUAL(jed.find("d"), astring("83"), TEST + "value for d missing or invalid");
229 ASSERT_EQUAL(jed.find("e"), astring("54"), TEST + "value for e missing or invalid");
230 ASSERT_EQUAL(jed.find("sud"), astring("xj23-8"), TEST + "value for sud missing or invalid");
231 ASSERT_EQUAL(jed.find("nigh"), astring("2"), TEST + "value for nigh missing or invalid");
234 astring test_set_6 = "\r\n\r\n\r\
235 # this is yet another test with comments.\r\n\
236 ; we want to be sure stuff works right.\r\n\
239 shagbot =once upon a time there was a man \r\n\
240 \t\t\tpunzola megamum =brandle the handle \r\n\
242 mensch = racer X\r\n\
245 astring TEST = "Sixth Test: ";
246 astring testing = test_set_6;
247 LOG(astring("file before parsing:\n===========\n") + testing + "\n===========");
248 variable_tokenizer jed("\n\r", "=");
249 jed.set_comment_chars("#;");
250 ASSERT_TRUE(jed.parse(testing), TEST + "jed should be parseable");
253 LOG(astring("file after parsing:\n===========\n") + out + "\n===========");
254 LOG("and in tabular form:");
255 LOG(jed.table().text_form());
257 variable_tokenizer gorp("\n\r", "=");
258 gorp.set_comment_chars("#;");
259 ASSERT_TRUE(gorp.parse(out), TEST + "gorp should be parseable");
260 LOG("gorp in tabular form:");
261 LOG(gorp.table().text_form());
262 LOG(a_sprintf("gorp has %d fields, jed has %d fields", gorp.symbols(), jed.symbols()));
263 ASSERT_TRUE(gorp.table() == jed.table(), TEST + "gorp text not same as jed!");
265 ASSERT_EQUAL(jed.find("crumpet"), astring("tempest"),
266 TEST + "value for crumpet missing or invalid");
267 ASSERT_EQUAL(jed.find("moomar"), astring("18"),
268 TEST + "value for moomar missing or invalid");
269 ASSERT_EQUAL(jed.find("shagbot"), astring("once upon a time there was a man"),
270 TEST + "value for shagbot missing or invalid");
271 ASSERT_EQUAL(jed.find("trapzoot"), astring("uhhh"),
272 TEST + "value for trapzoot missing or invalid");
273 ASSERT_EQUAL(jed.find("punzola megamum"), astring("brandle the handle"),
274 TEST + "value for punzola missing or invalid");
275 ASSERT_EQUAL(jed.find("mensch"), astring("racer X"),
276 TEST + "value for mensch missing or invalid");
279 return final_report();
284 HOOPLE_MAIN(test_tokenizer, );