| 1 |
import unittest |
| 2 |
import textwrap |
| 3 |
|
| 4 |
from pygments.lexers import GraphQLLexer |
| 5 |
from pygments.token import ( |
| 6 |
Error, |
| 7 |
Keyword, |
| 8 |
Name, |
| 9 |
Number, |
| 10 |
Punctuation, |
| 11 |
String, |
| 12 |
Whitespace |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
def assert_tokens_equal(self, text, expected, stack=("root",)): |
| 17 |
text = textwrap.dedent(text) |
| 18 |
lexer = GraphQLLexer() |
| 19 |
tokens_itr = lexer.get_tokens_unprocessed(text, stack=stack) |
| 20 |
self.assertEqual(list(tokens_itr), expected) |
| 21 |
|
| 22 |
|
| 23 |
def assert_value_equal(self, text, expected): |
| 24 |
if not isinstance(expected, list): |
| 25 |
expected = [expected] |
| 26 |
assert_tokens_equal(self, text, expected, stack=("value",)) |
| 27 |
|
| 28 |
|
| 29 |
def assert_string_equal(self, text, tokens): |
| 30 |
start = (0, String, '"') |
| 31 |
end = (len(text) - 1, String, '"') |
| 32 |
expected = [start, *tokens, end] |
| 33 |
assert_tokens_equal(self, text, expected, stack=("value",)) |
| 34 |
|
| 35 |
|
| 36 |
class TestValue(unittest.TestCase): |
| 37 |
|
| 38 |
maxDiff = None |
| 39 |
|
| 40 |
def test_float(self): |
| 41 |
assert_value_equal(self, "5.5", (0, Number.Float, "5.5")) |
| 42 |
assert_value_equal(self, "5e3", (0, Number.Float, "5e3")) |
| 43 |
assert_value_equal(self, "5E3", (0, Number.Float, "5E3")) |
| 44 |
assert_value_equal(self, "5e-3", (0, Number.Float, "5e-3")) |
| 45 |
assert_value_equal(self, "5E-3", (0, Number.Float, "5E-3")) |
| 46 |
assert_value_equal(self, "5e+3", (0, Number.Float, "5e+3")) |
| 47 |
assert_value_equal(self, "5E+3", (0, Number.Float, "5E+3")) |
| 48 |
assert_value_equal(self, "5.1e3", (0, Number.Float, "5.1e3")) |
| 49 |
assert_value_equal(self, "5.1E3", (0, Number.Float, "5.1E3")) |
| 50 |
assert_value_equal(self, "5.1e-3", (0, Number.Float, "5.1e-3")) |
| 51 |
assert_value_equal(self, "5.1E-3", (0, Number.Float, "5.1E-3")) |
| 52 |
assert_value_equal(self, "5.1e+3", (0, Number.Float, "5.1e+3")) |
| 53 |
assert_value_equal(self, "5.1E+3", (0, Number.Float, "5.1E+3")) |
| 54 |
|
| 55 |
def test_integer(self): |
| 56 |
assert_value_equal(self, "5", (0, Number.Integer, "5")) |
| 57 |
assert_value_equal(self, "-0", (0, Number.Integer, "-0")) |
| 58 |
# Not really following the specifications |
| 59 |
assert_value_equal(self, "-02", (0, Number.Integer, "-02")) |
| 60 |
|
| 61 |
def test_string(self): |
| 62 |
assert_string_equal(self, '"asdf"', [(1, String, "asdf")]) |
| 63 |
assert_string_equal( |
| 64 |
self, |
| 65 |
r'"asdf\""', |
| 66 |
[ |
| 67 |
(1, String, "asdf"), |
| 68 |
(5, String.Escape, r"\""), |
| 69 |
], |
| 70 |
) |
| 71 |
assert_string_equal( |
| 72 |
self, r'"asdf\m"', [(1, String, "asdf"), (5, Error, "\\"), (6, String, "m")] |
| 73 |
) |
| 74 |
assert_string_equal( |
| 75 |
self, |
| 76 |
r'"asdf\u00E8"', |
| 77 |
[ |
| 78 |
(1, String, "asdf"), |
| 79 |
(5, String.Escape, r"\u00E8"), |
| 80 |
], |
| 81 |
) |
| 82 |
|
| 83 |
def test_boolean(self): |
| 84 |
assert_value_equal(self, "true", (0, Name.Builtin, "true")) |
| 85 |
assert_value_equal(self, "false", (0, Name.Builtin, "false")) |
| 86 |
|
| 87 |
def test_variable(self): |
| 88 |
assert_value_equal(self, "$hello", (0, Name.Variable, "$hello")) |
| 89 |
assert_value_equal( |
| 90 |
self, |
| 91 |
"$0hello", |
| 92 |
[ |
| 93 |
(0, Error, "$"), |
| 94 |
(1, Number.Integer, "0"), |
| 95 |
(2, Name.Constant, "hello"), |
| 96 |
], |
| 97 |
) |
| 98 |
|
| 99 |
def test_list_value(self): |
| 100 |
assert_value_equal( |
| 101 |
self, |
| 102 |
'[1, 1.1, "abc"]', |
| 103 |
[ |
| 104 |
(0, Punctuation, "["), |
| 105 |
(1, Number.Integer, "1"), |
| 106 |
(2, Punctuation, ","), |
| 107 |
(3, Whitespace, " "), |
| 108 |
(4, Number.Float, "1.1"), |
| 109 |
(7, Punctuation, ","), |
| 110 |
(8, Whitespace, " "), |
| 111 |
(9, String, '"'), |
| 112 |
(10, String, "abc"), |
| 113 |
(13, String, '"'), |
| 114 |
(14, Punctuation, "]"), |
| 115 |
], |
| 116 |
) |
| 117 |
assert_value_equal( |
| 118 |
self, |
| 119 |
'[1, 1.1, ["abc"]]', |
| 120 |
[ |
| 121 |
(0, Punctuation, "["), |
| 122 |
(1, Number.Integer, "1"), |
| 123 |
(2, Punctuation, ","), |
| 124 |
(3, Whitespace, " "), |
| 125 |
(4, Number.Float, "1.1"), |
| 126 |
(7, Punctuation, ","), |
| 127 |
(8, Whitespace, " "), |
| 128 |
(9, Punctuation, "["), |
| 129 |
(10, String, '"'), |
| 130 |
(11, String, "abc"), |
| 131 |
(14, String, '"'), |
| 132 |
(15, Punctuation, "]"), |
| 133 |
(16, Punctuation, "]"), |
| 134 |
], |
| 135 |
) |
| 136 |
|
| 137 |
|
| 138 |
class TestGraphqlLexer(unittest.TestCase): |
| 139 |
|
| 140 |
maxDiff = None |
| 141 |
|
| 142 |
def assert_builtin(self, text): |
| 143 |
assert_tokens_equal(self, text, [(0, Name.Builtin, text)], stack=("builtins",)) |
| 144 |
|
| 145 |
def test_keyworkd(self): |
| 146 |
assert_tokens_equal(self, "type", [(0, Keyword, "type")]) |
| 147 |
assert_tokens_equal(self, "schema", [(0, Keyword, "schema")]) |
| 148 |
assert_tokens_equal(self, "extend", [(0, Keyword, "extend")]) |
| 149 |
assert_tokens_equal(self, "enum", [(0, Keyword, "enum")]) |
| 150 |
assert_tokens_equal(self, "scalar", [(0, Keyword, "scalar")]) |
| 151 |
assert_tokens_equal(self, "implements", [(0, Keyword, "implements")]) |
| 152 |
assert_tokens_equal(self, "interface", [(0, Keyword, "interface")]) |
| 153 |
assert_tokens_equal(self, "union", [(0, Keyword, "union")]) |
| 154 |
assert_tokens_equal(self, "input", [(0, Keyword, "input")]) |
| 155 |
assert_tokens_equal(self, "directive", [(0, Keyword, "directive")]) |
| 156 |
|
| 157 |
def test_arguments_integer(self): |
| 158 |
text = """\ |
| 159 |
{ |
| 160 |
field(arg: 24) |
| 161 |
field |
| 162 |
} |
| 163 |
""" |
| 164 |
|
| 165 |
expected = [ |
| 166 |
(0, Punctuation, "{"), |
| 167 |
(1, Whitespace, "\n "), |
| 168 |
(4, Name, "field"), |
| 169 |
(9, Punctuation, "("), |
| 170 |
(10, Name, "arg"), |
| 171 |
# (10, Name.Attribute, 'arg'), |
| 172 |
(13, Punctuation, ":"), |
| 173 |
(14, Whitespace, " "), |
| 174 |
(15, Number.Integer, "24"), |
| 175 |
(17, Punctuation, ")"), |
| 176 |
(18, Whitespace, "\n "), |
| 177 |
(21, Name, "field"), |
| 178 |
(26, Whitespace, "\n"), |
| 179 |
(27, Punctuation, "}"), |
| 180 |
(28, Whitespace, "\n"), |
| 181 |
] |
| 182 |
assert_tokens_equal(self, text, expected) |
| 183 |
|
| 184 |
def test_arguments(self): |
| 185 |
text = """\ |
| 186 |
{ |
| 187 |
human(id: "1000") { |
| 188 |
name |
| 189 |
height |
| 190 |
} |
| 191 |
} |
| 192 |
""" |
| 193 |
expected = [ |
| 194 |
(0, Punctuation, "{"), |
| 195 |
(1, Whitespace, "\n "), |
| 196 |
(4, Name, "human"), |
| 197 |
(9, Punctuation, "("), |
| 198 |
# (10, Name.Attribute, 'id'), |
| 199 |
(10, Name, "id"), |
| 200 |
(12, Punctuation, ":"), |
| 201 |
(13, Whitespace, " "), |
| 202 |
(14, String, '"'), |
| 203 |
(15, String, "1000"), |
| 204 |
(19, String, '"'), |
| 205 |
(20, Punctuation, ")"), |
| 206 |
(21, Whitespace, " "), |
| 207 |
(22, Punctuation, "{"), |
| 208 |
(23, Whitespace, "\n "), |
| 209 |
(28, Name, "name"), |
| 210 |
(32, Whitespace, "\n "), |
| 211 |
(37, Name, "height"), |
| 212 |
(43, Whitespace, "\n "), |
| 213 |
(46, Punctuation, "}"), |
| 214 |
(47, Whitespace, "\n"), |
| 215 |
(48, Punctuation, "}"), |
| 216 |
(49, Whitespace, "\n"), |
| 217 |
] |
| 218 |
assert_tokens_equal(self, text, expected) |
| 219 |
|
| 220 |
def test_argument_scalar(self): |
| 221 |
text = """\ |
| 222 |
{ |
| 223 |
human(id: "1000") { |
| 224 |
name |
| 225 |
height(unit: FOOT) |
| 226 |
} |
| 227 |
} |
| 228 |
""" |
| 229 |
expected = [ |
| 230 |
(0, Punctuation, "{"), |
| 231 |
(1, Whitespace, "\n "), |
| 232 |
(4, Name, "human"), |
| 233 |
(9, Punctuation, "("), |
| 234 |
# (10, Name.Attribute, 'id'), |
| 235 |
(10, Name, "id"), |
| 236 |
(12, Punctuation, ":"), |
| 237 |
(13, Whitespace, " "), |
| 238 |
(14, String, '"'), |
| 239 |
(15, String, "1000"), |
| 240 |
(19, String, '"'), |
| 241 |
(20, Punctuation, ")"), |
| 242 |
(21, Whitespace, " "), |
| 243 |
(22, Punctuation, "{"), |
| 244 |
(23, Whitespace, "\n "), |
| 245 |
(28, Name, "name"), |
| 246 |
(32, Whitespace, "\n "), |
| 247 |
(37, Name, "height"), |
| 248 |
(43, Punctuation, "("), |
| 249 |
# (44, Name.Attribute, 'unit'), |
| 250 |
(44, Name, "unit"), |
| 251 |
(48, Punctuation, ":"), |
| 252 |
(49, Whitespace, " "), |
| 253 |
(50, Name.Constant, "FOOT"), |
| 254 |
(54, Punctuation, ")"), |
| 255 |
(55, Whitespace, "\n "), |
| 256 |
(58, Punctuation, "}"), |
| 257 |
(59, Whitespace, "\n"), |
| 258 |
(60, Punctuation, "}"), |
| 259 |
(61, Whitespace, "\n"), |
| 260 |
] |
| 261 |
assert_tokens_equal(self, text, expected) |
| 262 |
|
| 263 |
def test_alias(self): |
| 264 |
text = """\ |
| 265 |
{ |
| 266 |
empireHero: hero(episode: EMPIRE) { |
| 267 |
name |
| 268 |
} |
| 269 |
jediHero: hero(episode: JEDI) { |
| 270 |
name |
| 271 |
} |
| 272 |
} |
| 273 |
""" |
| 274 |
expected = [ |
| 275 |
(0, Punctuation, "{"), |
| 276 |
(1, Whitespace, "\n "), |
| 277 |
(4, Name.Label, "empireHero"), |
| 278 |
(14, Punctuation, ":"), |
| 279 |
(15, Whitespace, " "), |
| 280 |
(16, Name, "hero"), |
| 281 |
(20, Punctuation, "("), |
| 282 |
# (21, Name.Attribute, 'episode'), |
| 283 |
(21, Name, "episode"), |
| 284 |
(28, Punctuation, ":"), |
| 285 |
(29, Whitespace, " "), |
| 286 |
(30, Name.Constant, "EMPIRE"), |
| 287 |
(36, Punctuation, ")"), |
| 288 |
(37, Whitespace, " "), |
| 289 |
(38, Punctuation, "{"), |
| 290 |
(39, Whitespace, "\n "), |
| 291 |
(44, Name, "name"), |
| 292 |
(48, Whitespace, "\n "), |
| 293 |
(51, Punctuation, "}"), |
| 294 |
(52, Whitespace, "\n "), |
| 295 |
(55, Name.Label, "jediHero"), |
| 296 |
(63, Punctuation, ":"), |
| 297 |
(64, Whitespace, " "), |
| 298 |
(65, Name, "hero"), |
| 299 |
(69, Punctuation, "("), |
| 300 |
# (70, Name.Attribute, 'episode'), |
| 301 |
(70, Name, "episode"), |
| 302 |
(77, Punctuation, ":"), |
| 303 |
(78, Whitespace, " "), |
| 304 |
(79, Name.Constant, "JEDI"), |
| 305 |
(83, Punctuation, ")"), |
| 306 |
(84, Whitespace, " "), |
| 307 |
(85, Punctuation, "{"), |
| 308 |
(86, Whitespace, "\n "), |
| 309 |
(91, Name, "name"), |
| 310 |
(95, Whitespace, "\n "), |
| 311 |
(98, Punctuation, "}"), |
| 312 |
(99, Whitespace, "\n"), |
| 313 |
(100, Punctuation, "}"), |
| 314 |
(101, Whitespace, "\n"), |
| 315 |
] |
| 316 |
assert_tokens_equal(self, text, expected) |
| 317 |
|
| 318 |
def test_variables(self): |
| 319 |
text = """\ |
| 320 |
mutation testVar($int: Int, $myType: MyType, |
| 321 |
$required: String!, $list: [Int!]!) |
| 322 |
{ |
| 323 |
doSomenthing(int: $int, myType: $myType, |
| 324 |
required: $required, list: $list) |
| 325 |
{ |
| 326 |
response |
| 327 |
} |
| 328 |
} |
| 329 |
""" |
| 330 |
expected = [ |
| 331 |
(0, Keyword, "mutation"), |
| 332 |
(8, Whitespace, " "), |
| 333 |
(9, Name.Function, "testVar"), |
| 334 |
(16, Punctuation, "("), |
| 335 |
(17, Name.Variable, "$int"), |
| 336 |
(21, Punctuation, ":"), |
| 337 |
(22, Whitespace, " "), |
| 338 |
(23, Name.Builtin, "Int"), |
| 339 |
(26, Punctuation, ","), |
| 340 |
(27, Whitespace, " "), |
| 341 |
(28, Name.Variable, "$myType"), |
| 342 |
(35, Punctuation, ":"), |
| 343 |
(36, Whitespace, " "), |
| 344 |
(37, Name.Class, "MyType"), |
| 345 |
(43, Punctuation, ","), |
| 346 |
(44, Whitespace, "\n "), |
| 347 |
(49, Name.Variable, "$required"), |
| 348 |
(58, Punctuation, ":"), |
| 349 |
(59, Whitespace, " "), |
| 350 |
(60, Name.Builtin, "String"), |
| 351 |
(66, Punctuation, "!"), |
| 352 |
(67, Punctuation, ","), |
| 353 |
(68, Whitespace, " "), |
| 354 |
(69, Name.Variable, "$list"), |
| 355 |
(74, Punctuation, ":"), |
| 356 |
(75, Whitespace, " "), |
| 357 |
(76, Punctuation, "["), |
| 358 |
(77, Name.Builtin, "Int"), |
| 359 |
(80, Punctuation, "!"), |
| 360 |
(81, Punctuation, "]"), |
| 361 |
(82, Punctuation, "!"), |
| 362 |
(83, Punctuation, ")"), |
| 363 |
(84, Whitespace, "\n"), |
| 364 |
(85, Punctuation, "{"), |
| 365 |
(86, Whitespace, "\n "), |
| 366 |
(89, Name, "doSomenthing"), |
| 367 |
(101, Punctuation, "("), |
| 368 |
(102, Name, "int"), |
| 369 |
(105, Punctuation, ":"), |
| 370 |
(106, Whitespace, " "), |
| 371 |
(107, Name.Variable, "$int"), |
| 372 |
(111, Punctuation, ","), |
| 373 |
(112, Whitespace, " "), |
| 374 |
(113, Name, "myType"), |
| 375 |
(119, Punctuation, ":"), |
| 376 |
(120, Whitespace, " "), |
| 377 |
(121, Name.Variable, "$myType"), |
| 378 |
(128, Punctuation, ","), |
| 379 |
(129, Whitespace, "\n "), |
| 380 |
(136, Name, "required"), |
| 381 |
(144, Punctuation, ":"), |
| 382 |
(145, Whitespace, " "), |
| 383 |
(146, Name.Variable, "$required"), |
| 384 |
(155, Punctuation, ","), |
| 385 |
(156, Whitespace, " "), |
| 386 |
(157, Name, "list"), |
| 387 |
(161, Punctuation, ":"), |
| 388 |
(162, Whitespace, " "), |
| 389 |
(163, Name.Variable, "$list"), |
| 390 |
(168, Punctuation, ")"), |
| 391 |
(169, Whitespace, "\n "), |
| 392 |
(172, Punctuation, "{"), |
| 393 |
(173, Whitespace, "\n "), |
| 394 |
(178, Name, "response"), |
| 395 |
(186, Whitespace, "\n "), |
| 396 |
(189, Punctuation, "}"), |
| 397 |
(190, Whitespace, "\n"), |
| 398 |
(191, Punctuation, "}"), |
| 399 |
(192, Whitespace, "\n"), |
| 400 |
] |
| 401 |
assert_tokens_equal(self, text, expected) |
| 402 |
|
| 403 |
def test_operation_definition_directives(self): |
| 404 |
text = """\ |
| 405 |
query ($foo: Boolean = true, $bar: Boolean = false) { |
| 406 |
field @skip(if: $foo) { |
| 407 |
subfieldA |
| 408 |
} |
| 409 |
field @skip(if: $bar) { |
| 410 |
subfieldB |
| 411 |
} |
| 412 |
} |
| 413 |
""" |
| 414 |
expected = [ |
| 415 |
(0, Keyword, "query"), |
| 416 |
(5, Whitespace, " "), |
| 417 |
(6, Punctuation, "("), |
| 418 |
(7, Name.Variable, "$foo"), |
| 419 |
(11, Punctuation, ":"), |
| 420 |
(12, Whitespace, " "), |
| 421 |
(13, Name.Builtin, "Boolean"), |
| 422 |
(20, Whitespace, " "), |
| 423 |
(21, Punctuation, "="), |
| 424 |
(22, Whitespace, " "), |
| 425 |
(23, Name.Builtin, "true"), |
| 426 |
(27, Punctuation, ","), |
| 427 |
(28, Whitespace, " "), |
| 428 |
(29, Name.Variable, "$bar"), |
| 429 |
(33, Punctuation, ":"), |
| 430 |
(34, Whitespace, " "), |
| 431 |
(35, Name.Builtin, "Boolean"), |
| 432 |
(42, Whitespace, " "), |
| 433 |
(43, Punctuation, "="), |
| 434 |
(44, Whitespace, " "), |
| 435 |
(45, Name.Builtin, "false"), |
| 436 |
(50, Punctuation, ")"), |
| 437 |
(51, Whitespace, " "), |
| 438 |
(52, Punctuation, "{"), |
| 439 |
(53, Whitespace, "\n "), |
| 440 |
(56, Name, "field"), |
| 441 |
(61, Whitespace, " "), |
| 442 |
(62, Name.Decorator, "@skip"), |
| 443 |
(67, Punctuation, "("), |
| 444 |
(68, Name, "if"), |
| 445 |
(70, Punctuation, ":"), |
| 446 |
(71, Whitespace, " "), |
| 447 |
(72, Name.Variable, "$foo"), |
| 448 |
(76, Punctuation, ")"), |
| 449 |
(77, Whitespace, " "), |
| 450 |
(78, Punctuation, "{"), |
| 451 |
(79, Whitespace, "\n "), |
| 452 |
(84, Name, "subfieldA"), |
| 453 |
(93, Whitespace, "\n "), |
| 454 |
(96, Punctuation, "}"), |
| 455 |
(97, Whitespace, "\n "), |
| 456 |
(100, Name, "field"), |
| 457 |
(105, Whitespace, " "), |
| 458 |
(106, Name.Decorator, "@skip"), |
| 459 |
(111, Punctuation, "("), |
| 460 |
(112, Name, "if"), |
| 461 |
(114, Punctuation, ":"), |
| 462 |
(115, Whitespace, " "), |
| 463 |
(116, Name.Variable, "$bar"), |
| 464 |
(120, Punctuation, ")"), |
| 465 |
(121, Whitespace, " "), |
| 466 |
(122, Punctuation, "{"), |
| 467 |
(123, Whitespace, "\n "), |
| 468 |
(128, Name, "subfieldB"), |
| 469 |
(137, Whitespace, "\n "), |
| 470 |
(140, Punctuation, "}"), |
| 471 |
(141, Whitespace, "\n"), |
| 472 |
(142, Punctuation, "}"), |
| 473 |
(143, Whitespace, "\n"), |
| 474 |
] |
| 475 |
assert_tokens_equal(self, text, expected) |
| 476 |
|
| 477 |
def test_operation_definition_multi_directives(self): |
| 478 |
text = """\ |
| 479 |
query ($foo: Boolean = true, $bar: Boolean = false) { |
| 480 |
field @skip(if: $foo) @skip(if: $bar) |
| 481 |
} |
| 482 |
""" |
| 483 |
expected = [ |
| 484 |
(0, Keyword, "query"), |
| 485 |
(5, Whitespace, " "), |
| 486 |
(6, Punctuation, "("), |
| 487 |
(7, Name.Variable, "$foo"), |
| 488 |
(11, Punctuation, ":"), |
| 489 |
(12, Whitespace, " "), |
| 490 |
(13, Name.Builtin, "Boolean"), |
| 491 |
(20, Whitespace, " "), |
| 492 |
(21, Punctuation, "="), |
| 493 |
(22, Whitespace, " "), |
| 494 |
(23, Name.Builtin, "true"), |
| 495 |
(27, Punctuation, ","), |
| 496 |
(28, Whitespace, " "), |
| 497 |
(29, Name.Variable, "$bar"), |
| 498 |
(33, Punctuation, ":"), |
| 499 |
(34, Whitespace, " "), |
| 500 |
(35, Name.Builtin, "Boolean"), |
| 501 |
(42, Whitespace, " "), |
| 502 |
(43, Punctuation, "="), |
| 503 |
(44, Whitespace, " "), |
| 504 |
(45, Name.Builtin, "false"), |
| 505 |
(50, Punctuation, ")"), |
| 506 |
(51, Whitespace, " "), |
| 507 |
(52, Punctuation, "{"), |
| 508 |
(53, Whitespace, "\n "), |
| 509 |
(56, Name, "field"), |
| 510 |
(61, Whitespace, " "), |
| 511 |
(62, Name.Decorator, "@skip"), |
| 512 |
(67, Punctuation, "("), |
| 513 |
(68, Name, "if"), |
| 514 |
(70, Punctuation, ":"), |
| 515 |
(71, Whitespace, " "), |
| 516 |
(72, Name.Variable, "$foo"), |
| 517 |
(76, Punctuation, ")"), |
| 518 |
(77, Whitespace, " "), |
| 519 |
(78, Name.Decorator, "@skip"), |
| 520 |
(83, Punctuation, "("), |
| 521 |
(84, Name, "if"), |
| 522 |
(86, Punctuation, ":"), |
| 523 |
(87, Whitespace, " "), |
| 524 |
(88, Name.Variable, "$bar"), |
| 525 |
(92, Punctuation, ")"), |
| 526 |
(93, Whitespace, "\n"), |
| 527 |
(94, Punctuation, "}"), |
| 528 |
(95, Whitespace, "\n"), |
| 529 |
] |
| 530 |
assert_tokens_equal(self, text, expected) |
| 531 |
|
| 532 |
def test_fragment_spread(self): |
| 533 |
text = """\ |
| 534 |
query withNestedFragments { |
| 535 |
user(id: 4) { |
| 536 |
friends(first: 10) { |
| 537 |
...friendFields |
| 538 |
} |
| 539 |
mutualFriends(first: 10) { |
| 540 |
...friendFields @skip(if: true) |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
""" |
| 545 |
expected = [ |
| 546 |
(0, Keyword, "query"), |
| 547 |
(5, Whitespace, " "), |
| 548 |
(6, Name.Function, "withNestedFragments"), |
| 549 |
(25, Whitespace, " "), |
| 550 |
(26, Punctuation, "{"), |
| 551 |
(27, Whitespace, "\n "), |
| 552 |
(30, Name, "user"), |
| 553 |
(34, Punctuation, "("), |
| 554 |
(35, Name, "id"), |
| 555 |
(37, Punctuation, ":"), |
| 556 |
(38, Whitespace, " "), |
| 557 |
(39, Number.Integer, "4"), |
| 558 |
(40, Punctuation, ")"), |
| 559 |
(41, Whitespace, " "), |
| 560 |
(42, Punctuation, "{"), |
| 561 |
(43, Whitespace, "\n "), |
| 562 |
(48, Name, "friends"), |
| 563 |
(55, Punctuation, "("), |
| 564 |
(56, Name, "first"), |
| 565 |
(61, Punctuation, ":"), |
| 566 |
(62, Whitespace, " "), |
| 567 |
(63, Number.Integer, "10"), |
| 568 |
(65, Punctuation, ")"), |
| 569 |
(66, Whitespace, " "), |
| 570 |
(67, Punctuation, "{"), |
| 571 |
(68, Whitespace, "\n "), |
| 572 |
(75, Punctuation, "..."), |
| 573 |
(78, Name, "friendFields"), |
| 574 |
(90, Whitespace, "\n "), |
| 575 |
(95, Punctuation, "}"), |
| 576 |
(96, Whitespace, "\n "), |
| 577 |
(101, Name, "mutualFriends"), |
| 578 |
(114, Punctuation, "("), |
| 579 |
(115, Name, "first"), |
| 580 |
(120, Punctuation, ":"), |
| 581 |
(121, Whitespace, " "), |
| 582 |
(122, Number.Integer, "10"), |
| 583 |
(124, Punctuation, ")"), |
| 584 |
(125, Whitespace, " "), |
| 585 |
(126, Punctuation, "{"), |
| 586 |
(127, Whitespace, "\n "), |
| 587 |
(134, Punctuation, "..."), |
| 588 |
(137, Name, "friendFields"), |
| 589 |
(149, Whitespace, " "), |
| 590 |
(150, Name.Decorator, "@skip"), |
| 591 |
(155, Punctuation, "("), |
| 592 |
(156, Name, "if"), |
| 593 |
(158, Punctuation, ":"), |
| 594 |
(159, Whitespace, " "), |
| 595 |
(160, Name.Builtin, "true"), |
| 596 |
(164, Punctuation, ")"), |
| 597 |
(165, Whitespace, "\n "), |
| 598 |
(170, Punctuation, "}"), |
| 599 |
(171, Whitespace, "\n "), |
| 600 |
(174, Punctuation, "}"), |
| 601 |
(175, Whitespace, "\n"), |
| 602 |
(176, Punctuation, "}"), |
| 603 |
(177, Whitespace, "\n"), |
| 604 |
] |
| 605 |
assert_tokens_equal(self, text, expected) |
| 606 |
|
| 607 |
def test_inline_fragment(self): |
| 608 |
text = """\ |
| 609 |
query inlineFragmentTyping { |
| 610 |
profiles(handles: ["zuck", "cocacola"]) { |
| 611 |
handle |
| 612 |
... on User { |
| 613 |
friends { |
| 614 |
count |
| 615 |
} |
| 616 |
} |
| 617 |
... on Page { |
| 618 |
likers { |
| 619 |
count |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
""" |
| 625 |
expected = [ |
| 626 |
(0, Keyword, "query"), |
| 627 |
(5, Whitespace, " "), |
| 628 |
(6, Name.Function, "inlineFragmentTyping"), |
| 629 |
(26, Whitespace, " "), |
| 630 |
(27, Punctuation, "{"), |
| 631 |
(28, Whitespace, "\n "), |
| 632 |
(31, Name, "profiles"), |
| 633 |
(39, Punctuation, "("), |
| 634 |
(40, Name, "handles"), |
| 635 |
(47, Punctuation, ":"), |
| 636 |
(48, Whitespace, " "), |
| 637 |
(49, Punctuation, "["), |
| 638 |
(50, String, '"'), |
| 639 |
(51, String, "zuck"), |
| 640 |
(55, String, '"'), |
| 641 |
(56, Punctuation, ","), |
| 642 |
(57, Whitespace, " "), |
| 643 |
(58, String, '"'), |
| 644 |
(59, String, "cocacola"), |
| 645 |
(67, String, '"'), |
| 646 |
(68, Punctuation, "]"), |
| 647 |
(69, Punctuation, ")"), |
| 648 |
(70, Whitespace, " "), |
| 649 |
(71, Punctuation, "{"), |
| 650 |
(72, Whitespace, "\n "), |
| 651 |
(77, Name, "handle"), |
| 652 |
(83, Whitespace, "\n "), |
| 653 |
(88, Punctuation, "..."), |
| 654 |
(91, Whitespace, " "), |
| 655 |
(92, Keyword, "on"), |
| 656 |
(94, Whitespace, " "), |
| 657 |
(95, Name.Class, "User"), |
| 658 |
(99, Whitespace, " "), |
| 659 |
(100, Punctuation, "{"), |
| 660 |
(101, Whitespace, "\n "), |
| 661 |
(108, Name, "friends"), |
| 662 |
(115, Whitespace, " "), |
| 663 |
(116, Punctuation, "{"), |
| 664 |
(117, Whitespace, "\n "), |
| 665 |
(126, Name, "count"), |
| 666 |
(131, Whitespace, "\n "), |
| 667 |
(138, Punctuation, "}"), |
| 668 |
(139, Whitespace, "\n "), |
| 669 |
(144, Punctuation, "}"), |
| 670 |
(145, Whitespace, "\n "), |
| 671 |
(150, Punctuation, "..."), |
| 672 |
(153, Whitespace, " "), |
| 673 |
(154, Keyword, "on"), |
| 674 |
(156, Whitespace, " "), |
| 675 |
(157, Name.Class, "Page"), |
| 676 |
(161, Whitespace, " "), |
| 677 |
(162, Punctuation, "{"), |
| 678 |
(163, Whitespace, "\n "), |
| 679 |
(170, Name, "likers"), |
| 680 |
(176, Whitespace, " "), |
| 681 |
(177, Punctuation, "{"), |
| 682 |
(178, Whitespace, "\n "), |
| 683 |
(187, Name, "count"), |
| 684 |
(192, Whitespace, "\n "), |
| 685 |
(199, Punctuation, "}"), |
| 686 |
(200, Whitespace, "\n "), |
| 687 |
(205, Punctuation, "}"), |
| 688 |
(206, Whitespace, "\n "), |
| 689 |
(209, Punctuation, "}"), |
| 690 |
(210, Whitespace, "\n"), |
| 691 |
(211, Punctuation, "}"), |
| 692 |
(212, Whitespace, "\n"), |
| 693 |
] |
| 694 |
assert_tokens_equal(self, text, expected) |
| 695 |
|
| 696 |
def test_input_value_nested_array(self): |
| 697 |
text = """\ |
| 698 |
{ |
| 699 |
node(arg: [1, [2, 3]]) |
| 700 |
} |
| 701 |
""" |
| 702 |
expected = [ |
| 703 |
(0, Punctuation, "{"), |
| 704 |
(1, Whitespace, "\n "), |
| 705 |
(4, Name, "node"), |
| 706 |
(8, Punctuation, "("), |
| 707 |
(9, Name, "arg"), |
| 708 |
(12, Punctuation, ":"), |
| 709 |
(13, Whitespace, " "), |
| 710 |
(14, Punctuation, "["), |
| 711 |
(15, Number.Integer, "1"), |
| 712 |
(16, Punctuation, ","), |
| 713 |
(17, Whitespace, " "), |
| 714 |
(18, Punctuation, "["), |
| 715 |
(19, Number.Integer, "2"), |
| 716 |
(20, Punctuation, ","), |
| 717 |
(21, Whitespace, " "), |
| 718 |
(22, Number.Integer, "3"), |
| 719 |
(23, Punctuation, "]"), |
| 720 |
(24, Punctuation, "]"), |
| 721 |
(25, Punctuation, ")"), |
| 722 |
(26, Whitespace, "\n"), |
| 723 |
(27, Punctuation, "}"), |
| 724 |
(28, Whitespace, "\n"), |
| 725 |
] |
| 726 |
assert_tokens_equal(self, text, expected) |
| 727 |
|
| 728 |
def test_input_value_object(self): |
| 729 |
text = """\ |
| 730 |
{ |
| 731 |
node(arg: {a: "a", b: "b"}) |
| 732 |
} |
| 733 |
""" |
| 734 |
expected = [ |
| 735 |
(0, Punctuation, "{"), |
| 736 |
(1, Whitespace, "\n "), |
| 737 |
(4, Name, "node"), |
| 738 |
(8, Punctuation, "("), |
| 739 |
(9, Name, "arg"), |
| 740 |
(12, Punctuation, ":"), |
| 741 |
(13, Whitespace, " "), |
| 742 |
(14, Punctuation, "{"), |
| 743 |
(15, Name, "a"), |
| 744 |
(16, Punctuation, ":"), |
| 745 |
(17, Whitespace, " "), |
| 746 |
(18, String, '"'), |
| 747 |
(19, String, "a"), |
| 748 |
(20, String, '"'), |
| 749 |
(21, Punctuation, ","), |
| 750 |
(22, Whitespace, " "), |
| 751 |
(23, Name, "b"), |
| 752 |
(24, Punctuation, ":"), |
| 753 |
(25, Whitespace, " "), |
| 754 |
(26, String, '"'), |
| 755 |
(27, String, "b"), |
| 756 |
(28, String, '"'), |
| 757 |
(29, Punctuation, "}"), |
| 758 |
(30, Punctuation, ")"), |
| 759 |
(31, Whitespace, "\n"), |
| 760 |
(32, Punctuation, "}"), |
| 761 |
(33, Whitespace, "\n"), |
| 762 |
] |
| 763 |
assert_tokens_equal(self, text, expected) |
| 764 |
|
| 765 |
def test_input_nested_value_object(self): |
| 766 |
text = """\ |
| 767 |
{ |
| 768 |
node(arg: {a: "a", b: {c: "c", d: {e: "e"}}}) |
| 769 |
} |
| 770 |
""" |
| 771 |
expected = [ |
| 772 |
(0, Punctuation, "{"), |
| 773 |
(1, Whitespace, "\n "), |
| 774 |
(4, Name, "node"), |
| 775 |
(8, Punctuation, "("), |
| 776 |
(9, Name, "arg"), |
| 777 |
(12, Punctuation, ":"), |
| 778 |
(13, Whitespace, " "), |
| 779 |
(14, Punctuation, "{"), |
| 780 |
(15, Name, "a"), |
| 781 |
(16, Punctuation, ":"), |
| 782 |
(17, Whitespace, " "), |
| 783 |
(18, String, '"'), |
| 784 |
(19, String, "a"), |
| 785 |
(20, String, '"'), |
| 786 |
(21, Punctuation, ","), |
| 787 |
(22, Whitespace, " "), |
| 788 |
(23, Name, "b"), |
| 789 |
(24, Punctuation, ":"), |
| 790 |
(25, Whitespace, " "), |
| 791 |
(26, Punctuation, "{"), |
| 792 |
(27, Name, "c"), |
| 793 |
(28, Punctuation, ":"), |
| 794 |
(29, Whitespace, " "), |
| 795 |
(30, String, '"'), |
| 796 |
(31, String, "c"), |
| 797 |
(32, String, '"'), |
| 798 |
(33, Punctuation, ","), |
| 799 |
(34, Whitespace, " "), |
| 800 |
(35, Name, "d"), |
| 801 |
(36, Punctuation, ":"), |
| 802 |
(37, Whitespace, " "), |
| 803 |
(38, Punctuation, "{"), |
| 804 |
(39, Name, "e"), |
| 805 |
(40, Punctuation, ":"), |
| 806 |
(41, Whitespace, " "), |
| 807 |
(42, String, '"'), |
| 808 |
(43, String, "e"), |
| 809 |
(44, String, '"'), |
| 810 |
(45, Punctuation, "}"), |
| 811 |
(46, Punctuation, "}"), |
| 812 |
(47, Punctuation, "}"), |
| 813 |
(48, Punctuation, ")"), |
| 814 |
(49, Whitespace, "\n"), |
| 815 |
(50, Punctuation, "}"), |
| 816 |
(51, Whitespace, "\n"), |
| 817 |
] |
| 818 |
assert_tokens_equal(self, text, expected) |
| 819 |
|
| 820 |
def test_input_value_mixed(self): |
| 821 |
text = """\ |
| 822 |
{ |
| 823 |
node1(arg: {a: [1, 2]}) |
| 824 |
node2(arg: [1, {a: "a"}]) |
| 825 |
} |
| 826 |
""" |
| 827 |
expected = [ |
| 828 |
(0, Punctuation, "{"), |
| 829 |
(1, Whitespace, "\n "), |
| 830 |
(4, Name, "node1"), |
| 831 |
(9, Punctuation, "("), |
| 832 |
(10, Name, "arg"), |
| 833 |
(13, Punctuation, ":"), |
| 834 |
(14, Whitespace, " "), |
| 835 |
(15, Punctuation, "{"), |
| 836 |
(16, Name, "a"), |
| 837 |
(17, Punctuation, ":"), |
| 838 |
(18, Whitespace, " "), |
| 839 |
(19, Punctuation, "["), |
| 840 |
(20, Number.Integer, "1"), |
| 841 |
(21, Punctuation, ","), |
| 842 |
(22, Whitespace, " "), |
| 843 |
(23, Number.Integer, "2"), |
| 844 |
(24, Punctuation, "]"), |
| 845 |
(25, Punctuation, "}"), |
| 846 |
(26, Punctuation, ")"), |
| 847 |
(27, Whitespace, "\n "), |
| 848 |
(30, Name, "node2"), |
| 849 |
(35, Punctuation, "("), |
| 850 |
(36, Name, "arg"), |
| 851 |
(39, Punctuation, ":"), |
| 852 |
(40, Whitespace, " "), |
| 853 |
(41, Punctuation, "["), |
| 854 |
(42, Number.Integer, "1"), |
| 855 |
(43, Punctuation, ","), |
| 856 |
(44, Whitespace, " "), |
| 857 |
(45, Punctuation, "{"), |
| 858 |
(46, Name, "a"), |
| 859 |
(47, Punctuation, ":"), |
| 860 |
(48, Whitespace, " "), |
| 861 |
(49, String, '"'), |
| 862 |
(50, String, "a"), |
| 863 |
(51, String, '"'), |
| 864 |
(52, Punctuation, "}"), |
| 865 |
(53, Punctuation, "]"), |
| 866 |
(54, Punctuation, ")"), |
| 867 |
(55, Whitespace, "\n"), |
| 868 |
(56, Punctuation, "}"), |
| 869 |
(57, Whitespace, "\n"), |
| 870 |
] |
| 871 |
assert_tokens_equal(self, text, expected) |