1- from json import dumps
21from typing import Optional
32
43from pytest import raises
1110from ..utils import dedent
1211
1312
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-
4113def lex_value (s : str ) -> Optional [str ]:
4214 lexer = Lexer (Source (s ))
4315 value = lexer .advance ().value
@@ -52,24 +24,10 @@ def __init__(self, doc_string: str):
5224 def to_equal (self , expected : str ):
5325 doc_string = self .doc_string
5426 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
6328
6429 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
7331
7432 def to_stay_the_same (self ):
7533 self .to_equal (self .doc_string )
@@ -139,14 +97,6 @@ def strips_documents_with_only_ignored_characters():
13997 ExpectStripped (",," ).to_equal ("" )
14098 ExpectStripped ("#comment\n , \n " ).to_equal ("" )
14199
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-
150100 def strips_leading_and_trailing_ignored_tokens ():
151101 ExpectStripped ("\n 1" ).to_equal ("1" )
152102 ExpectStripped (",1" ).to_equal ("1" )
@@ -158,109 +108,32 @@ def strips_leading_and_trailing_ignored_tokens():
158108 ExpectStripped ("1,," ).to_equal ("1" )
159109 ExpectStripped ("1#comment\n , \n " ).to_equal ("1" )
160110
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-
173111 def strips_ignored_tokens_between_punctuator_tokens ():
174112 ExpectStripped ("[,)" ).to_equal ("[)" )
175113 ExpectStripped ("[\r )" ).to_equal ("[)" )
176114 ExpectStripped ("[\r \r )" ).to_equal ("[)" )
177115 ExpectStripped ("[\r ,)" ).to_equal ("[)" )
178116 ExpectStripped ("[,\n )" ).to_equal ("[)" )
179117
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-
194118 def strips_ignored_tokens_between_punctuator_and_non_punctuator_tokens ():
195119 ExpectStripped ("[,1" ).to_equal ("[1" )
196120 ExpectStripped ("[\r 1" ).to_equal ("[1" )
197121 ExpectStripped ("[\r \r 1" ).to_equal ("[1" )
198122 ExpectStripped ("[\r ,1" ).to_equal ("[1" )
199123 ExpectStripped ("[,\n 1" ).to_equal ("[1" )
200124
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-
217125 def strips_ignored_tokens_between_non_punctuator_and_punctuator_tokens ():
218126 ExpectStripped ("1,[" ).to_equal ("1[" )
219127 ExpectStripped ("1\r [" ).to_equal ("1[" )
220128 ExpectStripped ("1\r \r [" ).to_equal ("1[" )
221129 ExpectStripped ("1\r ,[" ).to_equal ("1[" )
222130 ExpectStripped ("1,\n [" ).to_equal ("1[" )
223131
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-
244132 def replace_ignored_tokens_between_non_punctuator_tokens_and_spread_with_space ():
245133 ExpectStripped ("a ..." ).to_equal ("a ..." )
246134 ExpectStripped ("1 ..." ).to_equal ("1 ..." )
247135 ExpectStripped ("1 ... ..." ).to_equal ("1 ......" )
248136
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-
264137 def replace_ignored_tokens_between_non_punctuator_tokens_with_space ():
265138 ExpectStripped ("1 2" ).to_stay_the_same ()
266139 ExpectStripped ('"" ""' ).to_stay_the_same ()
@@ -271,57 +144,17 @@ def replace_ignored_tokens_between_non_punctuator_tokens_with_space():
271144 ExpectStripped ("a 1" ).to_equal ("a 1" )
272145 ExpectStripped ("a \t 1" ).to_equal ("a 1" )
273146
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-
288147 def does_not_strip_ignored_tokens_embedded_in_the_string ():
289148 ExpectStripped ('" "' ).to_stay_the_same ()
290149 ExpectStripped ('","' ).to_stay_the_same ()
291150 ExpectStripped ('",,"' ).to_stay_the_same ()
292151 ExpectStripped ('",|"' ).to_stay_the_same ()
293152
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-
302153 def does_not_strip_ignored_tokens_embedded_in_the_block_string ():
303154 ExpectStripped ('""","""' ).to_stay_the_same ()
304155 ExpectStripped ('""",,"""' ).to_stay_the_same ()
305156 ExpectStripped ('""",|"""' ).to_stay_the_same ()
306157
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-
325158 def strips_ignored_characters_inside_block_strings ():
326159 # noinspection PyShadowingNames
327160 def expect_stripped_string (block_str : str ):
0 commit comments