00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <fstream>
00027 #include <iostream>
00028 #include <mpcl/test.h>
00029 #include <mpcl/text/regex/matcher.hh>
00030 #include <sstream>
00031
00032
00033 using mpcl::text::regex::TMatcher;
00034 using mpcl::text::TString;
00035
00036
00037
00038
00039
00040
00041 static int _TestAgents (void)
00042 {
00043
00044 using std::size_t;
00045
00046 TMatcher tMatcher;
00047 TString yAgent;
00048 TString yVersion;
00049 TString ySubVersion;
00050 size_t zSize;
00051
00052 TEST_INIT ("test for web browser agents identifiers");
00053
00054
00055
00056
00057 yAgent = "Mozilla/4.75 [en] (X11; U; Linux 2.2.16-22 i686; Nav)";
00058 tMatcher.setInput (yAgent);
00059 zSize = tMatcher.scan ("Mozilla/%t [.]*<<EOF>>", &yVersion, NULL);
00060 TEST_NUMBERS (true, ( zSize > 0 ));
00061 TEST_STRINGS ("4.75", yVersion.c_str());
00062
00063
00064
00065
00066 yAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)";
00067 tMatcher.setInput (yAgent);
00068 zSize = tMatcher.scan ( "Mozilla/%t (compatible; MSIE %t; [.]*<<EOF>>" ,
00069 &yVersion ,
00070 &ySubVersion ,
00071 NULL );
00072 TEST_NUMBERS (true, ( zSize > 0 ));
00073 TEST_STRINGS ("4.0", yVersion.c_str());
00074 TEST_STRINGS ("5.0", ySubVersion.c_str());
00075
00076 TEST_MEMORY_STATUS;
00077 TEST_RETURN_CODE;
00078
00079 }
00080
00081
00082 static int _TestCookies (void)
00083 {
00084
00085 TString yName;
00086 TString ySelector1;
00087 TString ySelector2;
00088 TString yValue;
00089 TString ySource ( "cookie1=0aabn7z3,monday(009=1,700=90)" \
00090 "0aabn7z3,friday(003=9,090=1)" \
00091 "0zzabbn7,january(009=1,030=7)" );
00092 TMatcher tMatcher (ySource);
00093
00094 TEST_INIT ("tests the local stream mode for class 'TMatcher'");
00095
00096
00097
00098
00099
00100 ySource = "fake-value";
00101 tMatcher.scan ("%s=%t", &yName, &yValue, NULL);
00102 TEST_STRINGS ("cookie1", yName.c_str());
00103 TEST_STRINGS ( "0aabn7z3,monday(009=1,700=90)"
00104 "0aabn7z3,friday(003=9,090=1)"
00105 "0zzabbn7,january(009=1,030=7)" ,
00106 yValue.c_str() );
00107
00108
00109
00110
00111 tMatcher.setInput (yValue);
00112 tMatcher.scan ("%s,%s(%t)", &ySelector1, &ySelector2, &yValue, NULL);
00113 TEST_STRINGS ("0aabn7z3", ySelector1.c_str());
00114 TEST_STRINGS ("monday", ySelector2.c_str());
00115 TEST_STRINGS ("009=1,700=90", yValue.c_str());
00116
00117 tMatcher.scan ("%s,%s(%t)", &ySelector1, &ySelector2, &yValue, NULL);
00118 TEST_STRINGS ("0aabn7z3", ySelector1.c_str());
00119 TEST_STRINGS ("friday", ySelector2.c_str());
00120 TEST_STRINGS ("003=9,090=1", yValue.c_str());
00121
00122 tMatcher.scan ("%s,%s(%t)", &ySelector1, &ySelector2, &yValue, NULL);
00123 TEST_STRINGS ("0zzabbn7", ySelector1.c_str());
00124 TEST_STRINGS ("january", ySelector2.c_str());
00125 TEST_STRINGS ("009=1,030=7", yValue.c_str());
00126
00127 TEST_MEMORY_STATUS;
00128 TEST_RETURN_CODE;
00129
00130 }
00131
00132
00133 static int _TestSgmlComments (void)
00134 {
00135
00136 TMatcher tMatcher;
00137 TString ySource;
00138 TString yText;
00139
00140 TEST_INIT ("tests the SGML comments parsing");
00141
00142 ySource =
00143 "<!doctype cdml public \"-//MPCL//DTD CDML 1.0//EN\">\n"
00144 "<cdml>\n"
00145 " <!---->\n"
00146 " <!-- sample text <sample symbols> -->\n"
00147 " <!-- sample text <sample symbols> -->\n"
00148 " <!---->\n"
00149 " <instruct name=\"alter_table_statement\">\n"
00150 " <clause name=\"body\">\n"
00151 "code code %variable% %variable% code\n"
00152 " </clause>\n"
00153 " </instruct>\n"
00154 "</cdml>\n";
00155 tMatcher.setInput (ySource);
00156 tMatcher.scan ("<!%t>[[:space:]]*<cdml>[[:space:]]*", &yText, NULL);
00157 TEST_STRINGS ("doctype cdml public \"-//MPCL//DTD CDML 1.0//EN\"", yText.c_str());
00158
00159 tMatcher.scan ("<!--%t-->[[:space:]]*", &yText, NULL);
00160 TEST_NUMBERS (true, yText.empty());
00161
00162 yText.erase();
00163 tMatcher.scan ("<!--%t-->[[:space:]]*", &yText, NULL);
00164 TEST_STRINGS (" sample text <sample symbols> ", yText.c_str());
00165
00166 yText.erase();
00167 tMatcher.scan ("<!--%t-->[[:space:]]*", &yText, NULL);
00168 TEST_STRINGS (" sample text <sample symbols> ", yText.c_str());
00169
00170 yText.erase();
00171 tMatcher.scan ("<!--%t-->[[:space:]]*", &yText, NULL);
00172 TEST_NUMBERS (true, yText.empty());
00173
00174 TEST_MEMORY_STATUS;
00175 TEST_RETURN_CODE;
00176
00177 }
00178
00179
00180 static int _TestTransactions (void)
00181 {
00182
00183 using std::basic_stringstream;
00184
00185 int iAmmount;
00186 TString yCard;
00187 TString yCode;
00188 TString yMessage;
00189 TString yName;
00190 TString yResult;
00191 std::basic_stringstream<char> tStringstream;
00192 TMatcher tMatcher;
00193
00194 TEST_INIT ("another test");
00195 tStringstream << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00196 << "commerce=big_mall:result=KO:reference=12:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00197 << "commerce=big_mall:result=KO:reference=23123123:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00198 << "commerce=big_mall:result=KO:reference=23:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00199 << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00200 << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00201 << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00202 << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00203 << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00204 << "commerce=big_mall:result=KO:reference=0:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00205 << "commerce=big_mall:result=KO:reference=0728199711135914458:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00206 << "commerce=another_mall:result=KO:reference=0728199711135914459:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n"
00207 << "commerce=big_mall:result=KO:reference=0728199711135914459:cost=3164:code=TRANSACTION DENIED:card=0609:message= Thanks for using this service:name=John Doe:\n";
00208 tMatcher.setInput (tStringstream);
00209 TEST_NUMBERS ( true ,
00210 ( tMatcher.scan ( "[.]*commerce=big_mall:"
00211 "result=%s:"
00212 "reference=0728199711135914459:"
00213 "cost=%d:"
00214 "code=%t:"
00215 "card=%s:"
00216 "message=%t:"
00217 "name=%t:" ,
00218 &yResult ,
00219 &iAmmount ,
00220 &yCode ,
00221 &yCard ,
00222 &yMessage ,
00223 &yName ,
00224 NULL ) > 0 ) );
00225 TEST_STRINGS (yResult.c_str(), "KO");
00226 TEST_STRINGS (yName.c_str(), "John Doe");
00227 TEST_STRINGS (yCode.c_str(), "TRANSACTION DENIED");
00228 TEST_STRINGS (yCard.c_str(), "0609");
00229 TEST_NUMBERS (iAmmount, 3164);
00230
00231 TEST_MEMORY_STATUS;
00232 TEST_RETURN_CODE;
00233
00234 }
00235
00236
00238 int main (void)
00239 {
00240
00241 using std::ifstream;
00242
00243 TEST_INIT ("tests for class 'TMatcher'");
00244
00245 TString yTest;
00246 TString yTest2;
00247 TString yTest4 ("company=acme");
00248 ifstream tTestIfstream ("test-matcher.input");
00249 std::basic_istringstream<char> tTestStringstream2 ("--option=value 1\n<clause>\nHello\n</clause>\n?");
00250 std::basic_istringstream<char> tTestStringstream3 ("login=r12323oot&password=&s1=sex+machine");
00251 std::basic_istringstream<char> tTestStringstream4 (yTest4.c_str());
00252 std::basic_istringstream<char> tTestStringstream5 (" \n Hello \n\t");
00253 TMatcher tMatcher1 (tTestIfstream);
00254 TMatcher tMatcher2 (tTestStringstream2);
00255 TMatcher tMatcher3 (tTestStringstream3);
00256 TMatcher tMatcher4 (tTestStringstream4);
00257 TMatcher tMatcher5 (tTestStringstream5);
00258
00259 tMatcher1.define ("{clause_tag}", "[[:space:]]*<clause>[[:space:]]*");
00260 tMatcher2.define ("{sep}+", "[[:space:]]+");
00261 tMatcher2.define ("{sep}*", "[[:space:]]*");
00262
00263
00264
00265
00266 TEST_NUMBERS (true, ( tMatcher1.scan ("[[:space:]]+<clause>[[:space:]]*%s", &yTest, NULL) > 0 ));
00267 TEST_STRINGS (yTest.c_str(), "Line1");
00268 TEST_NUMBERS (true, ( tMatcher1.scan ("[[:space:]]+<clause>[[:space:]]*%q", &yTest, NULL) > 0 ));
00269 TEST_STRINGS (yTest.c_str(), "Line2 Line2");
00270 TEST_NUMBERS (true, ( tMatcher1.scan ("[[:space:]]+<clause>[[:space:]]*%t</clause>[[:space:]]*", &yTest, NULL) > 0 ));
00271 TEST_STRINGS (yTest.c_str(), "Line3\nLine4\nLine5\n\n");
00272 TEST_NUMBERS (true, ( tMatcher1.scan ("{clause_tag}%t</clause>[[:space:]]*", &yTest, NULL) > 0 ));
00273 TEST_STRINGS (yTest.c_str(), "Line6\nLine7\nLine8\n");
00274 TEST_NUMBERS (true, ( tMatcher1.scan ("%s=%t&", &yTest, &yTest2, NULL) > 0 ));
00275 TEST_STRINGS (yTest.c_str(), "login");
00276 TEST_STRINGS (yTest2.c_str(), "r12323oot");
00277 TEST_NUMBERS (true, ( tMatcher1.scan ("%s=%t&", &yTest, &yTest2, NULL) > 0 ));
00278 TEST_STRINGS (yTest.c_str(), "password");
00279 TEST_STRINGS (yTest2.c_str(), "");
00280 TEST_NUMBERS (false, ( tMatcher1.scan ("%s=%t&", &yTest, &yTest2, NULL) > 0 ));
00281 yTest = "-";
00282 yTest2 = "-";
00283 TEST_NUMBERS (true, ( tMatcher1.scan ("%s=%t", &yTest, &yTest2, NULL) > 0 ));
00284 TEST_STRINGS (yTest.c_str(), "s1");
00285 TEST_STRINGS (yTest2.c_str(), "sex+machine");
00286
00287
00288
00289
00290 TEST_NUMBERS (true, ( tMatcher2.scan ("{sep}*--option{sep}*=%t\n", &yTest, NULL) > 0 ));
00291 TEST_STRINGS (yTest.c_str(), "value 1");
00292 TEST_NUMBERS (true, ( tMatcher3.scan ("%s=%t&", &yTest, &yTest2, NULL) > 0 ));
00293 TEST_STRINGS (yTest.c_str(), "login");
00294 TEST_STRINGS (yTest2.c_str(), "r12323oot");
00295 TEST_NUMBERS (true, ( tMatcher3.scan ("%s=%t&", &yTest, &yTest2, NULL) > 0 ));
00296 TEST_STRINGS (yTest.c_str(), "password");
00297 TEST_STRINGS (yTest2.c_str(), "");
00298 TEST_NUMBERS (false, ( tMatcher3.scan ("%s=%t&", &yTest, &yTest2, NULL) > 0 ));
00299 yTest = "";
00300 yTest2 = "";
00301 TEST_NUMBERS (true, ( tMatcher3.scan ("%s=%t", &yTest, &yTest2, NULL) > 0 ));
00302 TEST_STRINGS (yTest.c_str(), "s1");
00303 TEST_STRINGS (yTest2.c_str(), "sex+machine");
00304 TEST_NUMBERS (false, ( tMatcher4.scan ("%t=%t&", &yTest, &yTest2, NULL) > 0 ));
00305 TEST_NUMBERS (true, ( tMatcher4.scan ("%t=%t", &yTest, &yTest2, NULL) > 0 ));
00306 TEST_STRINGS (yTest.c_str(), "company");
00307 TEST_STRINGS (yTest2.c_str(), "acme");
00308 TEST_NUMBERS (true, ( tMatcher5.scan ("[[:space:]]*%t[[:space:]]*<<EOF>>", &yTest, NULL) > 0 ));
00309 TEST_STRINGS (yTest.c_str(), "Hello");
00310
00311 tMatcher2.setCaseSensitiveness (false);
00312 TEST_NUMBERS (true, ( tMatcher2.scan ("{sep}*<CLAUSE>{sep}*%s{sep}*</ClAuSe>", &yTest, NULL) > 0 ));
00313 TEST_STRINGS (yTest.c_str(), "Hello");
00314
00315 TEST_FUNCTION (_TestAgents());
00316 TEST_FUNCTION (_TestCookies());
00317 TEST_FUNCTION (_TestSgmlComments());
00318 TEST_FUNCTION (_TestTransactions());
00319
00320 TEST_MEMORY_STATUS;
00321 TEST_RETURN_CODE;
00322
00323 }