1
- from json import dumps
2
1
from typing import Optional
3
2
4
3
from pytest import raises
11
10
from ..utils import dedent
12
11
13
12
14
- ignored_tokens = [
15
- # UnicodeBOM
16
- "\uFEFF " , # Byte Order Mark (U+FEFF)
17
- # WhiteSpace
18
- "\t " , # Horizontal Tab (U+0009)
19
- " " , # Space (U+0020)
20
- # LineTerminator
21
- "\n " , # "New Line (U+000A)"
22
- "\r " , # "Carriage Return (U+000D)" [ lookahead ! "New Line (U+000A)" ]
23
- "\r \n " , # "Carriage Return (U+000D)" "New Line (U+000A)"
24
- # Comment
25
- '# "Comment" string\n ' , # `#` CommentChar*
26
- # Comma
27
- "," , # ,
28
- ]
29
-
30
- punctuator_tokens = ["!" , "$" , "(" , ")" , "..." , ":" , "=" , "@" , "[" , "]" , "{" , "|" , "}" ]
31
-
32
- non_punctuator_tokens = [
33
- "name_token" , # Name
34
- "1" , # IntValue
35
- "3.14" , # FloatValue
36
- '"some string value"' , # StringValue
37
- '"""block\n string\n value"""' , # StringValue(BlockString)
38
- ]
39
-
40
-
41
13
def lex_value (s : str ) -> Optional [str ]:
42
14
lexer = Lexer (Source (s ))
43
15
value = lexer .advance ().value
@@ -52,24 +24,10 @@ def __init__(self, doc_string: str):
52
24
def to_equal (self , expected : str ):
53
25
doc_string = self .doc_string
54
26
stripped = strip_ignored_characters (doc_string )
55
-
56
- assert stripped == expected , dedent (
57
- f"""
58
- Expected strip_ignored_characters({ doc_string !r} )
59
- to equal { expected !r}
60
- but got { stripped !r}
61
- """
62
- )
27
+ assert stripped == expected
63
28
64
29
stripped_twice = strip_ignored_characters (stripped )
65
-
66
- assert stripped == stripped_twice , dedent (
67
- f""""
68
- Expected strip_ignored_characters({ stripped !r} )"
69
- to equal { stripped !r}
70
- but got { stripped_twice !r}
71
- """
72
- )
30
+ assert stripped == stripped_twice
73
31
74
32
def to_stay_the_same (self ):
75
33
self .to_equal (self .doc_string )
@@ -139,14 +97,6 @@ def strips_documents_with_only_ignored_characters():
139
97
ExpectStripped (",," ).to_equal ("" )
140
98
ExpectStripped ("#comment\n , \n " ).to_equal ("" )
141
99
142
- for ignored in ignored_tokens :
143
- ExpectStripped (ignored ).to_equal ("" )
144
-
145
- for another_ignored in ignored_tokens :
146
- ExpectStripped (ignored + another_ignored ).to_equal ("" )
147
-
148
- ExpectStripped ("" .join (ignored_tokens )).to_equal ("" )
149
-
150
100
def strips_leading_and_trailing_ignored_tokens ():
151
101
ExpectStripped ("\n 1" ).to_equal ("1" )
152
102
ExpectStripped (",1" ).to_equal ("1" )
@@ -158,109 +108,32 @@ def strips_leading_and_trailing_ignored_tokens():
158
108
ExpectStripped ("1,," ).to_equal ("1" )
159
109
ExpectStripped ("1#comment\n , \n " ).to_equal ("1" )
160
110
161
- for token in punctuator_tokens + non_punctuator_tokens :
162
- for ignored in ignored_tokens :
163
- ExpectStripped (ignored + token ).to_equal (token )
164
- ExpectStripped (token + ignored ).to_equal (token )
165
-
166
- for another_ignored in ignored_tokens :
167
- ExpectStripped (token + ignored + ignored ).to_equal (token )
168
- ExpectStripped (ignored + another_ignored + token ).to_equal (token )
169
-
170
- ExpectStripped ("" .join (ignored_tokens ) + token ).to_equal (token )
171
- ExpectStripped (token + "" .join (ignored_tokens )).to_equal (token )
172
-
173
111
def strips_ignored_tokens_between_punctuator_tokens ():
174
112
ExpectStripped ("[,)" ).to_equal ("[)" )
175
113
ExpectStripped ("[\r )" ).to_equal ("[)" )
176
114
ExpectStripped ("[\r \r )" ).to_equal ("[)" )
177
115
ExpectStripped ("[\r ,)" ).to_equal ("[)" )
178
116
ExpectStripped ("[,\n )" ).to_equal ("[)" )
179
117
180
- for left in punctuator_tokens :
181
- for right in punctuator_tokens :
182
- for ignored in ignored_tokens :
183
- ExpectStripped (left + ignored + right ).to_equal (left + right )
184
-
185
- for another_ignored in ignored_tokens :
186
- ExpectStripped (
187
- left + ignored + another_ignored + right
188
- ).to_equal (left + right )
189
-
190
- ExpectStripped (left + "" .join (ignored_tokens ) + right ).to_equal (
191
- left + right
192
- )
193
-
194
118
def strips_ignored_tokens_between_punctuator_and_non_punctuator_tokens ():
195
119
ExpectStripped ("[,1" ).to_equal ("[1" )
196
120
ExpectStripped ("[\r 1" ).to_equal ("[1" )
197
121
ExpectStripped ("[\r \r 1" ).to_equal ("[1" )
198
122
ExpectStripped ("[\r ,1" ).to_equal ("[1" )
199
123
ExpectStripped ("[,\n 1" ).to_equal ("[1" )
200
124
201
- for non_punctuator in non_punctuator_tokens :
202
- for punctuator in punctuator_tokens :
203
- for ignored in ignored_tokens :
204
- ExpectStripped (punctuator + ignored + non_punctuator ).to_equal (
205
- punctuator + non_punctuator
206
- )
207
-
208
- for another_ignored in ignored_tokens :
209
- ExpectStripped (
210
- punctuator + ignored + another_ignored + non_punctuator
211
- ).to_equal (punctuator + non_punctuator )
212
-
213
- ExpectStripped (
214
- punctuator + "" .join (ignored_tokens ) + non_punctuator
215
- ).to_equal (punctuator + non_punctuator )
216
-
217
125
def strips_ignored_tokens_between_non_punctuator_and_punctuator_tokens ():
218
126
ExpectStripped ("1,[" ).to_equal ("1[" )
219
127
ExpectStripped ("1\r [" ).to_equal ("1[" )
220
128
ExpectStripped ("1\r \r [" ).to_equal ("1[" )
221
129
ExpectStripped ("1\r ,[" ).to_equal ("1[" )
222
130
ExpectStripped ("1,\n [" ).to_equal ("1[" )
223
131
224
- for non_punctuator in non_punctuator_tokens :
225
- for punctuator in punctuator_tokens :
226
- # Special case for that is handled in the below test
227
- if punctuator == "..." :
228
- continue
229
-
230
- for ignored in ignored_tokens :
231
- ExpectStripped (non_punctuator + ignored + punctuator ).to_equal (
232
- non_punctuator + punctuator
233
- )
234
-
235
- for another_ignored in ignored_tokens :
236
- ExpectStripped (
237
- non_punctuator + ignored + another_ignored + punctuator
238
- ).to_equal (non_punctuator + punctuator )
239
-
240
- ExpectStripped (
241
- non_punctuator + "" .join (ignored_tokens ) + punctuator
242
- ).to_equal (non_punctuator + punctuator )
243
-
244
132
def replace_ignored_tokens_between_non_punctuator_tokens_and_spread_with_space ():
245
133
ExpectStripped ("a ..." ).to_equal ("a ..." )
246
134
ExpectStripped ("1 ..." ).to_equal ("1 ..." )
247
135
ExpectStripped ("1 ... ..." ).to_equal ("1 ......" )
248
136
249
- for non_punctuator in non_punctuator_tokens :
250
- for ignored in ignored_tokens :
251
- ExpectStripped (non_punctuator + ignored + "..." ).to_equal (
252
- non_punctuator + " ..."
253
- )
254
-
255
- for another_ignored in ignored_tokens :
256
- ExpectStripped (
257
- non_punctuator + ignored + another_ignored + " ..."
258
- ).to_equal (non_punctuator + " ..." )
259
-
260
- ExpectStripped (non_punctuator + "" .join (ignored_tokens ) + "..." ).to_equal (
261
- non_punctuator + " ..."
262
- )
263
-
264
137
def replace_ignored_tokens_between_non_punctuator_tokens_with_space ():
265
138
ExpectStripped ("1 2" ).to_stay_the_same ()
266
139
ExpectStripped ('"" ""' ).to_stay_the_same ()
@@ -271,57 +144,17 @@ def replace_ignored_tokens_between_non_punctuator_tokens_with_space():
271
144
ExpectStripped ("a 1" ).to_equal ("a 1" )
272
145
ExpectStripped ("a \t 1" ).to_equal ("a 1" )
273
146
274
- for left in non_punctuator_tokens :
275
- for right in non_punctuator_tokens :
276
- for ignored in ignored_tokens :
277
- ExpectStripped (left + ignored + right ).to_equal (left + " " + right )
278
-
279
- for another_ignored in ignored_tokens :
280
- ExpectStripped (
281
- left + ignored + another_ignored + right
282
- ).to_equal (left + " " + right )
283
-
284
- ExpectStripped (left + "" .join (ignored_tokens ) + right ).to_equal (
285
- left + " " + right
286
- )
287
-
288
147
def does_not_strip_ignored_tokens_embedded_in_the_string ():
289
148
ExpectStripped('" "' ).to_stay_the_same ()
290
149
ExpectStripped ('","' ).to_stay_the_same ()
291
150
ExpectStripped ('",,"' ).to_stay_the_same ()
292
151
ExpectStripped ('",|"' ).to_stay_the_same ()
293
152
294
- for ignored in ignored_tokens :
295
- ExpectStripped (dumps (ignored )).to_stay_the_same ()
296
-
297
- for another_ignored in ignored_tokens :
298
- ExpectStripped (dumps (ignored + another_ignored )).to_stay_the_same ()
299
-
300
- ExpectStripped (dumps ("" .join (ignored_tokens ))).to_stay_the_same ()
301
-
302
153
def does_not_strip_ignored_tokens_embedded_in_the_block_string ():
303
154
ExpectStripped ('""","""' ).to_stay_the_same ()
304
155
ExpectStripped ('""",,"""' ).to_stay_the_same ()
305
156
ExpectStripped ('""",|"""' ).to_stay_the_same ()
306
157
307
- ignored_tokens_without_formatting = [
308
- token
309
- for token in ignored_tokens
310
- if token not in ["\n " , "\r " , "\r \n " , "\t " , " " ]
311
- ]
312
-
313
- for ignored in ignored_tokens_without_formatting :
314
- ExpectStripped ('"""|' + ignored + '|"""' ).to_stay_the_same ()
315
-
316
- for another_ignored in ignored_tokens_without_formatting :
317
- ExpectStripped (
318
- '"""|' + ignored + another_ignored + '|"""'
319
- ).to_stay_the_same ()
320
-
321
- ExpectStripped (
322
- '"""|' + "" .join (ignored_tokens_without_formatting ) + '|"""'
323
- ).to_stay_the_same ()
324
-
325
158
def strips_ignored_characters_inside_block_strings ():
326
159
# noinspection PyShadowingNames
327
160
def expect_stripped_string (block_str : str ):
0 commit comments