-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e63c8f
commit b14ae8c
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import optparse | ||
|
||
import pytest | ||
|
||
from cylc.flow.option_parsers import Options | ||
|
||
|
||
@pytest.fixture | ||
def simple_parser(): | ||
"""Simple option parser.""" | ||
parser = optparse.OptionParser() | ||
parser.add_option('-a', action='store') | ||
parser.add_option('-b', action='store_true') | ||
parser.add_option('-c', default='C') | ||
return parser | ||
|
||
|
||
def test_options(simple_parser): | ||
"""It is a substitute for an optparse options object.""" | ||
options = Options(parser=simple_parser) | ||
opts = options(a=1, b=True) | ||
|
||
# we can access options as attributes | ||
assert opts.a == 1 | ||
assert opts.b is True | ||
|
||
# defaults are automatically substituted | ||
assert opts.c == 'C' | ||
|
||
# get-like syntax should work | ||
assert opts.get('d', 42) == 42 | ||
|
||
# invalid keys result in KeyErrors | ||
with pytest.raises(KeyError): | ||
opts.d | ||
with pytest.raises(KeyError): | ||
opts(d=1) | ||
|
||
# just for fun we can still use dict syntax | ||
assert opts['a'] == 1 |