Adds cmake build support.

This commit is contained in:
Tom Hicks
2025-04-09 14:30:09 -07:00
parent a4109d2f49
commit 99f586975c
30 changed files with 2018 additions and 50 deletions

View File

@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.14)
project(TinyTestExample VERSION 1.0.0 LANGUAGES CXX)
# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Debug: Print CMAKE_PREFIX_PATH before finding package
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
# Find TinyTest package
find_package(TinyTest REQUIRED)
# Debug: Print variables after finding TinyTest
message(STATUS "TinyTest found: ${TinyTest_FOUND}")
message(STATUS "TinyTest include dirs: ${TINYTEST_INCLUDE_DIRS}")
message(STATUS "TinyTest libraries: ${TINYTEST_LIBRARIES}")
# Create an example executable that uses TinyTest
add_executable(example_test
main.cpp
)
# Configure include directories explicitly
target_include_directories(example_test
PRIVATE
${TINYTEST_INCLUDE_DIRS}
)
# Link against TinyTest
target_link_libraries(example_test
PRIVATE
TinyTest::tinytest
)
# Add a test using CTest
include(CTest)
enable_testing()
add_test(NAME example_test COMMAND example_test)

View File

@@ -0,0 +1,55 @@
# TinyTest Example Project
This is a sample project that demonstrates how to use TinyTest in a CMake project.
## Prerequisites
- CMake 3.14 or higher
- C++17 compatible compiler
- TinyTest installed (the build script will install it if needed)
## Building and Running
### On Linux/macOS
```bash
# Make the build script executable
chmod +x build.sh
# Run the build script
./build.sh
```
### On Windows
```batch
build.bat
```
## How it Works
1. The build script:
- Checks if TinyTest is installed. If not, it builds and installs it
- Creates a build directory
- Configures the project with CMake
- Builds the project
- Runs the tests with CTest
2. The CMake configuration:
- Finds TinyTest with `find_package(TinyTest REQUIRED)`
- Links the example test executable against `TinyTest::tinytest`
- Registers the test with CTest
3. The test code:
- Includes the TinyTest header
- Defines a test suite using TinyTest's API
- Runs the tests and reports results
## Understanding the Code
The main.cpp file demonstrates:
- How to define a function to test
- How to create test suites
- How to define tests with expected output
- How to execute test suites
- How to print and interpret test results

View File

@@ -0,0 +1,38 @@
@echo off
:: Build script for the sample project on Windows
:: Check if TinyTest is installed
if not exist "%USERPROFILE%\tinytest-install" (
echo TinyTest is not installed in %USERPROFILE%\tinytest-install. Installing it now...
:: Go to the root of the TinyTest project
cd ..\..\
:: Build and install TinyTest
if not exist build mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=%USERPROFILE%\tinytest-install
cmake --build . --config Release
cmake --install . --config Release
:: Return to the sample project directory
cd ..\examples\sample_project
echo TinyTest has been installed.
)
:: Create build directory
if not exist build mkdir build
cd build
:: Configure, build, and test the sample project
echo Configuring the project...
cmake .. -DCMAKE_PREFIX_PATH=%USERPROFILE%\tinytest-install
echo Building the project...
cmake --build . --config Release
echo Running the tests...
ctest -C Release --output-on-failure
echo Example project has been built and tested successfully!

View File

@@ -0,0 +1,59 @@
#!/bin/bash
# Build script for the sample project
# Exit on error
set -e
# Define installation paths to check
TINYTEST_INSTALL_PATHS=("$HOME/tinytest-install" "$HOME/tinytest-verify")
TINYTEST_INSTALLED=false
# Check if TinyTest is installed in any of the potential locations
for INSTALL_PATH in "${TINYTEST_INSTALL_PATHS[@]}"; do
if [ -d "$INSTALL_PATH" ]; then
echo "Found TinyTest installation at $INSTALL_PATH"
TINYTEST_PATH=$INSTALL_PATH
TINYTEST_INSTALLED=true
break
fi
done
# Install TinyTest if not found
if [ "$TINYTEST_INSTALLED" = false ]; then
echo "TinyTest is not installed. Installing it now..."
# Store current directory
EXAMPLE_DIR=$(pwd)
# Go to the root of the TinyTest project
cd ../../
# Build and install TinyTest
mkdir -p build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/tinytest-install
cmake --build .
cmake --install .
# Return to the sample project directory
cd $EXAMPLE_DIR
echo "TinyTest has been installed."
TINYTEST_PATH=$HOME/tinytest-install
fi
# Create build directory
mkdir -p build
cd build
# Configure, build, and test the sample project
echo "Configuring the project..."
cmake .. -DCMAKE_PREFIX_PATH=$TINYTEST_PATH
echo "Building the project..."
cmake --build .
echo "Running the tests..."
ctest --output-on-failure
echo "Example project has been built and tested successfully!"

View File

@@ -0,0 +1,46 @@
#include <iostream>
#include <string>
#include <vector>
#include <tinytest/tinytest.h>
// Function to test
std::string truncate(const std::string& text, int length) {
if (text.length() <= length) {
return text;
}
return text.substr(0, length) + "...";
}
int main() {
// Define test suites using TinyTest
auto truncate_tests = TinyTest::MakeTestSuite(
"TruncateFunction",
truncate,
{
TinyTest::MakeTest(
"should return the original text if it's shorter than the limit",
std::string("Hello"),
std::make_tuple(std::string("Hello"), 10)
),
TinyTest::MakeTest(
"should truncate the text with ellipsis if it's longer than the limit",
std::string("Hello..."),
std::make_tuple(std::string("Hello, World!"), 5)
),
TinyTest::MakeTest(
"should handle empty strings",
std::string(""),
std::make_tuple(std::string(""), 5)
)
}
);
// Execute test suite
auto results = TinyTest::ExecuteSuite(truncate_tests);
// Print results
TinyTest::PrintResults(std::cout, results);
// Return non-zero exit code if any tests failed
return (results.Failed() > 0 || results.Errors() > 0) ? 1 : 0;
}