Skip to content

Commit c2566da

Browse files
committed
test_visitor: cleanup test for nodes with unknown kinds
Replicates graphql/graphql-js@264f758
1 parent 5f3f3be commit c2566da

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

tests/language/test_visitor.py

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,75 @@ def leave_selection_set(*args):
539539
["leave", "selection_set", None],
540540
]
541541

542+
def visit_nodes_with_unknown_kinds_but_does_not_traverse_deeper():
543+
custom_ast = parse("{ a }")
544+
545+
class CustomFieldNode(SelectionNode):
546+
__slots__ = "name", "selection_set"
547+
548+
name: NameNode
549+
selection_set: Optional[SelectionSetNode]
550+
551+
custom_selection_set = cast(FieldNode, custom_ast.definitions[0]).selection_set
552+
assert custom_selection_set is not None
553+
custom_selection_set.selections = custom_selection_set.selections + [
554+
CustomFieldNode(
555+
name=NameNode(value="NameNodeToBeSkipped"),
556+
selection_set=SelectionSetNode(
557+
selections=CustomFieldNode(
558+
name=NameNode(value="NameNodeToBeSkipped")
559+
)
560+
),
561+
)
562+
]
563+
564+
visited = []
565+
566+
class TestVisitor(Visitor):
567+
@staticmethod
568+
def enter(node, *_args):
569+
visited.append(["enter", node.kind, get_value(node)])
570+
571+
@staticmethod
572+
def leave(node, *_args):
573+
visited.append(["leave", node.kind, get_value(node)])
574+
575+
visit(custom_ast, TestVisitor())
576+
assert visited == [
577+
["enter", "document", None],
578+
["enter", "operation_definition", None],
579+
["enter", "selection_set", None],
580+
["enter", "field", None],
581+
["enter", "name", "a"],
582+
["leave", "name", "a"],
583+
["leave", "field", None],
584+
["enter", "custom_field", None],
585+
["leave", "custom_field", None],
586+
["leave", "selection_set", None],
587+
["leave", "operation_definition", None],
588+
["leave", "document", None],
589+
]
590+
591+
def cannot_define_visitor_with_unknown_ast_nodes():
592+
with raises(TypeError) as exc_info:
593+
594+
class VisitorWithNonExistingNode(Visitor):
595+
def enter_field(self, *_args):
596+
pass
597+
598+
def leave_garfield(self, *_args):
599+
pass
600+
601+
assert str(exc_info.value) == "Invalid AST node kind: garfield."
602+
603+
with raises(TypeError) as exc_info:
604+
605+
class VisitorWithUnspecificNode(Visitor):
606+
def enter_type_system_extension(self, *_args):
607+
pass
608+
609+
assert str(exc_info.value) == "Invalid AST node kind: type_system_extension."
610+
542611
def legacy_visits_variables_defined_in_fragments():
543612
ast = parse(
544613
"fragment a($v: Boolean = false) on t { f }",
@@ -970,75 +1039,6 @@ def leave(*args):
9701039
]
9711040

9721041

973-
def describe_support_for_custom_ast_nodes():
974-
custom_ast = parse("{ a }")
975-
976-
class CustomFieldNode(SelectionNode):
977-
__slots__ = "name", "selection_set"
978-
979-
name: NameNode
980-
selection_set: Optional[SelectionSetNode]
981-
982-
custom_selection_set = cast(FieldNode, custom_ast.definitions[0]).selection_set
983-
assert custom_selection_set is not None
984-
custom_selection_set.selections = custom_selection_set.selections + [
985-
CustomFieldNode(
986-
name=NameNode(value="b"),
987-
selection_set=SelectionSetNode(
988-
selections=CustomFieldNode(name=NameNode(value="c"))
989-
),
990-
)
991-
]
992-
993-
def does_not_traverse_unknown_node_kinds():
994-
visited = []
995-
996-
class TestVisitor(Visitor):
997-
@staticmethod
998-
def enter(node, *_args):
999-
visited.append(["enter", node.kind, get_value(node)])
1000-
1001-
@staticmethod
1002-
def leave(node, *_args):
1003-
visited.append(["leave", node.kind, get_value(node)])
1004-
1005-
visit(custom_ast, TestVisitor())
1006-
assert visited == [
1007-
["enter", "document", None],
1008-
["enter", "operation_definition", None],
1009-
["enter", "selection_set", None],
1010-
["enter", "field", None],
1011-
["enter", "name", "a"],
1012-
["leave", "name", "a"],
1013-
["leave", "field", None],
1014-
["enter", "custom_field", None],
1015-
["leave", "custom_field", None],
1016-
["leave", "selection_set", None],
1017-
["leave", "operation_definition", None],
1018-
["leave", "document", None],
1019-
]
1020-
1021-
def cannot_define_visitor_with_unknown_ast_nodes():
1022-
with raises(TypeError) as exc_info:
1023-
1024-
class VisitorWithNonExistingNode(Visitor):
1025-
def enter_field(self, *_args):
1026-
pass
1027-
1028-
def leave_garfield(self, *_args):
1029-
pass
1030-
1031-
assert str(exc_info.value) == "Invalid AST node kind: garfield."
1032-
1033-
with raises(TypeError) as exc_info:
1034-
1035-
class VisitorWithUnspecificNode(Visitor):
1036-
def enter_type_system_extension(self, *_args):
1037-
pass
1038-
1039-
assert str(exc_info.value) == "Invalid AST node kind: type_system_extension."
1040-
1041-
10421042
def describe_visit_in_parallel():
10431043
@mark.parametrize("skip_action", (SKIP, False), ids=("SKIP", "False"))
10441044
def allows_skipping_a_sub_tree(skip_action):

0 commit comments

Comments
 (0)