mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Enhancements - Complete Record parsing
- Enhanced Record unit tests - Fixed broken Att unit tests - Fixed broken Att
This commit is contained in:
parent
75c220c352
commit
a98951a8c3
5 changed files with 54 additions and 77 deletions
11
src/Att.cpp
11
src/Att.cpp
|
@ -114,18 +114,15 @@ bool Att::parse (Nibbler& n)
|
||||||
|
|
||||||
if (n.skip (':'))
|
if (n.skip (':'))
|
||||||
{
|
{
|
||||||
if (n.getQuoted ('"', mValue))
|
// Both quoted and unquoted Att's are accepted.
|
||||||
return true;
|
|
||||||
|
|
||||||
// This is here to tolerate unquoted values.
|
|
||||||
// Consider removing this for a stricter parse.
|
// Consider removing this for a stricter parse.
|
||||||
if (n.getUntil (' ', mValue))
|
if (n.getQuoted ('"', mValue) ||
|
||||||
|
n.getUntil (' ', mValue))
|
||||||
{
|
{
|
||||||
dequote (mValue);
|
|
||||||
decode (mValue);
|
decode (mValue);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
throw std::string ("Missing attribute value");
|
throw std::string ("Missing attribute value");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -37,25 +37,12 @@ Record::Record ()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
Record::Record (const Record& other)
|
|
||||||
{
|
|
||||||
throw std::string ("unimplemented Record::Record");
|
|
||||||
*this = other;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Record::Record (const std::string& input)
|
Record::Record (const std::string& input)
|
||||||
{
|
{
|
||||||
parse (input);
|
parse (input);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
Record& Record::operator= (const Record& other)
|
|
||||||
{
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Record::~Record ()
|
Record::~Record ()
|
||||||
{
|
{
|
||||||
|
@ -80,7 +67,7 @@ std::string Record::composeF4 ()
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// start --> name --> : --> " --> value --> " --> end
|
// [ --> start --> name --> : --> " --> value --> " --> ] --> end
|
||||||
// ^ |
|
// ^ |
|
||||||
// +------------- \s <--------------+
|
// +------------- \s <--------------+
|
||||||
//
|
//
|
||||||
|
@ -93,13 +80,15 @@ void Record::parse (const std::string& input)
|
||||||
n.skip (']') &&
|
n.skip (']') &&
|
||||||
n.depleted ())
|
n.depleted ())
|
||||||
{
|
{
|
||||||
Nibbler nl (line);
|
if (line.length () == 0)
|
||||||
|
throw std::string ("Empty FF4 record");
|
||||||
|
|
||||||
|
Nibbler nl (line);
|
||||||
Att a;
|
Att a;
|
||||||
while (a.parse (nl))
|
while (!nl.depleted () && a.parse (nl))
|
||||||
{
|
{
|
||||||
nl.skip (' ');
|
|
||||||
(*this)[a.name ()] = a;
|
(*this)[a.name ()] = a;
|
||||||
|
nl.skip (' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string remainder;
|
std::string remainder;
|
||||||
|
|
|
@ -36,9 +36,7 @@ class Record : public std::map <std::string, Att>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Record (); // Default constructor
|
Record (); // Default constructor
|
||||||
Record (const Record&); // Copy constructor
|
|
||||||
Record (const std::string&); // Copy constructor
|
Record (const std::string&); // Copy constructor
|
||||||
Record& operator= (const Record&); // Assignment operator
|
|
||||||
virtual ~Record (); // Destructor
|
virtual ~Record (); // Destructor
|
||||||
|
|
||||||
std::string composeF4 ();
|
std::string composeF4 ();
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
UnitTest t (38);
|
UnitTest t (37);
|
||||||
|
|
||||||
Att a1 ("name", "value");
|
Att a1 ("name", "value");
|
||||||
t.is (a1.name (), "name", "Att::Att (name, value), Att.name");
|
t.is (a1.name (), "name", "Att::Att (name, value), Att.name");
|
||||||
|
@ -145,22 +145,19 @@ int main (int argc, char** argv)
|
||||||
good = true;
|
good = true;
|
||||||
try {a7.parse (n);} catch (...) {good = false;}
|
try {a7.parse (n);} catch (...) {good = false;}
|
||||||
t.ok (good, "Att::parse (name:\"value)");
|
t.ok (good, "Att::parse (name:\"value)");
|
||||||
t.is (a7.composeF4 (), "name:\"value\"", "Att::composeF4 -> name:\"value\"");
|
t.is (a7.composeF4 (), "name:\""value\"", "Att::composeF4 -> name:\""value\"");
|
||||||
|
|
||||||
n = Nibbler ("name:value\"");
|
n = Nibbler ("name:value\"");
|
||||||
good = true;
|
good = true;
|
||||||
try {a7.parse (n);} catch (...) {good = false;}
|
try {a7.parse (n);} catch (...) {good = false;}
|
||||||
t.ok (good, "Att::parse (name:value\")");
|
t.ok (good, "Att::parse (name:value\")");
|
||||||
t.is (a7.composeF4 (), "name:\"value\"", "Att::composeF4 -> name:\"value\"");
|
t.is (a7.composeF4 (), "name:\"value"\"", "Att::composeF4 -> name:\"value"\"");
|
||||||
|
|
||||||
n = Nibbler ("name:val\"ue");
|
n = Nibbler ("name:val\"ue");
|
||||||
good = true;
|
good = true;
|
||||||
try {a7.parse (n);} catch (...) {good = false;}
|
try {a7.parse (n);} catch (...) {good = false;}
|
||||||
t.ok (good, "Att::parse (name:val\"ue)");
|
t.ok (good, "Att::parse (name:val\"ue)");
|
||||||
t.is (a7.composeF4 (), "name:\"value\"", "Att::composeF4 -> name:\"value\"");
|
t.is (a7.composeF4 (), "name:\"val"ue\"", "Att::composeF4 -> name:\"val"ue\"");
|
||||||
|
|
||||||
n = Nibbler ("name:\"\"va\"\"\"\"\"lu\"\"\"e\"\"");
|
|
||||||
t.is (a7.composeF4 (), "name:\"value\"", "Att::composeF4 (name:\"\"va\"\"\"\"\"lu\"\"\"e\"\")");
|
|
||||||
|
|
||||||
n = Nibbler ("name\"");
|
n = Nibbler ("name\"");
|
||||||
good = true;
|
good = true;
|
||||||
|
|
|
@ -29,57 +29,53 @@
|
||||||
#include <Record.h>
|
#include <Record.h>
|
||||||
#include <test.h>
|
#include <test.h>
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
Record parseRecord (const std::string& input)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Record r (input);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
catch (std::string& e)
|
|
||||||
{
|
|
||||||
std::cout << "# Exception: " << e << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
std::cout << "# Exception!" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Record ();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
UnitTest t (4);
|
UnitTest t (11);
|
||||||
|
|
||||||
// (blank)
|
// (blank)
|
||||||
Record record = parseRecord ("");
|
bool good = true;
|
||||||
t.is (record.size (), (size_t)0, "Record (blank)");
|
Record record;
|
||||||
|
|
||||||
|
try {record = Record ("");}
|
||||||
|
catch (std::string& e){t.diag (e); good = false;}
|
||||||
|
t.notok (good, "Record::Record ('')");
|
||||||
|
|
||||||
// []
|
// []
|
||||||
record = parseRecord ("[]");
|
good = true;
|
||||||
t.is (record.size (), (size_t)0, "Record []");
|
try {record = Record ("[]");}
|
||||||
|
catch (std::string& e){t.diag (e); good = false;}
|
||||||
|
t.notok (good, "Record::Record ('[]')");
|
||||||
|
|
||||||
|
// [name:value]
|
||||||
|
good = true;
|
||||||
|
try {record = Record ("[name:value]");}
|
||||||
|
catch (std::string& e){t.diag (e); good = false;}
|
||||||
|
t.ok (good, "Record::Record ('[name:value]')");
|
||||||
|
t.is (record.get ("name"), "value", "name=value");
|
||||||
|
|
||||||
// [name:"value"]
|
// [name:"value"]
|
||||||
record = parseRecord ("[name:\"value\"]");
|
good = true;
|
||||||
t.is (record.size (), (size_t)1, "Record [name:value]");
|
try {record = Record ("[name:\"value\"]");}
|
||||||
if (record.size () == 1)
|
catch (std::string& e){t.diag (e); good = false;}
|
||||||
{
|
t.ok (good, "Record::Record ('[name:\"value\"]')");
|
||||||
Att a = record["name"];
|
t.is (record.get ("name"), "value", "name=value");
|
||||||
t.is (a.name (), "name", "Record [name:value] -> 'name'");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
t.fail ("Record [name:value] -> 'name'");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO [name:"value"]
|
// [name:"one two"]
|
||||||
// TODO [name:"one two"]
|
good = true;
|
||||||
// TODO [one:two three:four]
|
try {record = Record ("[name:\"one two\"]");}
|
||||||
|
catch (std::string& e){t.diag (e); good = false;}
|
||||||
|
t.ok (good, "Record::Record ('[name:\"one two\"]')");
|
||||||
|
t.is (record.get ("name"), "one two", "name=one two");
|
||||||
|
|
||||||
|
// [one:two three:four]
|
||||||
|
good = true;
|
||||||
|
try {record = Record ("[one:\"two\" three:\"four\"]");}
|
||||||
|
catch (std::string& e){t.diag (e); good = false;}
|
||||||
|
t.ok (good, "Record::Record ('[one:\"two\" three:\"four\"]')");
|
||||||
|
t.is (record.get ("one"), "two", "one=two");
|
||||||
|
t.is (record.get ("three"), "four", "three=four");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue