From b14ae8cbb163f362300a6d05b51902fb2f629a80 Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Wed, 27 May 2020 12:37:18 +0100 Subject: [PATCH] tests: test option_parsers.Options --- cylc/flow/tests/option_parsers.py | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cylc/flow/tests/option_parsers.py diff --git a/cylc/flow/tests/option_parsers.py b/cylc/flow/tests/option_parsers.py new file mode 100644 index 00000000000..767a8cf739c --- /dev/null +++ b/cylc/flow/tests/option_parsers.py @@ -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