feisty meow concerns codebase 2.140
test_tokenizer.cpp
Go to the documentation of this file.
1/*
2* Name : test_tokenizer
3* Author : Chris Koeritz
4* Purpose: Puts the variable_tokenizer through some paces.
5**
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 *
12*/
13
15#include <basis/astring.h>
16#include <basis/byte_array.h>
17#include <basis/functions.h>
18#include <basis/guards.h>
23#include <textual/parser_bits.h>
24#include <unit_test/unit_base.h>
25
26using namespace application;
27using namespace basis;
28using namespace configuration;
29using namespace filesystem;
30using namespace loggers;
31using namespace mathematics;
32using namespace structures;
33using namespace textual;
34using namespace timely;
35using namespace unit_test;
36
37#define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
38
39const int MAX_LINE_SIZE = 1000;
40 // the largest line we will deal with in a file.
41
42class test_tokenizer : public virtual unit_base, public virtual application_shell
43{
44public:
45 test_tokenizer() {}
46 DEFINE_CLASS_NAME("test_tokenizer");
47 virtual int execute();
48};
49
51
52int test_tokenizer::execute()
53{
54 FUNCDEF("execute");
55 {
56 astring test_set_1 = "\n\
57[frederick]\n\
58samba=dance\n\
59tantalus rex=gumby\n\
6057 chevy heap=\"16 anagrams of misty immediately\"\n\
61lingus distractus\n\
62shouldus havus assignmentum=\n\
63above better be parsed = 1\n\
64;and this comment too yo\n\
65ted=agent 12\n";
66
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");
72 astring out;
73 jed.text_form(out);
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());
79
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()));
84//}
85
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!");
104 }
105 {
106 astring test_set_2 = "Name=SRV, Parent=, Persist=Y, Entry=Y, Required=Y, Desc=Server, Tbl=Server";
107
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");
113 astring out;
114 jed.text_form(out);
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!");
123 }
124
125 {
126 astring test_set_3 = "\n\
127[frederick]\n\
128samba=dance\n\
129tantalus rex=gumby \"don#t\n\n'play'\nthat\" homey '\n\ndog\n\n yo \"\ncreen\" arf'\n\
13057 chevy heap=\"16 anagrams of misty immediately\"\n\
131lingus distractus\n\
132shouldus havus assignmentum=\n\
133above better be parsed = 1\n\
134;and this comment too yo\n\
135ted=agent 12\n";
136
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");
142 astring out;
143 jed.text_form(out);
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 "
168 "\"\ncreen\" arf'"),
169 TEST + "gorp tantalus rex has incorrect contents!");
170 }
171 {
172 astring test_set_4 = "\n\
173[garfola]\n\
174treadmill=\"this ain't the place\nwhere'n we been done\nseein' no quotes\"\n\
175borfulate='similarly \"we\" do not like\nthe \" quote \" type thing here'\n\
176";
177
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");
183 astring out;
184 jed.text_form(out);
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!");
201 }
202 {
203 astring test_set_5 = "\n\
204 x~35; y~92 ;#comment ; d ~83 ; e~ 54 ; ? new comment ;sud ~ xj23-8 ; nigh ~2";
205
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");
212 astring out;
213 jed.text_form(out);
214 LOG(astring("file after parsing:\n") + out);
215 LOG("and in tabular form:");
216 LOG(jed.table().text_form());
217
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!");
225
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");
232 }
233 {
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\
237crumpet=tempest\r\n\
238 moomar=18\r\n\
239shagbot =once upon a time there was a man \r\n\
240\t\t\tpunzola megamum =brandle the handle \r\n\
241trapzoot= uhhh\r\n\
242mensch = racer X\r\n\
243\r\n\r\n\r\n";
244
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");
251 astring out;
252 jed.text_form(out);
253 LOG(astring("file after parsing:\n===========\n") + out + "\n===========");
254 LOG("and in tabular form:");
255 LOG(jed.table().text_form());
256
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());
262LOG(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!");
264
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");
277 }
278
279 critical_events::alert_message(astring(class_name()) + ": works for those functions tested.");
280
281 return final_report();
282}
283
285
286HOOPLE_MAIN(test_tokenizer, );
287
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
Definition astring.cpp:130
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
Definition astring.cpp:577
Manages a bank of textual definitions of variables.
static void alert_message(const char *info, const char *title="Alert Message")
shows the message in "info", with an optional "title" on the message.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define TEST(action)
#define LOG(to_print)
const int MAX_LINE_SIZE
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition unit_base.h:46
#define ASSERT_FALSE(a, test_name)
Definition unit_base.h:50