Skip to content

Commit

Permalink
tests: test option_parsers.Options
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-sanders committed Jun 4, 2020
1 parent 4e63c8f commit b14ae8c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cylc/flow/tests/option_parsers.py
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

0 comments on commit b14ae8c

Please sign in to comment.