diff --git a/dune/common/test/parametertreetest.cc b/dune/common/test/parametertreetest.cc index 1a621a5cc6ee0f8baeefd22d6616a0462e786e37..06d9e4e7fc8f0be33f925b04654de34d039eba19 100644 --- a/dune/common/test/parametertreetest.cc +++ b/dune/common/test/parametertreetest.cc @@ -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; }