Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复当assert_函数带有参数指定时rewrite报错问题 #159

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions testbase/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,15 @@ def rewrite_assert_(self, elem):

# Rewrite assert into a bunch of statements.
if isinstance(elem, ast.Expr):
top_condition, explanation = self.visit(elem.value.args[1])
msg_arg = elem.value.args[0]
if len(elem.value.args) < 2 and getattr(elem.value, "keywords", None):
for keyword in elem.value.keywords:
if keyword.arg == "message":
msg_arg = keyword.value
elif keyword.arg == "value":
top_condition, explanation = self.visit(keyword.value)
else:
top_condition, explanation = self.visit(elem.value.args[1])
msg_arg = elem.value.args[0]
caller_id = "self"
elif isinstance(elem, ast.Call):
top_condition, explanation = self.visit(elem.args[1])
Expand Down
10 changes: 10 additions & 0 deletions tests/test_testbase/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ def assert_foo_bar_with_for(self):
for _ in range(2):
self.assert_("assert", self.foo(self.bar(1)) in [2, 4])

def assert_foo_bar_with_while(self):
i = 1
while i < 3:
self.assert_("assert", self.foo(self.bar(1)) in [2, 4])

def assert_foo_bar_with_dict(self):
i = 1
if i == 1:
self.assert_(message="assert", value=self.foo(self.bar(1)) in [2, 4])


class AssertionInnerInvokeForTest(AssertionFailureTest):
"""xxxx"""
Expand Down
Loading