Skip to content

Commit

Permalink
Fix (a and b) and c case. Keep unparsed output consist on AST with or…
Browse files Browse the repository at this point in the history
…iginal input.

Fix the bug that failed tests return a successful return code
  • Loading branch information
yunline committed Dec 13, 2024
1 parent 0ed3575 commit e29f4f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 2 additions & 2 deletions oneliner/expr_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
PREC_COMPARE = next(enum)
PREC_NOT = next(enum)

PREC_AND = next(enum)
PREC_AND_SLOT = next(enum)
PREC_AND = next(enum)

PREC_OR = next(enum)
PREC_OR_SLOT = next(enum)
PREC_OR = next(enum)

PREC_COMPREHENSION_SLOT_ITER = next(enum)

Expand Down
11 changes: 9 additions & 2 deletions oneliner_tests/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import sys
import unittest

from . import oneliner_test, unparser_test

unittest.main(unparser_test.__name__, exit=False)
unittest.main(oneliner_test.__name__)
return_code = 0

result = unittest.main(unparser_test.__name__, exit=False).result
return_code |= not result.wasSuccessful()
result = unittest.main(oneliner_test.__name__, exit=False).result
return_code |= not result.wasSuccessful()

sys.exit(return_code)
45 changes: 26 additions & 19 deletions oneliner_tests/unparser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,35 @@ def ast_equivalent(a: ast.AST, b: ast.AST):

class _TestExprUnparse(unittest.TestCase):
def assertUnparseConsist(self, code, msg=None):
module = ast.parse(code, filename="<original>")
assert isinstance(module.body[0], ast.Expr)
original_tree = module.body[0].value
testing_result = expr_unparse(original_tree)
standard_result = ast.unparse(original_tree)
original_tree = ast.parse(code, filename="<original>")
assert isinstance(original_tree.body[0], ast.Expr)
original_expr = original_tree.body[0].value
testing_result = expr_unparse(original_expr)
standard_result = ast.unparse(original_expr)
testing_tree = ast.parse(testing_result, filename="<expr_unparse>")
standard_tree = ast.parse(standard_result, filename="<ast.unparse>")

if not ast_equivalent(testing_tree, standard_tree):
print(f"\nCase {repr(code)} not equivalent in AST, checking byte code")
# if not equivalent in ast, fallback to check if equivalent in byte code
testing_code_obj = compile(testing_result, "<expr_unparse>", "exec")
standard_code_obj = compile(standard_result, "<ast.unparse>", "exec")
if testing_code_obj.co_code != standard_code_obj.co_code:
testing_dump = ast.dump(testing_tree, indent=4)
standard_dump = ast.dump(standard_tree, indent=4)
msg = self._formatMessage(
msg,
f"\nast.unparse:\n{standard_result}\n{standard_dump}"
f"\nexpr_unparse:\n{testing_result}\n{testing_dump}",
)
raise self.failureException(msg)
if not ast_equivalent(testing_tree, original_tree):
original_dump = ast.dump(original_tree, indent=4)
testing_dump = ast.dump(testing_tree, indent=4)
standard_dump = ast.dump(standard_tree, indent=4)
msg = self._formatMessage(
msg,
f"\nexpr_unparse result is different from original\n"
f"\noriginal:\n{code}\n{original_dump}"
f"\nast.unparse:\n{standard_result}\n{standard_dump}"
f"\nexpr_unparse:\n{testing_result}\n{testing_dump}",
)
raise self.failureException(msg)
if not ast_equivalent(standard_tree, original_tree):
# test the standard lib
# hopefully we will find cpython bug one day xD
print(
f"\nast.unparse result is different from original"
f"\noriginal: {code}"
f"\nast.unparse: {standard_result}\n"
f"\nexpr_unparse: {testing_result}\n"
)


class TestSingleExprUnparse(_TestExprUnparse):
Expand Down

0 comments on commit e29f4f3

Please sign in to comment.