Adds cmake build support.
This commit is contained in:
26
examples/preinstalled/CMakeLists.txt
Normal file
26
examples/preinstalled/CMakeLists.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(TinyTestPreinstalledExample 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)
|
||||
|
||||
# Find TinyTest package
|
||||
find_package(TinyTest REQUIRED)
|
||||
|
||||
# Create the example executable
|
||||
add_executable(calculator_test
|
||||
main.cpp
|
||||
)
|
||||
|
||||
# Link against the installed TinyTest library
|
||||
target_link_libraries(calculator_test
|
||||
PRIVATE
|
||||
TinyTest::tinytest
|
||||
)
|
||||
|
||||
# Enable CTest
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
add_test(NAME calculator_test COMMAND calculator_test)
|
||||
78
examples/preinstalled/README.md
Normal file
78
examples/preinstalled/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# TinyTest Preinstalled Example
|
||||
|
||||
This example demonstrates how to use TinyTest when it is installed on the system, integrating it through the CMake `find_package` mechanism.
|
||||
|
||||
## What This Example Demonstrates
|
||||
|
||||
- How to use a preinstalled TinyTest library with `find_package`
|
||||
- How to create and run test suites for a simple calculator application
|
||||
- How to test both regular function behavior and exception handling
|
||||
- How to combine and report test results
|
||||
|
||||
## 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, pointing to the TinyTest installation
|
||||
- Builds the project
|
||||
- Runs the tests with CTest
|
||||
|
||||
2. The CMake configuration:
|
||||
- Finds TinyTest with `find_package(TinyTest REQUIRED)`
|
||||
- Links the calculator test executable against `TinyTest::tinytest`
|
||||
- Registers the test with CTest
|
||||
|
||||
3. The test code:
|
||||
- Defines simple calculator functions
|
||||
- Creates test suites for each function
|
||||
- Executes the test suites and collects results
|
||||
- Tests exception handling for division by zero
|
||||
- Prints a summary of the results
|
||||
|
||||
## Code Structure
|
||||
|
||||
- `main.cpp`: Contains the calculator functions and tests
|
||||
- `CMakeLists.txt`: Configures the build system to use the installed TinyTest
|
||||
- `build.sh`/`build.bat`: Scripts to install TinyTest if needed, and build and run the example
|
||||
|
||||
## Understanding the Integration
|
||||
|
||||
The key part of the integration is in the CMakeLists.txt file:
|
||||
|
||||
```cmake
|
||||
# Find TinyTest package
|
||||
find_package(TinyTest REQUIRED)
|
||||
|
||||
# Link against the installed TinyTest library
|
||||
target_link_libraries(calculator_test
|
||||
PRIVATE
|
||||
TinyTest::tinytest
|
||||
)
|
||||
```
|
||||
|
||||
This approach requires TinyTest to be installed on the system, either manually or through a package manager. The build script in this example will install TinyTest automatically if it's not found.
|
||||
45
examples/preinstalled/build.bat
Normal file
45
examples/preinstalled/build.bat
Normal file
@@ -0,0 +1,45 @@
|
||||
@echo off
|
||||
:: Build script for the preinstalled TinyTest example on Windows
|
||||
|
||||
:: Check if TinyTest is installed
|
||||
if not exist "%USERPROFILE%\tinytest-install" (
|
||||
echo TinyTest is not installed. Installing it now...
|
||||
|
||||
:: Store the current directory
|
||||
set EXAMPLE_DIR=%CD%
|
||||
|
||||
:: Go to the root of the TinyTest project (assuming we're in examples/preinstalled)
|
||||
cd ..\..
|
||||
|
||||
:: Build and install TinyTest
|
||||
if not exist build-install mkdir build-install
|
||||
cd build-install
|
||||
cmake .. -DCMAKE_INSTALL_PREFIX=%USERPROFILE%\tinytest-install
|
||||
cmake --build . --config Release
|
||||
cmake --install . --config Release
|
||||
|
||||
:: Return to the example directory
|
||||
cd %EXAMPLE_DIR%
|
||||
|
||||
echo TinyTest has been installed to %USERPROFILE%\tinytest-install
|
||||
)
|
||||
|
||||
echo Building the preinstalled TinyTest example
|
||||
|
||||
:: Create build directory
|
||||
if not exist build mkdir build
|
||||
cd build
|
||||
|
||||
:: Configure the project with the path to the installed TinyTest
|
||||
echo Configuring the project...
|
||||
cmake .. -DCMAKE_PREFIX_PATH=%USERPROFILE%\tinytest-install
|
||||
|
||||
:: Build the project
|
||||
echo Building the project...
|
||||
cmake --build . --config Release
|
||||
|
||||
:: Run the tests
|
||||
echo Running the tests...
|
||||
ctest -C Release --output-on-failure
|
||||
|
||||
echo Preinstalled example has been built and tested successfully!
|
||||
48
examples/preinstalled/build.sh
Executable file
48
examples/preinstalled/build.sh
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
# Build script for the preinstalled TinyTest example
|
||||
|
||||
# Exit on error
|
||||
set -e
|
||||
|
||||
# Check if TinyTest is installed
|
||||
if [ ! -d "$HOME/tinytest-install" ]; then
|
||||
echo "TinyTest is not installed. Installing it now..."
|
||||
|
||||
# Store the current directory
|
||||
EXAMPLE_DIR=$(pwd)
|
||||
|
||||
# Go to the root of the TinyTest project (assuming we're in examples/preinstalled)
|
||||
cd ../../
|
||||
|
||||
# Build and install TinyTest
|
||||
mkdir -p build-install
|
||||
cd build-install
|
||||
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/tinytest-install
|
||||
cmake --build .
|
||||
cmake --install .
|
||||
|
||||
# Return to the example directory
|
||||
cd $EXAMPLE_DIR
|
||||
|
||||
echo "TinyTest has been installed to $HOME/tinytest-install"
|
||||
fi
|
||||
|
||||
echo "Building the preinstalled TinyTest example"
|
||||
|
||||
# Create build directory
|
||||
mkdir -p build
|
||||
cd build
|
||||
|
||||
# Configure the project with the path to the installed TinyTest
|
||||
echo "Configuring the project..."
|
||||
cmake .. -DCMAKE_PREFIX_PATH=$HOME/tinytest-install
|
||||
|
||||
# Build the project
|
||||
echo "Building the project..."
|
||||
cmake --build .
|
||||
|
||||
# Run the tests
|
||||
echo "Running the tests..."
|
||||
ctest --output-on-failure
|
||||
|
||||
echo "Preinstalled example has been built and tested successfully!"
|
||||
116
examples/preinstalled/main.cpp
Normal file
116
examples/preinstalled/main.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tinytest/tinytest.h>
|
||||
|
||||
// Function to test - simple calculator functions
|
||||
double add(double a, double b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
double subtract(double a, double b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
double multiply(double a, double b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
double divide(double a, double b) {
|
||||
if (b == 0) {
|
||||
throw std::invalid_argument("Division by zero");
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Create and execute test suites
|
||||
auto addSuite = TinyTest::MakeTestSuite(
|
||||
"Addition",
|
||||
add,
|
||||
{
|
||||
TinyTest::MakeTest("should correctly add positive numbers",
|
||||
5.0,
|
||||
std::make_tuple(2.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle negative numbers",
|
||||
-1.0,
|
||||
std::make_tuple(-4.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle zeros",
|
||||
3.0,
|
||||
std::make_tuple(3.0, 0.0))
|
||||
}
|
||||
);
|
||||
|
||||
auto subtractSuite = TinyTest::MakeTestSuite(
|
||||
"Subtraction",
|
||||
subtract,
|
||||
{
|
||||
TinyTest::MakeTest("should correctly subtract positive numbers",
|
||||
2.0,
|
||||
std::make_tuple(5.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle negative numbers",
|
||||
-7.0,
|
||||
std::make_tuple(-4.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle zeros",
|
||||
3.0,
|
||||
std::make_tuple(3.0, 0.0))
|
||||
}
|
||||
);
|
||||
|
||||
auto multiplySuite = TinyTest::MakeTestSuite(
|
||||
"Multiplication",
|
||||
multiply,
|
||||
{
|
||||
TinyTest::MakeTest("should correctly multiply positive numbers",
|
||||
15.0,
|
||||
std::make_tuple(5.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle negative numbers",
|
||||
-12.0,
|
||||
std::make_tuple(-4.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle zeros",
|
||||
0.0,
|
||||
std::make_tuple(3.0, 0.0))
|
||||
}
|
||||
);
|
||||
|
||||
auto divideSuite = TinyTest::MakeTestSuite(
|
||||
"Division",
|
||||
divide,
|
||||
{
|
||||
TinyTest::MakeTest("should correctly divide positive numbers",
|
||||
2.0,
|
||||
std::make_tuple(6.0, 3.0)),
|
||||
TinyTest::MakeTest("should handle negative numbers",
|
||||
-2.0,
|
||||
std::make_tuple(-6.0, 3.0))
|
||||
// Division by zero test will be handled separately
|
||||
}
|
||||
);
|
||||
|
||||
// Execute test suites and collect results
|
||||
TinyTest::TestResults results;
|
||||
results += TinyTest::ExecuteSuite(addSuite);
|
||||
results += TinyTest::ExecuteSuite(subtractSuite);
|
||||
results += TinyTest::ExecuteSuite(multiplySuite);
|
||||
results += TinyTest::ExecuteSuite(divideSuite);
|
||||
|
||||
// Manually test the division by zero case
|
||||
std::cout << "Testing division by zero exception handling:" << std::endl;
|
||||
try {
|
||||
divide(1.0, 0.0);
|
||||
std::cout << " ❌FAILED: Division by zero did not throw an exception" << std::endl;
|
||||
results.Fail("Division by zero did not throw an exception");
|
||||
} catch (const std::invalid_argument&) {
|
||||
std::cout << " ✅PASSED: Division by zero correctly threw an exception" << std::endl;
|
||||
results.Pass();
|
||||
} catch (...) {
|
||||
std::cout << " ❌FAILED: Division by zero threw an unexpected exception type" << std::endl;
|
||||
results.Fail("Division by zero threw an unexpected exception type");
|
||||
}
|
||||
|
||||
// Print the summary
|
||||
std::cout << "\nTest Results Summary:\n";
|
||||
TinyTest::PrintResults(std::cout, results);
|
||||
|
||||
// Return non-zero exit code if there were failures or errors
|
||||
return (results.Errors() > 0 || results.Failed() > 0) ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user