automatic regressive testing

This commit is contained in:
Yuyao Huang 2024-04-19 14:54:14 +08:00
parent 3879263121
commit 5017ce2736
8 changed files with 57 additions and 12 deletions

View File

@ -1,4 +1,4 @@
from trace_commentor import Commentor
from test_utils import *
def test_function_def():
@ -7,4 +7,7 @@ def test_function_def():
def target():
pass
print(target())
asserteq_or_print(target(), '''
def target():
pass
''')

View File

@ -1,4 +1,5 @@
from trace_commentor import Commentor
from test_utils import asserteq_or_print
def test_binop():
@ -7,4 +8,28 @@ def test_binop():
def target():
1 + 1
print(target())
asserteq_or_print(
target(), '''
def target():
1 + 1
"""
2 : 1 + 1
"""
''')
def test_binop_cascade():
@Commentor()
def target():
1 + 1 + 1
asserteq_or_print(
target(), '''
def target():
1 + 1 + 1
"""
2 : 1 + 1
3 : 1 + 1 + 1
"""
''')

View File

@ -1,4 +1,4 @@
from trace_commentor import Commentor
from test_utils import *
def test_constant():
@ -7,4 +7,8 @@ def test_constant():
def target():
1
print(target())
asserteq_or_print(target(),
'''
def target():
1
''')

7
tests/test_utils.py Normal file
View File

@ -0,0 +1,7 @@
from trace_commentor import flags, Commentor
def asserteq_or_print(value, ground_truth):
if flags.DEBUG or flags.PRINT:
print(value)
else:
assert value == ground_truth.strip("\n"), "\n".join(["\n\n<<<<<<<< VALUE", value, "========================", ground_truth.strip("\n"), ">>>>>>>> GROUND\n"])

View File

@ -1,4 +1,9 @@
DEBUG = False
import os
bool_env = lambda name: os.environ.get(name, "false").lower() in ('true', '1', 'yes')
DEBUG = bool_env("DEBUG")
PRINT = bool_env("PRINT")
INDENT = 4
SOURCE = 1
COMMENT = 2

View File

@ -2,7 +2,7 @@ from .. import flags
from ..utils import to_source
def FunctionDef(self, cmtor):
cmtor.append_source("def function():")
cmtor.append_source(f"def {self.name}():")
cmtor.indent += flags.INDENT
for stmt in self.body:

View File

@ -1,2 +1,2 @@
def Pass(self, cmtor):
cmtor.append("pass")
pass

View File

@ -1,3 +1,4 @@
import os
import astor
import inspect
from . import flags
@ -5,8 +6,8 @@ from . import flags
def sign(line: str, depth=1):
if flags.DEBUG:
currentframe = inspect.currentframe()
outerframe = inspect.getouterframes(currentframe, depth)
debug_msg = outerframe[1][3]
outerframe = inspect.getouterframes(currentframe, 1)[depth]
debug_msg = f"{outerframe.function} @ {os.path.relpath(outerframe.filename)}:{outerframe.lineno}"
return f"{line} --- {debug_msg}"
else:
return line