| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import optparse |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from cylc.flow.option_parsers import Options |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture |
| 25 |
def simple_parser(): |
| 26 |
"""Simple option parser.""" |
| 27 |
parser = optparse.OptionParser() |
| 28 |
parser.add_option('-a', action='store') |
| 29 |
parser.add_option('-b', action='store_true') |
| 30 |
parser.add_option('-c', default='C') |
| 31 |
return parser |
| 32 |
|
| 33 |
|
| 34 |
def test_options(simple_parser): |
| 35 |
"""It is a substitute for an optparse options object.""" |
| 36 |
options = Options(parser=simple_parser) |
| 37 |
opts = options(a=1, b=True) |
| 38 |
|
| 39 |
# we can access options as attributes |
| 40 |
assert opts.a == 1 |
| 41 |
assert opts.b is True |
| 42 |
|
| 43 |
# defaults are automatically substituted |
| 44 |
assert opts.c == 'C' |
| 45 |
|
| 46 |
# get-like syntax should work |
| 47 |
assert opts.get('d', 42) == 42 |
| 48 |
|
| 49 |
# invalid keys result in KeyErrors |
| 50 |
with pytest.raises(KeyError): |
| 51 |
opts.d |
| 52 |
with pytest.raises(KeyError): |
| 53 |
opts(d=1) |
| 54 |
|
| 55 |
# just for fun we can still use dict syntax |
| 56 |
assert opts['a'] == 1 |