Skip to content

Commit

Permalink
chore: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsilll committed Nov 3, 2024
1 parent b1ed4bd commit f25bcd9
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 174 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
.cache/
.vscode/
__pycache__/
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-unused-variable")

include_directories(include ${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

add_subdirectory(src)
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "src/*.cpp")

add_executable(compiler ${SOURCES})

llvm_map_components_to_libnames(llvm_libs core support)

target_link_libraries(compiler ${llvm_libs})
File renamed without changes.
File renamed without changes.
164 changes: 0 additions & 164 deletions samples/test.py

This file was deleted.

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.
7 changes: 0 additions & 7 deletions src/CMakeLists.txt

This file was deleted.

5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void reportErrors(

} // namespace

int main(int argc, char **argv) {
int main(int argc, char **argv) try {
llvm::cl::ParseCommandLineOptions(argc, argv, "Lang Compiler\n", nullptr,
nullptr, true);

Expand Down Expand Up @@ -244,4 +244,7 @@ int main(int argc, char **argv) {
}

return EXIT_SUCCESS;
} catch (const std::exception &e) {
llvm::errs() << "Error: " << e.what() << '\n';
return EXIT_FAILURE;
}
176 changes: 176 additions & 0 deletions tests/main.py
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__])

0 comments on commit f25bcd9

Please sign in to comment.