Tests: Refactoring

- Added functions to reduce code size.
- Added test to check for bad file, bad grammar.
This commit is contained in:
Paul Beckingham 2015-12-22 10:07:07 -05:00
parent 18186de479
commit 75ca4a3b71

View file

@ -28,49 +28,65 @@
#include <Grammar.h> #include <Grammar.h>
#include <test.h> #include <test.h>
////////////////////////////////////////////////////////////////////////////////
void testBadGrammarFile (UnitTest& t, File& file)
{
try
{
Grammar g;
g.loadFromFile (file);
t.fail ("Grammar::loadFromFile accepted a bad file");
}
catch (std::string error)
{
t.diag (error);
t.pass ("Grammar::loadFromFile rejected a bad file");
}
}
////////////////////////////////////////////////////////////////////////////////
void testBadGrammar (UnitTest& t, const std::string& bnf)
{
try
{
Grammar g;
g.loadFromString (bnf);
t.fail ("Grammar::loadFromFile accepted a bad grammar");
}
catch (std::string error)
{
t.diag (error);
t.pass ("Grammar::loadFromFile rejected a bad grammar");
}
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int, char**) int main (int, char**)
{ {
UnitTest t (4); UnitTest t (6);
// Test loading from a missing file. // Test loading from a missing file.
Grammar g;
File missing ("/tmp/does/not/exist"); File missing ("/tmp/does/not/exist");
t.ok (! missing.exists (), "Input file does not exist"); t.notok (missing.exists (), "Grammar file is recognized as missing");
try testBadGrammarFile (t, missing);
{
g.loadFromFile (missing);
t.fail ("Grammar::loadFromFile accepted a missing file");
}
catch (std::string s)
{
t.diag (s);
t.pass ("Grammar::loadFromFile rejected a missing file");
}
// Test error on parsing an empty grammar. // Test error on parsing bad grammar.
try testBadGrammar (t, "");
{ testBadGrammar (t, "# Comment\n"
g.loadFromString (""); "# Comment\n"
t.fail ("Grammar::loadFromFile accepted an empty grammar"); "\n"
} "\n"
catch (std::string s) "\n");
{
t.diag (s);
t.pass ("Grammar::loadFromFile rejected an empty grammar");
}
// Test error on non-empty but trivial grammar. // Unreferenced rule 'two'.
try testBadGrammar (t, "one: \"foo\"\n"
{ "\n"
g.loadFromString ("# Comment\n# comment\n\n\n\n"); "two: \"bar\"\n");
t.fail ("Grammar::loadFromFile accepted an empty grammar");
} // Undefined rule 'three'.
catch (std::string s) testBadGrammar (t, "one: three\n"
{ "\n"
t.diag (s); "two: \"foo\"\n");
t.pass ("Grammar::loadFromFile rejected an empty grammar");
}
return 0; return 0;
} }