Skip to content
Snippets Groups Projects
Commit c34eb58c authored by Christian Engwer's avatar Christian Engwer
Browse files

[test] add test for the new parametertree command line parser with keywords

parent 3fb778d9
No related branches found
No related tags found
No related merge requests found
......@@ -99,6 +99,90 @@ void testmodify(P parameterSet)
DUNE_THROW(Dune::Exception, "Failed to write subtree entry");
}
void testOptionsParserResults(std::vector<std::string> args,
const std::vector<std::string> & keywords,
unsigned int required,
bool allow_more,
bool overwrite,
std::string foo, std::string bar,
const std::string referr = "")
{
Dune::ParameterTree pt;
try {
char * argv[10];
for (std::size_t i = 0; i < args.size(); ++i)
argv[i] = &args[i][0];
Dune::ParameterTreeParser::readNamedOptions(args.size(), argv, pt, keywords, required, allow_more, overwrite);
assert(referr == "");
}
catch (const Dune::ParameterTreeParserError & e)
{
std::string err = e.what();
std::size_t offset = err.find("]: ");
err = err.substr(offset + 3, err.find('\n') - offset - 3);
assert(referr == err);
}
if (foo != "" && foo != pt.get<std::string>("foo"))
DUNE_THROW(Dune::Exception, "Options parser failed... foo = "
<< pt.get<std::string>("foo") << " != " << foo);
if (bar != "" && bar != pt.get<std::string>("bar"))
DUNE_THROW(Dune::Exception, "Options parser failed... bar = "
<< pt.get<std::string>("bar") << " != " << bar);
}
void testOptionsParser()
{
std::vector<std::string> keywords = { "foo", "bar" };
// check normal behaviour
{
std::vector<std::string> args = { "progname", "--bar=ligapokal", "peng", "--bar=ligapokal", "--argh=other"};
testOptionsParserResults(args,keywords,keywords.size(),true,true,"peng","ligapokal",
"" /* no error */ );
}
// bail out on overwrite
{
std::vector<std::string> args = { "progname", "--bar=ligapokal", "peng", "--bar=ligapokal", "--argh=other"};
testOptionsParserResults(args,keywords,keywords.size(),true,false,"peng","ligapokal",
"parameter bar already specified");
}
// bail out on unknown options
{
std::vector<std::string> args = { "progname", "--bar=ligapokal", "peng", "--bar=ligapokal", "--argh=other"};
testOptionsParserResults(args,keywords,keywords.size(),false,true,"peng","ligapokal",
"unknown parameter argh");
}
// bail out on missing parameter
{
std::vector<std::string> args = { "progname", "--bar=ligapokal"};
testOptionsParserResults(args,keywords,keywords.size(),true,true,"","ligapokal",
"missing parameter(s) ... foo");
}
// check optional parameter
{
std::vector<std::string> args = { "progname", "--foo=peng"};
testOptionsParserResults(args,keywords,1,true,true,"peng","",
"" /* no error */);
}
// check optional parameter, but bail out on missing parameter
{
std::vector<std::string> args = { "progname", "--bar=ligapokal"};
testOptionsParserResults(args,keywords,1,true,true,"","ligapokal",
"missing parameter(s) ... foo");
}
// bail out on too many parameters
{
std::vector<std::string> args = { "progname", "peng", "ligapokal", "hurz"};
testOptionsParserResults(args,keywords,keywords.size(),true,true,"peng","ligapokal",
"superfluous unnamed parameter");
}
// bail out on missing value
{
std::vector<std::string> args = { "progname", "--foo=peng", "--bar=ligapokal", "--hurz"};
testOptionsParserResults(args,keywords,keywords.size(),true,true,"peng","ligapokal",
"value missing for parameter --hurz");
}
}
int main()
{
try {
......@@ -125,11 +209,14 @@ int main()
// more const tests
testparam<Dune::ParameterTree>(c);
// check the command line parser
testOptionsParser();
}
catch (Dune::Exception & e)
{
std::cout << e << std::endl;
return 1;
}
return (0);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment