|
46 | 46 | ) |
47 | 47 |
|
48 | 48 |
|
49 | | -def get_multiline(): |
50 | | - """Data provider for multiline tests. Returned values are: |
51 | | -
|
52 | | - file_lines, value, index, maxline, error_message, expected |
53 | | - """ |
54 | | - r = [ |
55 | | - ([], "'''single line'''", 0, 0, None, ("'''single line'''", 0)), |
56 | | - ( |
57 | | - ["'''single line"], |
58 | | - "'''single line", # missing closing quote |
59 | | - 0, |
60 | | - 0, |
61 | | - FileParseError, |
62 | | - { |
63 | | - 'reason': 'Multiline string not closed', |
64 | | - 'line': "'''single line" |
65 | | - } |
66 | | - ), |
67 | | - ( |
68 | | - ["'''", "single line"], |
69 | | - "'''\n '''single line", # missing closing quote |
70 | | - 0, |
71 | | - 0, |
72 | | - FileParseError, |
73 | | - { |
74 | | - 'reason': 'Invalid line', |
75 | | - 'line': "'''" |
76 | | - } |
77 | | - ), |
78 | | - ( |
79 | | - ["", "another value"], # multiline, but we forgot to close quotes |
80 | | - "'''a\n#b", |
81 | | - 0, |
82 | | - 1, |
83 | | - FileParseError, |
84 | | - { |
85 | | - 'reason': "Multiline string not closed" |
86 | | - } |
87 | | - ), |
88 | | - ( |
89 | | - ["", "c'''"], |
90 | | - "'''a\n#b", |
91 | | - 0, |
92 | | - 1, |
93 | | - None, |
94 | | - ("'''a\n#b\nc'''", 1) |
95 | | - ), |
96 | | - ( |
97 | | - ["", "c'''"], # multiline, but we forgot to close quotes |
98 | | - "'''a\n#b", |
99 | | - 0, |
100 | | - 10000, # no error. The function will stop before on the quotes |
101 | | - None, |
102 | | - ("'''a\n#b\nc'''", 1) |
103 | | - ), |
104 | | - ( |
105 | | - ["", "c", "hello", ""], # quotes out of balance |
106 | | - "'''a\n#b", |
107 | | - 0, |
108 | | - 3, |
109 | | - FileParseError, |
110 | | - { |
111 | | - 'reason': "Multiline string not closed" |
112 | | - } |
113 | | - ), |
114 | | - ( |
115 | | - ["", "c", "hello", ""], |
116 | | - "'''a\n#b", |
117 | | - 0, |
118 | | - 4, # one too many |
119 | | - IndexError, |
120 | | - None |
121 | | - ), |
122 | | - ( |
123 | | - ["", "a'''c", "hello", ""], |
124 | | - "'''a\n#b", |
125 | | - 0, |
126 | | - 3, |
127 | | - FileParseError, |
128 | | - { |
129 | | - 'reason': 'Invalid line', |
130 | | - 'line': "a'''c" |
131 | | - } |
132 | | - ) |
133 | | - ] |
134 | | - return r |
135 | | - |
136 | | - |
137 | 49 | def test_file_parse_error(): |
138 | 50 | error = FileParseError(reason="No reason") |
139 | 51 | assert str(error) == "No reason" |
@@ -267,18 +179,106 @@ def test_addict_replace_value_1(): |
267 | 179 | assert cfg['scheduling']['graph']['team']['graph'] == ['ABC', 'test'] |
268 | 180 |
|
269 | 181 |
|
270 | | -def test_multiline(): |
271 | | - for flines, value, index, maxline, exc, expected in get_multiline(): |
272 | | - if exc is not None: |
273 | | - with pytest.raises(exc) as cm: |
274 | | - multiline(flines, value, index, maxline) |
275 | | - if isinstance(cm.value, FileParseError): |
276 | | - exc = cm.value |
277 | | - for key, attr in expected.items(): |
278 | | - assert getattr(exc, key) == attr |
279 | | - else: |
280 | | - r = multiline(flines, value, index, maxline) |
281 | | - assert r == expected |
| 182 | +@pytest.mark.parametrize( |
| 183 | + 'flines, value, index, maxline, exc, expected', |
| 184 | + ( |
| 185 | + param( |
| 186 | + [], |
| 187 | + "'''single line'''", |
| 188 | + 0, |
| 189 | + 0, |
| 190 | + None, |
| 191 | + ("'''single line'''", 0), |
| 192 | + id='single-line', |
| 193 | + ), |
| 194 | + param( |
| 195 | + ["'''single line"], |
| 196 | + "'''single line", # missing closing quote |
| 197 | + 0, |
| 198 | + 0, |
| 199 | + FileParseError, |
| 200 | + { |
| 201 | + 'reason': 'Multiline string not closed', |
| 202 | + 'line': "'''single line", |
| 203 | + }, |
| 204 | + id='missing-closing-quote', |
| 205 | + ), |
| 206 | + param( |
| 207 | + ["'''", "single line"], |
| 208 | + "'''\n '''single line", # missing closing quote |
| 209 | + 0, |
| 210 | + 0, |
| 211 | + FileParseError, |
| 212 | + {'reason': 'Invalid line', 'line': "'''"}, |
| 213 | + id='missing-closing-quote2', |
| 214 | + ), |
| 215 | + param( |
| 216 | + ["", "another value"], # multiline, but we forgot to close quotes |
| 217 | + "'''a\n#b", |
| 218 | + 0, |
| 219 | + 1, |
| 220 | + FileParseError, |
| 221 | + {'reason': "Multiline string not closed"}, |
| 222 | + id='multiline-missing-closing-quote', |
| 223 | + ), |
| 224 | + param( |
| 225 | + ["", "c'''"], |
| 226 | + "'''a\n#b", |
| 227 | + 0, |
| 228 | + 1, |
| 229 | + None, |
| 230 | + ("'''a\n#b\nc'''", 1), |
| 231 | + id='good-path', |
| 232 | + ), |
| 233 | + param( |
| 234 | + ["", "c'''"], # multiline, but we forgot to close quotes |
| 235 | + "'''a\n#b", |
| 236 | + 0, |
| 237 | + 10000, # no error. The function will stop before on the quotes |
| 238 | + None, |
| 239 | + ("'''a\n#b\nc'''", 1), |
| 240 | + id='multiline-missing-closing-quote2', |
| 241 | + ), |
| 242 | + param( |
| 243 | + ["", "c", "hello", ""], # quotes out of balance |
| 244 | + "'''a\n#b", |
| 245 | + 0, |
| 246 | + 3, |
| 247 | + FileParseError, |
| 248 | + {'reason': "Multiline string not closed"}, |
| 249 | + id='unbalanced-quotes', |
| 250 | + ), |
| 251 | + param( |
| 252 | + ["", "c", "hello", ""], |
| 253 | + "'''a\n#b", |
| 254 | + 0, |
| 255 | + 4, # one too many |
| 256 | + IndexError, |
| 257 | + None, |
| 258 | + id='one-too-many', |
| 259 | + ), |
| 260 | + param( |
| 261 | + ["", "a'''c", "hello", ""], |
| 262 | + "'''a\n#b", |
| 263 | + 0, |
| 264 | + 3, |
| 265 | + FileParseError, |
| 266 | + {'reason': 'Invalid line', 'line': "a'''c"}, |
| 267 | + id='invalid-quotes', |
| 268 | + ), |
| 269 | + ), |
| 270 | +) |
| 271 | +def test_multiline(flines, value, index, maxline, exc, expected): |
| 272 | + if exc is not None: |
| 273 | + with pytest.raises(exc) as cm: |
| 274 | + multiline(flines, value, index, maxline) |
| 275 | + if isinstance(cm.value, FileParseError): |
| 276 | + exc = cm.value |
| 277 | + for key, attr in expected.items(): |
| 278 | + assert getattr(exc, key) == attr |
| 279 | + else: |
| 280 | + r = multiline(flines, value, index, maxline) |
| 281 | + assert r == expected |
282 | 282 |
|
283 | 283 |
|
284 | 284 | def test_read_and_proc_no_template_engine(): |
|
0 commit comments