- Integrated modified Nibbler and test code from kronisk.  These
  changes make both test and Nibbler standalone objects, with configurable
  features.
This commit is contained in:
Paul Beckingham 2012-01-05 17:37:50 -05:00
parent a262d41828
commit fb38dca1db
4 changed files with 74 additions and 12 deletions

View file

@ -25,20 +25,20 @@
//
////////////////////////////////////////////////////////////////////////////////
#define L10N // Localization complete.
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <inttypes.h>
#include <Nibbler.h>
#ifdef NIBBLER_FEATURE_DATE
#include <Date.h>
#include <text.h>
#endif
#ifdef NIBBLER_FEATURE_REGEX
#include <RX.h>
#endif
const char* c_digits = "0123456789";
const char* c_digits = "0123456789"; // TODO Not used?
////////////////////////////////////////////////////////////////////////////////
Nibbler::Nibbler ()
@ -139,6 +139,7 @@ bool Nibbler::getUntil (const std::string& terminator, std::string& result)
}
////////////////////////////////////////////////////////////////////////////////
#ifdef NIBBLER_FEATURE_REGEX
bool Nibbler::getUntilRx (const std::string& regex, std::string& result)
{
if (_cursor < _length)
@ -162,6 +163,7 @@ bool Nibbler::getUntilRx (const std::string& regex, std::string& result)
return false;
}
#endif
////////////////////////////////////////////////////////////////////////////////
bool Nibbler::getUntilOneOf (const std::string& chars, std::string& result)
@ -529,6 +531,7 @@ bool Nibbler::getLiteral (const std::string& literal)
}
////////////////////////////////////////////////////////////////////////////////
#ifdef NIBBLER_FEATURE_REGEX
bool Nibbler::getRx (const std::string& regex, std::string& result)
{
if (_cursor < _length)
@ -553,6 +556,7 @@ bool Nibbler::getRx (const std::string& regex, std::string& result)
return false;
}
#endif
////////////////////////////////////////////////////////////////////////////////
bool Nibbler::getUUID (std::string& result)
@ -685,6 +689,7 @@ bool Nibbler::getDateISO (time_t& t)
}
////////////////////////////////////////////////////////////////////////////////
#ifdef NIBBLER_FEATURE_DATE
bool Nibbler::getDate (const std::string& format, time_t& t)
{
std::string::size_type i = _cursor;
@ -989,6 +994,7 @@ bool Nibbler::getDate (const std::string& format, time_t& t)
_cursor = i;
return true;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Assumes that the options are sorted by decreasing length, so that if the
@ -1120,6 +1126,7 @@ bool Nibbler::skipWS ()
}
////////////////////////////////////////////////////////////////////////////////
#ifdef NIBBLER_FEATURE_REGEX
bool Nibbler::skipRx (const std::string& regex)
{
if (_cursor < _length)
@ -1143,6 +1150,7 @@ bool Nibbler::skipRx (const std::string& regex)
return false;
}
#endif
////////////////////////////////////////////////////////////////////////////////
bool Nibbler::skipAllOneOf (const std::string& chars)
@ -1219,6 +1227,21 @@ bool Nibbler::depleted ()
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Override of ispunct, that considers #, $ and @ not to be punctuation.
//
// ispunct: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
// Punctuation: ! " % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ ` { | } ~
// delta: # $ @
//
bool Nibbler::isPunctuation (char c)
{
if (c == '@' || c == '#' || c == '$')
return false;
return ispunct (c);
}
////////////////////////////////////////////////////////////////////////////////
std::string Nibbler::dump ()
{

View file

@ -29,6 +29,12 @@
#define INCLUDED_NIBBLER
#define L10N // Localization complete.
#define NIBBLER_FEATURE_DATE
//#undef NIBBLER_FEATURE_DATE
#define NIBBLER_FEATURE_REGEX
//#undef NIBBLER_FEATURE_REGEX
#include <string>
#include <vector>
@ -44,7 +50,9 @@ public:
bool getUntil (char, std::string&);
bool getUntil (const std::string&, std::string&);
#ifdef NIBBLER_FEATURE_REGEX
bool getUntilRx (const std::string&, std::string&);
#endif
bool getUntilOneOf (const std::string&, std::string&);
bool getUntilWS (std::string&);
bool getUntilEOL (std::string&);
@ -64,10 +72,14 @@ public:
bool getNumber (double&);
bool getUnsignedNumber (double&);
bool getLiteral (const std::string&);
#ifdef NIBBLER_FEATURE_REGEX
bool getRx (const std::string&, std::string&);
#endif
bool getUUID (std::string&);
bool getDateISO (time_t&);
#ifdef NIBBLER_FEATURE_DATE
bool getDate (const std::string&, time_t&);
#endif
bool getOneOf (const std::vector <std::string>&, std::string&);
bool getName (std::string&);
bool getWord (std::string&);
@ -77,7 +89,9 @@ public:
bool skipAll (char);
bool skipAllOneOf (const std::string&);
bool skipWS ();
#ifdef NIBBLER_FEATURE_REGEX
bool skipRx (const std::string&);
#endif
char next ();
std::string next (const int quantity);
@ -89,6 +103,7 @@ public:
bool depleted ();
static bool isPunctuation (char);
std::string dump ();
private:

View file

@ -26,8 +26,10 @@
////////////////////////////////////////////////////////////////////////////////
#include <Context.h>
#include <Date.h>
#include <Nibbler.h>
#ifdef NIBBLER_FEATURE_DATE
#include <Date.h>
#endif
#include <test.h>
Context context;
@ -35,7 +37,19 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
#ifdef NIBBLER_FEARURE_DATE
#ifdef NIBBLER_FEATURE_REGEX
UnitTest t (292);
#else
UnitTest t (268);
#endif
#else
#ifdef NIBBLER_FEATURE_REGEX
UnitTest t (242);
#else
UnitTest t (218);
#endif
#endif
try
{
@ -44,7 +58,10 @@ int main (int argc, char** argv)
int i;
double d;
time_t ti;
#ifdef NIBBLER_FEATURE_DATE
Date dt;
#endif
std::vector <std::string> options;
// Make sure the nibbler behaves itself with trivial input.
@ -65,7 +82,9 @@ int main (int argc, char** argv)
t.notok (n.getUntilEOL (s), "trivial: getUntilEOL");
t.notok (n.getUntilEOS (s), "trivial: getUntilEOS");
t.notok (n.getDateISO (ti), "trivial: getDateISO");
#ifdef NIBBLER_FEATURE_DATE
t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate");
#endif
t.notok (n.getOneOf (options, s), "trivial: getOneOf");
t.ok (n.depleted (), "trivial: depleted");
@ -81,6 +100,7 @@ int main (int argc, char** argv)
t.notok (n.getUntil (' ', s), " '' : getUntil (' ') -> false");
t.ok (n.depleted (), " '' : depleted () -> true");
#ifdef NIBBLER_FEATURE_REGEX
// bool getUntilRx (const std::string&, std::string&);
t.diag ("Nibbler::getUntilRx");
n = Nibbler ("one two");
@ -95,6 +115,7 @@ int main (int argc, char** argv)
t.ok (n.getUntilRx ("$", s), " 'two' : getUntilRx ('$') -> true");
t.is (s, "two", " 'two' : getUntilRx ('$') -> 'two'");
t.ok (n.depleted (), " '' : depleted () -> true");
#endif
// bool getUntilOneOf (const std::string&, std::string&);
t.diag ("Nibbler::getUntilOneOf");
@ -170,6 +191,7 @@ int main (int argc, char** argv)
t.is (s, "foo", " 'foo' : getUntilEOS () -> 'foo'");
t.ok (n.depleted (), " '' : depleted () -> true");
#ifdef NIBBLER_FEATURE_REGEX
// bool skipRx (const std::string&);
t.diag ("Nibbler::skipRx");
n = Nibbler ("one two");
@ -178,6 +200,7 @@ int main (int argc, char** argv)
t.ok (n.skipRx ("e+"), " 'e two' : skipRx ('e+') -> true");
t.ok (n.skipRx ("...."), " ' two' : skipRx ('....') -> true");
t.ok (n.depleted (), " '' : depleted () -> true");
#endif
// bool getQuoted (char, std::string&);
t.diag ("Nibbler::getQuoted");
@ -280,6 +303,7 @@ int main (int argc, char** argv)
t.ok (n.getLiteral ("bar"), " 'bar' : getLiteral ('bar') -> true");
t.ok (n.depleted (), " '' : depleted () -> true");
#ifdef NIBBLER_FEATURE_REGEX
// bool getRx (const std::string&, std::string&);
t.diag ("Nibbler::getRx");
n = Nibbler ("one two three");
@ -293,6 +317,7 @@ int main (int argc, char** argv)
t.ok (n.getRx ("th...", s), " 'three' : getRx ('th...') -> true");
t.is (s, "three", " 'three' : getRx ('th...') -> 'three'");
t.ok (n.depleted (), " '' : depleted () -> true");
#endif
// bool getUUID (std::string&);
t.diag ("Nibbler::getUUID");
@ -321,6 +346,7 @@ int main (int argc, char** argv)
t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890");
t.ok (n.depleted (), "depleted");
#ifdef NIBBLER_FEATURE_DATE
// bool getDate (time_t&, const std::string&);
t.diag ("Nibbler::getDate");
n = Nibbler ("1/1/2008");
@ -401,6 +427,7 @@ int main (int argc, char** argv)
t.is (dt.hour (), 12, "ctor (std::string) -> h");
t.is (dt.minute (), 34, "ctor (std::string) -> N");
t.is (dt.second (), 56, "ctor (std::string) -> S");
#endif
// bool getOneOf (const std::vector <std::string>&, std::string&);
t.diag ("Nibbler::getOneOf");

View file

@ -29,9 +29,6 @@
#include <iomanip>
#include <string.h>
#include <math.h>
#include <main.h>
#include <util.h>
#include <text.h>
#include <test.h>
///////////////////////////////////////////////////////////////////////////////
@ -394,9 +391,9 @@ void UnitTest::is (
///////////////////////////////////////////////////////////////////////////////
void UnitTest::diag (const std::string& text)
{
std::string trimmed = trim (text, " \t\n\r\f");
std::cout << "# " << trimmed << "\n";
std::string::size_type start = text.find_first_not_of (" \t\n\r\f");
std::string::size_type end = text.find_last_not_of (" \t\n\r\f");
std::cout << "# " << text.substr (start, end - start + 1);
}
///////////////////////////////////////////////////////////////////////////////