mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 21:34:32 +08:00
Use _assert_nodes_are_equal in the fuzzer
This commit is contained in:
@@ -215,7 +215,7 @@ class PythonGrammar(Grammar):
|
|||||||
)
|
)
|
||||||
self.version_info = version_info
|
self.version_info = version_info
|
||||||
|
|
||||||
def _tokenize_lines(self, lines, start_pos):
|
def _tokenize_lines(self, lines, start_pos=(1, 0)):
|
||||||
return tokenize_lines(lines, self.version_info, start_pos=start_pos)
|
return tokenize_lines(lines, self.version_info, start_pos=start_pos)
|
||||||
|
|
||||||
def _tokenize(self, code):
|
def _tokenize(self, code):
|
||||||
|
|||||||
@@ -78,6 +78,26 @@ def _assert_valid_graph(node):
|
|||||||
_assert_valid_graph(child)
|
_assert_valid_graph(child)
|
||||||
|
|
||||||
|
|
||||||
|
def _assert_nodes_are_equal(node1, node2):
|
||||||
|
try:
|
||||||
|
children1 = node1.children
|
||||||
|
except AttributeError:
|
||||||
|
assert not hasattr(node2, 'children'), (node1, node2)
|
||||||
|
assert node1.value == node2.value
|
||||||
|
assert node1.type == node2.type
|
||||||
|
assert node1.prefix == node2.prefix
|
||||||
|
assert node1.start_pos == node2.start_pos
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
children2 = node2.children
|
||||||
|
except AttributeError:
|
||||||
|
assert False, (node1, node2)
|
||||||
|
assert len(children1) == len(children2)
|
||||||
|
for n1, n2 in zip(children1, children2):
|
||||||
|
_assert_nodes_are_equal(n1, n2)
|
||||||
|
|
||||||
|
|
||||||
def _get_debug_error_message(module, old_lines, new_lines):
|
def _get_debug_error_message(module, old_lines, new_lines):
|
||||||
current_lines = split_lines(module.get_code(), keepends=True)
|
current_lines = split_lines(module.get_code(), keepends=True)
|
||||||
current_diff = difflib.unified_diff(new_lines, current_lines)
|
current_diff = difflib.unified_diff(new_lines, current_lines)
|
||||||
@@ -249,8 +269,14 @@ class DiffParser(object):
|
|||||||
# If there is reasonable suspicion that the diff parser is not
|
# If there is reasonable suspicion that the diff parser is not
|
||||||
# behaving well, this should be enabled.
|
# behaving well, this should be enabled.
|
||||||
try:
|
try:
|
||||||
assert self._module.get_code() == ''.join(new_lines)
|
code = ''.join(new_lines)
|
||||||
|
assert self._module.get_code() == code
|
||||||
_assert_valid_graph(self._module)
|
_assert_valid_graph(self._module)
|
||||||
|
without_diff_parser_module = Parser(
|
||||||
|
self._pgen_grammar,
|
||||||
|
error_recovery=True
|
||||||
|
).parse(self._tokenizer(new_lines))
|
||||||
|
_assert_nodes_are_equal(self._module, without_diff_parser_module)
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
print(_get_debug_error_message(self._module, old_lines, new_lines))
|
print(_get_debug_error_message(self._module, old_lines, new_lines))
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import pytest
|
|||||||
from parso.utils import split_lines
|
from parso.utils import split_lines
|
||||||
from parso import cache
|
from parso import cache
|
||||||
from parso import load_grammar
|
from parso import load_grammar
|
||||||
from parso.python.diff import DiffParser, _assert_valid_graph
|
from parso.python.diff import DiffParser, _assert_valid_graph, _assert_nodes_are_equal
|
||||||
from parso import parse
|
from parso import parse
|
||||||
|
|
||||||
ANY = object()
|
ANY = object()
|
||||||
@@ -39,26 +39,6 @@ def _check_error_leaves_nodes(node):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _assert_nodes_are_equal(node1, node2):
|
|
||||||
try:
|
|
||||||
children1 = node1.children
|
|
||||||
except AttributeError:
|
|
||||||
assert not hasattr(node2, 'children'), (node1, node2)
|
|
||||||
assert node1.value == node2.value
|
|
||||||
assert node1.type == node2.type
|
|
||||||
assert node1.prefix == node2.prefix
|
|
||||||
assert node1.start_pos == node2.start_pos
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
children2 = node2.children
|
|
||||||
except AttributeError:
|
|
||||||
assert False, (node1, node2)
|
|
||||||
assert len(children1) == len(children2)
|
|
||||||
for n1, n2 in zip(children1, children2):
|
|
||||||
_assert_nodes_are_equal(n1, n2)
|
|
||||||
|
|
||||||
|
|
||||||
class Differ(object):
|
class Differ(object):
|
||||||
grammar = load_grammar()
|
grammar = load_grammar()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user