Commit 2abf5fbb authored by Roman Alifanov's avatar Roman Alifanov

Add tests for compile-time method validation

parent ec4a018f
......@@ -47,6 +47,28 @@ def compile_ct(source: str) -> tuple[int, str, str]:
os.unlink(ct_file)
def compile_ct_check(source: str) -> tuple[int, str, str]:
with tempfile.NamedTemporaryFile(mode='w', suffix='.ct', delete=False) as f:
f.write(source)
f.flush()
ct_file = f.name
sh_file = ct_file.replace('.ct', '.sh')
try:
result = subprocess.run(
['python3', 'content', 'build', ct_file, '-o', sh_file],
capture_output=True,
text=True,
timeout=10
)
if os.path.exists(sh_file):
os.unlink(sh_file)
return result.returncode, result.stdout, result.stderr
finally:
os.unlink(ct_file)
class TestPrint:
def test_print_string(self):
code, stdout, _ = run_ct('print ("hello")')
......@@ -806,3 +828,78 @@ func double(x) {
print(double(-1))
''')
assert code != 0 or "must be" in stderr
class TestMethodValidation:
def test_unknown_array_method(self):
code, stdout, stderr = compile_ct_check('''
arr = [1, 2, 3]
x = arr.nonexistent()
''')
assert code != 0
assert "Unknown method 'nonexistent'" in stdout
assert "array" in stdout
def test_unknown_dict_method(self):
code, stdout, stderr = compile_ct_check('''
d = {"key": "value"}
x = d.nonexistent()
''')
assert code != 0
assert "Unknown method 'nonexistent'" in stdout
assert "dict" in stdout
def test_unknown_namespace_method(self):
code, stdout, stderr = compile_ct_check('''
x = fs.nonexistent()
''')
assert code != 0
assert "Unknown method 'nonexistent'" in stdout
assert "fs" in stdout
def test_valid_string_methods(self):
code, stdout, stderr = compile_ct_check('''
text = "hello"
a = text.upper()
b = text.lower()
c = text.len()
d = text.trim()
''')
assert code == 0
def test_valid_array_methods(self):
code, stdout, stderr = compile_ct_check('''
arr = [1, 2, 3]
arr.push(4)
x = arr.pop()
y = arr.len()
z = arr.join(" ")
''')
assert code == 0
def test_valid_dict_methods(self):
code, stdout, stderr = compile_ct_check('''
d = {"key": "value"}
a = d.get("key")
d.set("key2", "val2")
b = d.has("key")
c = d.keys()
''')
assert code == 0
def test_valid_namespace_methods(self):
code, stdout, stderr = compile_ct_check('''
x = math.abs(-5)
y = math.max(1, 2)
''')
assert code == 0
def test_error_shows_available_methods(self):
code, stdout, stderr = compile_ct_check('''
arr = [1, 2, 3]
x = arr.foo()
''')
assert code != 0
assert "Available:" in stdout
assert "push" in stdout
assert "pop" in stdout
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment