| 1 |
from json import dumps |
| 2 |
from typing import Optional |
| 3 |
|
| 4 |
from pytest import raises |
| 5 |
|
| 6 |
from graphql.error import GraphQLSyntaxError |
| 7 |
from graphql.language import Lexer, Source, TokenKind, parse |
| 8 |
from graphql.utilities import strip_ignored_characters |
| 9 |
|
| 10 |
from ..fixtures import kitchen_sink_query, kitchen_sink_sdl # noqa: F401 |
| 11 |
from ..utils import dedent |
| 12 |
|
| 13 |
ignored_tokens = [ |
| 14 |
# UnicodeBOM |
| 15 |
"\ufeff", # Byte Order Mark (U+FEFF) |
| 16 |
# WhiteSpace |
| 17 |
"\t", # Horizontal Tab (U+0009) |
| 18 |
" ", # Space (U+0020) |
| 19 |
# LineTerminator |
| 20 |
"\n", # "New Line (U+000A)" |
| 21 |
"\r", # "Carriage Return (U+000D)" [ lookahead ! "New Line (U+000A)" ] |
| 22 |
"\r\n", # "Carriage Return (U+000D)" "New Line (U+000A)" |
| 23 |
# Comment |
| 24 |
'# "Comment" string\n', # `#` CommentChar* |
| 25 |
# Comma |
| 26 |
",", # , |
| 27 |
] |
| 28 |
|
| 29 |
punctuator_tokens = ["!", "$", "(", ")", "...", ":", "=", "@", "[", "]", "{", "|", "}"] |
| 30 |
|
| 31 |
non_punctuator_tokens = [ |
| 32 |
"name_token", # Name |
| 33 |
"1", # IntValue |
| 34 |
"3.14", # FloatValue |
| 35 |
'"some string value"', # StringValue |
| 36 |
'"""block\nstring\nvalue"""', # StringValue(BlockString) |
| 37 |
] |
| 38 |
|
| 39 |
|
| 40 |
def lex_value(s: str) -> Optional[str]: |
| 41 |
lexer = Lexer(Source(s)) |
| 42 |
value = lexer.advance().value |
| 43 |
assert lexer.advance().kind == TokenKind.EOF, "Expected EOF" |
| 44 |
return value |
| 45 |
|
| 46 |
|
| 47 |
class ExpectStripped: |
| 48 |
def __init__(self, doc_string: str): |
| 49 |
self.doc_string = doc_string |
| 50 |
|
| 51 |
def to_equal(self, expected: str): |
| 52 |
doc_string = self.doc_string |
| 53 |
stripped = strip_ignored_characters(doc_string) |
| 54 |
|
| 55 |
assert stripped == expected, dedent(f""" |
| 56 |
Expected strip_ignored_characters({doc_string!r}) |
| 57 |
to equal {expected!r} |
| 58 |
but got {stripped!r} |
| 59 |
""") |
| 60 |
|
| 61 |
stripped_twice = strip_ignored_characters(stripped) |
| 62 |
|
| 63 |
assert stripped == stripped_twice, dedent(f"""" |
| 64 |
Expected strip_ignored_characters({stripped!r})" |
| 65 |
to equal {stripped!r} |
| 66 |
but got {stripped_twice!r} |
| 67 |
""") |
| 68 |
|
| 69 |
def to_stay_the_same(self): |
| 70 |
self.to_equal(self.doc_string) |
| 71 |
|
| 72 |
|
| 73 |
def describe_strip_ignored_characters(): |
| 74 |
def strips_ignored_characters_from_graphql_query_document(): |
| 75 |
query = dedent(""" |
| 76 |
query SomeQuery($foo: String!, $bar: String) { |
| 77 |
someField(foo: $foo, bar: $bar) { |
| 78 |
a |
| 79 |
b { |
| 80 |
c |
| 81 |
d |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
""") |
| 86 |
|
| 87 |
assert strip_ignored_characters(query) == ( |
| 88 |
"query SomeQuery($foo:String!$bar:String)" |
| 89 |
"{someField(foo:$foo bar:$bar){a b{c d}}}" |
| 90 |
) |
| 91 |
|
| 92 |
def strips_ignored_characters_from_graphql_sdl_document(): |
| 93 |
sdl = dedent(''' |
| 94 |
""" |
| 95 |
Type description |
| 96 |
""" |
| 97 |
type Foo { |
| 98 |
""" |
| 99 |
Field description |
| 100 |
""" |
| 101 |
bar: String |
| 102 |
} |
| 103 |
''') |
| 104 |
|
| 105 |
assert strip_ignored_characters(sdl) == ( |
| 106 |
'"""Type description""" type Foo{"""Field description""" bar:String}' |
| 107 |
) |
| 108 |
|
| 109 |
def report_document_with_invalid_token(): |
| 110 |
with raises(GraphQLSyntaxError) as exc_info: |
| 111 |
strip_ignored_characters('{ foo(arg: "\n"') |
| 112 |
|
| 113 |
assert str(exc_info.value) == dedent(""" |
| 114 |
Syntax Error: Unterminated string. |
| 115 |
|
| 116 |
GraphQL request:1:13 |
| 117 |
1 | { foo(arg: " |
| 118 |
| ^ |
| 119 |
2 | " |
| 120 |
""") |
| 121 |
|
| 122 |
def strips_non_parsable_document(): |
| 123 |
ExpectStripped('{ foo(arg: "str"').to_equal('{foo(arg:"str"') |
| 124 |
|
| 125 |
def strips_documents_with_only_ignored_characters(): |
| 126 |
ExpectStripped("\n").to_equal("") |
| 127 |
ExpectStripped(",").to_equal("") |
| 128 |
ExpectStripped(",,").to_equal("") |
| 129 |
ExpectStripped("#comment\n, \n").to_equal("") |
| 130 |
|
| 131 |
for ignored in ignored_tokens: |
| 132 |
ExpectStripped(ignored).to_equal("") |
| 133 |
|
| 134 |
for another_ignored in ignored_tokens: |
| 135 |
ExpectStripped(ignored + another_ignored).to_equal("") |
| 136 |
|
| 137 |
ExpectStripped("".join(ignored_tokens)).to_equal("") |
| 138 |
|
| 139 |
def strips_leading_and_trailing_ignored_tokens(): |
| 140 |
ExpectStripped("\n1").to_equal("1") |
| 141 |
ExpectStripped(",1").to_equal("1") |
| 142 |
ExpectStripped(",,1").to_equal("1") |
| 143 |
ExpectStripped("#comment\n, \n1").to_equal("1") |
| 144 |
|
| 145 |
ExpectStripped("1\n").to_equal("1") |
| 146 |
ExpectStripped("1,").to_equal("1") |
| 147 |
ExpectStripped("1,,").to_equal("1") |
| 148 |
ExpectStripped("1#comment\n, \n").to_equal("1") |
| 149 |
|
| 150 |
for token in punctuator_tokens + non_punctuator_tokens: |
| 151 |
for ignored in ignored_tokens: |
| 152 |
ExpectStripped(ignored + token).to_equal(token) |
| 153 |
ExpectStripped(token + ignored).to_equal(token) |
| 154 |
|
| 155 |
for another_ignored in ignored_tokens: |
| 156 |
ExpectStripped(token + ignored + ignored).to_equal(token) |
| 157 |
ExpectStripped(ignored + another_ignored + token).to_equal(token) |
| 158 |
|
| 159 |
ExpectStripped("".join(ignored_tokens) + token).to_equal(token) |
| 160 |
ExpectStripped(token + "".join(ignored_tokens)).to_equal(token) |
| 161 |
|
| 162 |
def strips_ignored_tokens_between_punctuator_tokens(): |
| 163 |
ExpectStripped("[,)").to_equal("[)") |
| 164 |
ExpectStripped("[\r)").to_equal("[)") |
| 165 |
ExpectStripped("[\r\r)").to_equal("[)") |
| 166 |
ExpectStripped("[\r,)").to_equal("[)") |
| 167 |
ExpectStripped("[,\n)").to_equal("[)") |
| 168 |
|
| 169 |
for left in punctuator_tokens: |
| 170 |
for right in punctuator_tokens: |
| 171 |
for ignored in ignored_tokens: |
| 172 |
ExpectStripped(left + ignored + right).to_equal(left + right) |
| 173 |
|
| 174 |
for another_ignored in ignored_tokens: |
| 175 |
ExpectStripped( |
| 176 |
left + ignored + another_ignored + right |
| 177 |
).to_equal(left + right) |
| 178 |
|
| 179 |
ExpectStripped(left + "".join(ignored_tokens) + right).to_equal( |
| 180 |
left + right |
| 181 |
) |
| 182 |
|
| 183 |
def strips_ignored_tokens_between_punctuator_and_non_punctuator_tokens(): |
| 184 |
ExpectStripped("[,1").to_equal("[1") |
| 185 |
ExpectStripped("[\r1").to_equal("[1") |
| 186 |
ExpectStripped("[\r\r1").to_equal("[1") |
| 187 |
ExpectStripped("[\r,1").to_equal("[1") |
| 188 |
ExpectStripped("[,\n1").to_equal("[1") |
| 189 |
|
| 190 |
for non_punctuator in non_punctuator_tokens: |
| 191 |
for punctuator in punctuator_tokens: |
| 192 |
for ignored in ignored_tokens: |
| 193 |
ExpectStripped(punctuator + ignored + non_punctuator).to_equal( |
| 194 |
punctuator + non_punctuator |
| 195 |
) |
| 196 |
|
| 197 |
for another_ignored in ignored_tokens: |
| 198 |
ExpectStripped( |
| 199 |
punctuator + ignored + another_ignored + non_punctuator |
| 200 |
).to_equal(punctuator + non_punctuator) |
| 201 |
|
| 202 |
ExpectStripped( |
| 203 |
punctuator + "".join(ignored_tokens) + non_punctuator |
| 204 |
).to_equal(punctuator + non_punctuator) |
| 205 |
|
| 206 |
def strips_ignored_tokens_between_non_punctuator_and_punctuator_tokens(): |
| 207 |
ExpectStripped("1,[").to_equal("1[") |
| 208 |
ExpectStripped("1\r[").to_equal("1[") |
| 209 |
ExpectStripped("1\r\r[").to_equal("1[") |
| 210 |
ExpectStripped("1\r,[").to_equal("1[") |
| 211 |
ExpectStripped("1,\n[").to_equal("1[") |
| 212 |
|
| 213 |
for non_punctuator in non_punctuator_tokens: |
| 214 |
for punctuator in punctuator_tokens: |
| 215 |
# Special case for that is handled in the below test |
| 216 |
if punctuator == "...": |
| 217 |
continue |
| 218 |
|
| 219 |
for ignored in ignored_tokens: |
| 220 |
ExpectStripped(non_punctuator + ignored + punctuator).to_equal( |
| 221 |
non_punctuator + punctuator |
| 222 |
) |
| 223 |
|
| 224 |
for another_ignored in ignored_tokens: |
| 225 |
ExpectStripped( |
| 226 |
non_punctuator + ignored + another_ignored + punctuator |
| 227 |
).to_equal(non_punctuator + punctuator) |
| 228 |
|
| 229 |
ExpectStripped( |
| 230 |
non_punctuator + "".join(ignored_tokens) + punctuator |
| 231 |
).to_equal(non_punctuator + punctuator) |
| 232 |
|
| 233 |
def replace_ignored_tokens_between_non_punctuator_tokens_and_spread_with_space(): |
| 234 |
ExpectStripped("a ...").to_equal("a ...") |
| 235 |
ExpectStripped("1 ...").to_equal("1 ...") |
| 236 |
ExpectStripped("1 ... ...").to_equal("1 ......") |
| 237 |
|
| 238 |
for non_punctuator in non_punctuator_tokens: |
| 239 |
for ignored in ignored_tokens: |
| 240 |
ExpectStripped(non_punctuator + ignored + "...").to_equal( |
| 241 |
non_punctuator + " ..." |
| 242 |
) |
| 243 |
|
| 244 |
for another_ignored in ignored_tokens: |
| 245 |
ExpectStripped( |
| 246 |
non_punctuator + ignored + another_ignored + " ..." |
| 247 |
).to_equal(non_punctuator + " ...") |
| 248 |
|
| 249 |
ExpectStripped(non_punctuator + "".join(ignored_tokens) + "...").to_equal( |
| 250 |
non_punctuator + " ..." |
| 251 |
) |
| 252 |
|
| 253 |
def replace_ignored_tokens_between_non_punctuator_tokens_with_space(): |
| 254 |
ExpectStripped("1 2").to_stay_the_same() |
| 255 |
ExpectStripped('"" ""').to_stay_the_same() |
| 256 |
ExpectStripped("a b").to_stay_the_same() |
| 257 |
|
| 258 |
ExpectStripped("a,1").to_equal("a 1") |
| 259 |
ExpectStripped("a,,1").to_equal("a 1") |
| 260 |
ExpectStripped("a 1").to_equal("a 1") |
| 261 |
ExpectStripped("a \t 1").to_equal("a 1") |
| 262 |
|
| 263 |
for left in non_punctuator_tokens: |
| 264 |
for right in non_punctuator_tokens: |
| 265 |
for ignored in ignored_tokens: |
| 266 |
ExpectStripped(left + ignored + right).to_equal(left + " " + right) |
| 267 |
|
| 268 |
for another_ignored in ignored_tokens: |
| 269 |
ExpectStripped( |
| 270 |
left + ignored + another_ignored + right |
| 271 |
).to_equal(left + " " + right) |
| 272 |
|
| 273 |
ExpectStripped(left + "".join(ignored_tokens) + right).to_equal( |
| 274 |
left + " " + right |
| 275 |
) |
| 276 |
|
| 277 |
def does_not_strip_ignored_tokens_embedded_in_the_string(): |
| 278 |
ExpectStripped('" "').to_stay_the_same() |
| 279 |
ExpectStripped('","').to_stay_the_same() |
| 280 |
ExpectStripped('",,"').to_stay_the_same() |
| 281 |
ExpectStripped('",|"').to_stay_the_same() |
| 282 |
|
| 283 |
for ignored in ignored_tokens: |
| 284 |
ExpectStripped(dumps(ignored)).to_stay_the_same() |
| 285 |
|
| 286 |
for another_ignored in ignored_tokens: |
| 287 |
ExpectStripped(dumps(ignored + another_ignored)).to_stay_the_same() |
| 288 |
|
| 289 |
ExpectStripped(dumps("".join(ignored_tokens))).to_stay_the_same() |
| 290 |
|
| 291 |
def does_not_strip_ignored_tokens_embedded_in_the_block_string(): |
| 292 |
ExpectStripped('""","""').to_stay_the_same() |
| 293 |
ExpectStripped('""",,"""').to_stay_the_same() |
| 294 |
ExpectStripped('""",|"""').to_stay_the_same() |
| 295 |
|
| 296 |
ignored_tokens_without_formatting = [ |
| 297 |
token |
| 298 |
for token in ignored_tokens |
| 299 |
if token not in ["\n", "\r", "\r\n", "\t", " "] |
| 300 |
] |
| 301 |
|
| 302 |
for ignored in ignored_tokens_without_formatting: |
| 303 |
ExpectStripped('"""|' + ignored + '|"""').to_stay_the_same() |
| 304 |
|
| 305 |
for another_ignored in ignored_tokens_without_formatting: |
| 306 |
ExpectStripped( |
| 307 |
'"""|' + ignored + another_ignored + '|"""' |
| 308 |
).to_stay_the_same() |
| 309 |
|
| 310 |
ExpectStripped( |
| 311 |
'"""|' + "".join(ignored_tokens_without_formatting) + '|"""' |
| 312 |
).to_stay_the_same() |
| 313 |
|
| 314 |
def strips_ignored_characters_inside_block_strings(): |
| 315 |
# noinspection PyShadowingNames |
| 316 |
def expect_stripped_string(block_str: str): |
| 317 |
original_value = lex_value(block_str) |
| 318 |
stripped_value = lex_value(strip_ignored_characters(block_str)) |
| 319 |
|
| 320 |
assert original_value == stripped_value, dedent(f""" |
| 321 |
Expected lexValue(stripIgnoredCharacters({block_str!r}) |
| 322 |
to equal {original_value!r} |
| 323 |
but got {stripped_value!r} |
| 324 |
""") |
| 325 |
return ExpectStripped(block_str) |
| 326 |
|
| 327 |
expect_stripped_string('""""""').to_stay_the_same() |
| 328 |
expect_stripped_string('""" """').to_equal('""""""') |
| 329 |
|
| 330 |
expect_stripped_string('"""a"""').to_stay_the_same() |
| 331 |
expect_stripped_string('""" a"""').to_equal('""" a"""') |
| 332 |
expect_stripped_string('""" a """').to_equal('""" a """') |
| 333 |
|
| 334 |
expect_stripped_string('"""\n"""').to_equal('""""""') |
| 335 |
expect_stripped_string('"""a\nb"""').to_equal('"""a\nb"""') |
| 336 |
expect_stripped_string('"""a\rb"""').to_equal('"""a\nb"""') |
| 337 |
expect_stripped_string('"""a\r\nb"""').to_equal('"""a\nb"""') |
| 338 |
expect_stripped_string('"""a\r\n\nb"""').to_equal('"""a\n\nb"""') |
| 339 |
|
| 340 |
expect_stripped_string('"""\\\n"""').to_stay_the_same() |
| 341 |
expect_stripped_string('""""\n"""').to_stay_the_same() |
| 342 |
expect_stripped_string('"""\\"""\n"""').to_equal('"""\\""""""') |
| 343 |
|
| 344 |
expect_stripped_string('"""\na\n b"""').to_stay_the_same() |
| 345 |
expect_stripped_string('"""\n a\n b"""').to_equal('"""a\nb"""') |
| 346 |
expect_stripped_string('"""\na\n b\nc"""').to_equal('"""a\n b\nc"""') |
| 347 |
|
| 348 |
# noinspection PyShadowingNames |
| 349 |
def strips_kitchen_sink_query_but_maintains_the_exact_same_ast( |
| 350 |
kitchen_sink_query, # noqa: F811 |
| 351 |
): |
| 352 |
stripped_query = strip_ignored_characters(kitchen_sink_query) |
| 353 |
assert strip_ignored_characters(stripped_query) == stripped_query |
| 354 |
|
| 355 |
query_ast = parse(kitchen_sink_query, no_location=True) |
| 356 |
stripped_ast = parse(stripped_query, no_location=True) |
| 357 |
assert stripped_ast == query_ast |
| 358 |
|
| 359 |
# noinspection PyShadowingNames |
| 360 |
def strips_kitchen_sink_sdl_but_maintains_the_exact_same_ast( |
| 361 |
kitchen_sink_sdl, # noqa: F811 |
| 362 |
): |
| 363 |
stripped_sdl = strip_ignored_characters(kitchen_sink_sdl) |
| 364 |
assert strip_ignored_characters(stripped_sdl) == stripped_sdl |
| 365 |
|
| 366 |
sdl_ast = parse(kitchen_sink_sdl, no_location=True) |
| 367 |
stripped_ast = parse(stripped_sdl, no_location=True) |
| 368 |
assert stripped_ast == sdl_ast |