Skip to content

Commit 7d053d0

Browse files
committed
Add initial setup for unit tests
1 parent be2a469 commit 7d053d0

9 files changed

Lines changed: 117 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# CMakeLists.txt
22
cmake_minimum_required(VERSION 3.22)
33

4+
enable_testing()
5+
46
project(clox LANGUAGES C)
57

68
# -----------------------------------------------------------------
@@ -37,6 +39,13 @@ set_source_files_properties(${VERSION_C} PROPERTIES GENERATED TRUE)
3739
file(GLOB SRC_FILES CONFIGURE_DEPENDS src/*.c)
3840
list(FILTER SRC_FILES EXCLUDE REGEX ".*/version\\.c$")
3941

42+
# ---
43+
# Test Source Files
44+
# ---
45+
file(GLOB UNIT_TEST_SRCS unit/*.c)
46+
47+
option(BUILD_TESTS "Build unit‑tests for project" OFF)
48+
4049
# -----------------------------------------------------------------
4150
# Executables
4251
# -----------------------------------------------------------------
@@ -56,6 +65,36 @@ target_link_libraries(clox-test PRIVATE readline m)
5665
add_dependencies(clox generate_version_c)
5766
add_dependencies(clox-test generate_version_c)
5867

68+
if (BUILD_TESTS)
69+
70+
# -----------------------------------------------------------------
71+
# Tests
72+
# -----------------------------------------------------------------
73+
74+
add_executable(clox-version-unit-test unit/version.c ${VERSION_C})
75+
add_dependencies(clox-version-unit-test generate_version_c)
76+
add_test(
77+
NAME clox-version-unit-test
78+
COMMAND clox-version-unit-test
79+
)
80+
81+
add_executable(clox-token-token-test unit/token.c src/token.c ${VERSION_C})
82+
add_dependencies(clox-token-token-test generate_version_c)
83+
add_test(
84+
NAME clox-token-unit-test
85+
COMMAND clox-token-unit-test
86+
)
87+
88+
add_executable(clox-value-unit-test unit/value.c src/value.c src/object.c src/hashtable.c src/chunk.c src/memory.c src/vm.c src/assert.c src/scanner.c src/compiler.c src/token.c src/native.c ${VERSION_C})
89+
add_dependencies(clox-value-unit-test generate_version_c)
90+
target_link_libraries(clox-value-unit-test PRIVATE readline m)
91+
add_test(
92+
NAME clox-value-unit-test
93+
COMMAND clox-value-unit-test
94+
)
95+
96+
endif()
97+
5998
# -----------------------------------------------------------------
6099
# Coverage option
61100
# -----------------------------------------------------------------
@@ -70,4 +109,7 @@ if(ENABLE_COVERAGE)
70109

71110
target_compile_options(clox-test PRIVATE ${COVERAGE_COMPILE_FLAGS})
72111
target_link_options(clox-test PRIVATE ${COVERAGE_LINK_FLAGS})
112+
113+
target_compile_options(clox-unit-test PRIVATE ${COVERAGE_COMPILE_FLAGS})
114+
target_link_options(clox-unit-test PRIVATE ${COVERAGE_LINK_FLAGS})
73115
endif()

justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ cmake:
2424
build: generate_version_c
2525
./scripts/build.sh
2626

27+
build-tests: generate_version_c
28+
./scripts/build-tests.sh
29+
2730
# Clean the build artifacts
2831
clean:
2932
./scripts/clean.sh
@@ -44,6 +47,9 @@ install:
4447
test *args: build
4548
./scripts/tests.sh {{ args }}
4649

50+
unit: build-tests
51+
./scripts/unit.sh
52+
4753
# Run the tests with coverage enabled
4854
coverage *args:
4955
./scripts/coverage.sh {{ args }}

scripts/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ Run `./scripts/coverage.sh` or `just coverage` to generate code coverage data.
4444

4545
Outputs `lcov.info` at the root of the project.
4646

47+
## Unit Tests
48+
49+
Run `./sciprts/build-tests.sh && ./scripts/unit.sh` or `just unit` to run the unit tests.
50+
51+
## Build Unit Tests
52+
53+
Run `./scripts/build-tests.sh` or `just build-tests` to build the [unit tests](#unit-tests).
54+
4755
## Run Examples
4856

4957
```sh

scripts/build-tests.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
BUILD_DIR=build
5+
ENABLE_COVERAGE=${ENABLE_COVERAGE:-OFF}
6+
7+
cmake -S . -B "$BUILD_DIR" -DENABLE_COVERAGE=$ENABLE_COVERAGE -DBUILD_TESTS=on
8+
cmake --build "$BUILD_DIR"

scripts/unit.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
cd build
5+
ctest --verbose

src/token.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ typedef enum {
5050
TOKEN_WHILE,
5151

5252
TOKEN_ERROR,
53-
TOKEN_EOF
53+
TOKEN_EOF,
54+
NumberOfDefinedTokens
5455
} TokenType;
5556

5657
typedef struct {

unit/token.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include "../src/token.h"
6+
7+
int main(void) {
8+
assert(NumberOfDefinedTokens == 44);
9+
assert(strcmp(tokenTypeToString(TOKEN_AND), "TOKEN_AND") == 0);
10+
return 0;
11+
}

unit/value.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include "../src/value.h"
6+
7+
#define TEST(name, value, expected) \
8+
static void name() { \
9+
char buffer[sizeof(expected)]; \
10+
valueToString(value, buffer, sizeof(buffer)); \
11+
assert(strcmp(expected, buffer) == 0); \
12+
}
13+
14+
TEST(test_nil, NIL_VAL, "nil");
15+
TEST(test_number, NUMBER_VAL(123), "123.000000");
16+
TEST(test_bool_true, BOOL_VAL(true), "true");
17+
TEST(test_bool_false, BOOL_VAL(false), "false");
18+
19+
int main(void) {
20+
test_nil();
21+
test_number();
22+
test_bool_true();
23+
test_bool_false();
24+
25+
return 0;
26+
}

unit/version.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
4+
#include "../src/version.h"
5+
6+
int main(void) {
7+
assert(strcmp(CLOX_VERSION, "0.1.0") == 0);
8+
return 0;
9+
}

0 commit comments

Comments
 (0)