diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9bb04e9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,67 @@ +# 2024 Josh O'Leary @ Redacted Software + +cmake_minimum_required(VERSION 3.18...3.25) +project( + jtest + VERSION 1.0 + LANGUAGES CXX +) + +if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) + message(FATAL_ERROR "In-source builds are not allowed!") +endif() + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + +include(cmake/CPM.cmake) + +###################### +# Create a library + + + +file(GLOB_RECURSE jtest_HEADERS "include/jtest/*.h" "include/jtest/*.hpp") +file(GLOB_RECURSE jtest_SRC "src/jtest/*.c" "src/jtest/*.cpp") + +# TODO: Fix Event needing to be included too, it should be an automatically-managed depencency of jlog!!! +CPMAddPackage( + NAME Event + URL https://git.redacted.cc/josh/Event/archive/Release-6.zip +) + +CPMAddPackage( + NAME jlog + URL https://git.redacted.cc/josh/jlog/archive/Prerelease-5.zip +) + +include_directories(${Event_SOURCE_DIR}/include) +include_directories(${jlog_SOURCE_DIR}/include) + +if (UNIX) + add_library(jtest SHARED ${jtest_SRC}) +endif() + +if (WIN32) + add_library(jtest STATIC ${jtest_SRC}) +endif() + +target_include_directories(jtest PUBLIC ${PROJECT_SOURCE_DIR}/include) + + +set_target_properties(jtest PROPERTIES LINKER_LANGUAGE CXX) + +install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME}) +install(FILES ${jtest_HEADERS} DESTINATION include/${PROJECT_NAME}) + +#################### +# Create Executable + + +target_link_libraries(jtest PUBLIC Event jlog) + +# Add an executable with main srces +add_executable(TestSuiteDemo main.cpp) + +# link the new library target with the binary target +target_link_libraries(TestSuiteDemo PUBLIC jtest) \ No newline at end of file diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..d866ad7 --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: MIT +# +# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors + +set(CPM_DOWNLOAD_VERSION 0.38.7) +set(CPM_HASH_SUM "83e5eb71b2bbb8b1f2ad38f1950287a057624e385c238f6087f94cdfc44af9c5") + +if(CPM_SOURCE_CACHE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() + +# Expand relative path. This is important if the provided path contains a tilde (~) +get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) + +file(DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake + ${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM} +) + +include(${CPM_DOWNLOAD_LOCATION}) \ No newline at end of file diff --git a/include/jtest/jtest.hpp b/include/jtest/jtest.hpp new file mode 100644 index 0000000..0d70100 --- /dev/null +++ b/include/jtest/jtest.hpp @@ -0,0 +1,45 @@ +/// Josh Unit Test Library + +#pragma once +#include +#include +#include + +namespace jtest { + + // Requirements + // + + bool check(bool condition) { + if (!condition) + throw; + return condition; + } + + bool test(const std::string& testname, const std::function& callback, const std::string& file, int line) + { + try { + callback(); + } catch(...) { + jlog::log({ + {.colorCode = jlog::ansi_escape_codes::FG_RED, .content = testname}, + {.content = "Failed!"} + }); + return false; + } + + jlog::log({ + {.content = std::format("{}:{}", file, line)}, + {.colorCode = jlog::ansi_escape_codes::FG_GREEN, .content = testname}, + {.content = "Passed!", .delimiter = ""} + }); + return true; + } + + void run_tests() { + + } + +} + +#define TEST(a, b) jtest::test(a, b, __FILE__, __LINE__); diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0bd81cb --- /dev/null +++ b/main.cpp @@ -0,0 +1,26 @@ +// +// Created by dawsh on 6/16/24. +// + +#include + +#include "include/jtest/jtest.hpp" + +// Look into a different mechanism more similar to gtest wherein we have a TEST macro +// that declares and "registers" a test, and then inside our main block we call RUN_ALL_TESTS(argc, argv); + +// Running into a suituation in which we can't catch assertations or "checks" +// Additionally, The tests appear to run out-of-order, but lambda functions shouldn't be asynchronous on their own?!! +int main() +{ + + TEST("Test1", []{ + jtest::check(2+2 == 4); + }); + + TEST("Test2", [] { + jtest::check(2+2 == 5); + }); + + jtest::run_tests(); +} \ No newline at end of file diff --git a/src/jtest/jtest.cpp b/src/jtest/jtest.cpp new file mode 100644 index 0000000..769db3c --- /dev/null +++ b/src/jtest/jtest.cpp @@ -0,0 +1,3 @@ +// +// Created by dawsh on 6/16/24. +//