Initial work, running into problems already. See main.cpp for comments.

This commit is contained in:
2024-06-17 15:08:16 -04:00
parent a7941918c3
commit 22175b25a9
5 changed files with 165 additions and 0 deletions

67
CMakeLists.txt Normal file
View File

@@ -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)

24
cmake/CPM.cmake Normal file
View File

@@ -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})

45
include/jtest/jtest.hpp Normal file
View File

@@ -0,0 +1,45 @@
/// Josh Unit Test Library
#pragma once
#include <functional>
#include <string>
#include <jlog/jlog.hpp>
namespace jtest {
// Requirements
//
bool check(bool condition) {
if (!condition)
throw;
return condition;
}
bool test(const std::string& testname, const std::function<void()>& 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__);

26
main.cpp Normal file
View File

@@ -0,0 +1,26 @@
//
// Created by dawsh on 6/16/24.
//
#include <cassert>
#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();
}

3
src/jtest/jtest.cpp Normal file
View File

@@ -0,0 +1,3 @@
//
// Created by dawsh on 6/16/24.
//