Debian Codegraph @

tests/u/parsec/test_types.py

raw
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 """Test parsec type parsing.
18
19 .. note::
20
21 This test is a pytest port of the old ``tests/parsec/synonyms``
22 test battery.
23
24 """
25
26 import pytest
27
28 from cylc.flow.parsec.config import (
29 ConfigNode as Conf
30 )
31 from cylc.flow.parsec.validate import (
32 CylcConfigValidator as VDR
33 )
34
35
36 @pytest.fixture
37 def generate_spec():
38 def _inner(typ, validator):
39 """Return a sample spec for the given data type.
40
41 Args:
42 typ (str):
43 An arbirtrary name for this type.
44 validator (str):
45 A parsec validator.
46
47 Returns:
48 cylc.flow.parsec.config.ConfigNode
49
50 """
51 with Conf('/') as myconf:
52 with Conf(typ):
53 Conf('<item>', validator)
54 return myconf
55 return _inner
56
57
58 @pytest.fixture
59 def generate_config():
60 def _inner(typ, value):
61 """Return a sample config for the given data type.
62
63 The aim is to cover every facetious combination of quotes
64 newlines and comments.
65
66 Args:
67 typ (str):
68 An arbitrary name for this type.
69 value (object):
70 A stringable value to dump in the conf.
71 For list types provide a list of stringable values.
72
73 """
74 if isinstance(value, list):
75 return f'''
76 [{typ}]
77 plain = {','.join(value)} # comment
78 spaced = {', '.join(value)} # comment
79 badly spaced = {' , '.join(value)} # comment
80 single quoted = {', '.join((f"'{x}'" for x in value))}
81 double quoted = {', '.join((f'"{x}"' for x in value))}
82 multi line = {value[0]}, \\
83 {', '.join(value[1:])} # comment
84 '''
85 else:
86 return f'''
87 [{typ}]
88 plain1 = {value} # comment
89 single quoted = '{value}' # comment
90 double quoted = "{value}" # comment
91 triple single quoted = \'\'\'{value}\'\'\' # comment
92 triple double quoted = """{value}""" # comment
93 triple single quoted multi = \'\'\'
94 {value}
95 \'\'\' # comment
96 triple double quoted multi = """
97 {value}
98 """ # comment
99 '''
100 return _inner
101
102
103 def test_types(generate_spec, generate_config, config):
104 """Test type parsing.
105
106 Test every facetious combination of:
107
108 * Data types.
109 * Quotation.
110 * Inline-commenting.
111
112 """
113 for typ, validator, string_repr, parsed_value in [
114 ('boolean', VDR.V_BOOLEAN, 'true', True),
115 ('boolean', VDR.V_BOOLEAN, 'True', True),
116 ('boolean', VDR.V_BOOLEAN, 'false', False),
117 ('boolean', VDR.V_BOOLEAN, 'False', False),
118 ('integer', VDR.V_INTEGER, '42', 42),
119 ('float', VDR.V_FLOAT, '9.9', 9.9),
120 ('string', VDR.V_STRING, 'the quick brown fox',
121 'the quick brown fox'),
122 (
123 'integer_list',
124 VDR.V_INTEGER_LIST,
125 ['1', '2', '3', '4', '5'],
126 [1, 2, 3, 4, 5]
127 ),
128 (
129 'float_list',
130 VDR.V_FLOAT_LIST,
131 ['1.1', '2.2', '3.3', '4.4', '5.5'],
132 [1.1, 2.2, 3.3, 4.4, 5.5]
133 ),
134 (
135 'string_list',
136 VDR.V_STRING_LIST,
137 ['be', 'ef', 'we', 'll', 'in', 'gt', 'on'],
138 ['be', 'ef', 'we', 'll', 'in', 'gt', 'on']
139 ),
140 ]:
141 spec = generate_spec(typ, validator)
142 conf = generate_config(typ, string_repr)
143 cfg = config(spec, conf)
144 assert all((
145 value == parsed_value
146 for value in cfg.get()[typ].values()
147 ))