From 8fc3e82173615d318f1a4de25b556fd06ed54917 Mon Sep 17 00:00:00 2001 From: haxala1r Date: Tue, 30 Sep 2025 21:46:26 +0300 Subject: [PATCH] Added testing with Catch2 --- .woodpecker/workflow.yaml | 4 +++- CMakeLists.txt | 26 +++++++++++++++++++++++--- src/tests/test.cpp | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/tests/test.cpp diff --git a/.woodpecker/workflow.yaml b/.woodpecker/workflow.yaml index af28bd9..c9139ca 100644 --- a/.woodpecker/workflow.yaml +++ b/.woodpecker/workflow.yaml @@ -13,6 +13,8 @@ steps: - name: test image: ubuntu commands: - # TODO: Probably make actual tests at some point + # Automated tests, this should not fail + - ./test + # Manual test, you can see the output of this in woodpecker - echo "(print 42)" | ./build/main # TODO: add publish step, when we're at a working state. \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 58d4591..d31d49e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,30 @@ cmake_minimum_required(VERSION 3.16) project(lispy_stuff) +# we'll use catch2 as testing library. +include(FetchContent) +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.8.1 +) +FetchContent_MakeAvailable(Catch2) +list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) +include(Catch) + set(HEADER_FILES src/include/lex.hpp) -set(SOURCE_FILES src/main.cpp src/lex.cpp) +set(SOURCE_FILES src/lex.cpp) +# we're not actually shipping a library yet, +# this is so we don't have to compile twice for main and tests. +add_library(libmash STATIC ${SOURCE_FILES} ${HEADER_FILES}) +target_include_directories(libmash PUBLIC src/include/) -add_executable(main ${SOURCE_FILES} ${HEADER_FILES}) +# Main target +add_executable(main src/main.cpp) +target_link_libraries(main libmash) + +# tests +add_executable(test src/tests/test.cpp) +target_link_libraries(test PRIVATE libmash Catch2::Catch2WithMain) -target_include_directories(main PRIVATE src/include/) \ No newline at end of file diff --git a/src/tests/test.cpp b/src/tests/test.cpp new file mode 100644 index 0000000..6db6ca4 --- /dev/null +++ b/src/tests/test.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; + +TEST_CASE("Lexer lexes doubles correctly", "[Lexer]") { + + SECTION("double and negative syntax") { + Lexer l("(1.0 0.1 -.1 -1. . - -. .-)"); + REQUIRE(l.next() == Token({OpenParen})); + REQUIRE(l.next() == Token({Double, 1.0})); + REQUIRE(l.next() == Token({Double, 0.1})); + REQUIRE(l.next() == Token({Double, -0.1})); + REQUIRE(l.next() == Token({Double, -1.0})); + REQUIRE(l.next() == Token({Symbol, "."})); + REQUIRE(l.next() == Token({Symbol, "-"})); + REQUIRE(l.next() == Token({Symbol, "-."})); + REQUIRE(l.next() == Token({Symbol, ".-"})); + REQUIRE(l.next() == Token({CloseParen})); + } +} +