Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Koch
dune-common
Commits
c34eb58c
Commit
c34eb58c
authored
11 years ago
by
Christian Engwer
Browse files
Options
Downloads
Patches
Plain Diff
[test] add test for the new parametertree command line parser with keywords
parent
3fb778d9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dune/common/test/parametertreetest.cc
+88
-1
88 additions, 1 deletion
dune/common/test/parametertreetest.cc
with
88 additions
and
1 deletion
dune/common/test/parametertreetest.cc
+
88
−
1
View file @
c34eb58c
...
...
@@ -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
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment