-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
189 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build/ | ||
.cache/ | ||
.vscode/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import json | ||
from subprocess import CompletedProcess | ||
|
||
|
||
def compile_program(file: str, opts: str = "") -> CompletedProcess[str]: | ||
import subprocess | ||
|
||
""" | ||
Compile the given program through the compiler executable and return the output. | ||
Args: | ||
program (str): The program to compile. | ||
Returns: | ||
Tuple[str, str]: A tuple containing the stdout and stderr of the compiler. | ||
""" | ||
# Open process with pipes for stdout and stderr | ||
return subprocess.run( | ||
["./build/compiler", file, opts], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
text=True, | ||
) | ||
|
||
|
||
def test_valid_01() -> None: | ||
res = compile_program("valid/01.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_02() -> None: | ||
res = compile_program("valid/02.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_03() -> None: | ||
res = compile_program("valid/03.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_04() -> None: | ||
res = compile_program("valid/04.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_05() -> None: | ||
res = compile_program("valid/05.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_06() -> None: | ||
res = compile_program("valid/06.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_07() -> None: | ||
res = compile_program("valid/07.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_08() -> None: | ||
res = compile_program("valid/08.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_09() -> None: | ||
res = compile_program("valid/09.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
|
||
def test_valid_10() -> None: | ||
res = compile_program("valid/10.lang") | ||
|
||
assert res.returncode == 0 | ||
assert not res.stderr | ||
|
||
def test_error_01() -> None: | ||
res = compile_program("samples/error/01.lang", "--error-format=json") | ||
|
||
assert res.returncode != 0 | ||
assert res.stderr | ||
|
||
try: | ||
error = json.loads(res.stderr) | ||
assert len(error) == 1 | ||
assert error[0]["id"] == "lex-invalid-char" | ||
assert error[0]["loc"] == "samples/error/01.lang:1:1" | ||
except json.JSONDecodeError as e: | ||
print(e) | ||
assert False | ||
|
||
def test_error_02() -> None: | ||
res = compile_program("samples/error/02.lang", "--error-format=json") | ||
|
||
assert res.returncode != 0 | ||
assert res.stderr | ||
|
||
try: | ||
error = json.loads(res.stderr) | ||
assert len(error) == 1 | ||
assert error[0]["id"] == "parse-unexpected-eof" | ||
assert error[0]["loc"] == "samples/error/02.lang:1:1" | ||
except json.JSONDecodeError as e: | ||
print(e) | ||
assert False | ||
|
||
def test_error_03() -> None: | ||
res = compile_program("samples/error/03.lang", "--error-format=json") | ||
|
||
assert res.returncode != 0 | ||
assert res.stderr | ||
|
||
print(res.stderr) | ||
try: | ||
error = json.loads(res.stderr) | ||
assert len(error) == 1 | ||
assert error[0]["id"] == "parse-unexpected-token" | ||
assert error[0]["loc"] == "samples/error/03.lang:2:9" | ||
except json.JSONDecodeError as e: | ||
print(e) | ||
assert False | ||
|
||
def test_error_04() -> None: | ||
res = compile_program("samples/error/04.lang", "--error-format=json") | ||
|
||
assert res.returncode != 0 | ||
assert res.stderr | ||
|
||
print(res.stderr) | ||
try: | ||
error = json.loads(res.stderr) | ||
assert len(error) == 1 | ||
assert error[0]["id"] == "cfa-early-return-stmt" | ||
assert error[0]["loc"] == "samples/error/04.lang:2:5" | ||
except json.JSONDecodeError as e: | ||
print(e) | ||
assert False | ||
|
||
def test_error_05() -> None: | ||
res = compile_program("samples/error/05.lang", "--error-format=json") | ||
|
||
assert res.returncode != 0 | ||
assert res.stderr | ||
|
||
print(res.stderr) | ||
try: | ||
error = json.loads(res.stderr) | ||
assert len(error) == 1 | ||
assert error[0]["id"] == "cfa-invalid-break-stmt" | ||
assert error[0]["loc"] == "samples/error/05.lang:2:5" | ||
except json.JSONDecodeError as e: | ||
print(e) | ||
assert False | ||
|
||
if __name__ == "__main__": | ||
import pytest | ||
|
||
pytest.main([__file__]) |