Adds cmake build support.
This commit is contained in:
36
examples/standalone/CMakeLists.txt
Normal file
36
examples/standalone/CMakeLists.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(TinyTestStandaloneExample 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)
|
||||
|
||||
# FetchContent for dependency management
|
||||
include(FetchContent)
|
||||
|
||||
# Method 1: Use FetchContent to download TinyTest from GitHub
|
||||
FetchContent_Declare(
|
||||
tinytest
|
||||
GIT_REPOSITORY https://github.com/headhunter45/TinyTest.git
|
||||
GIT_TAG main # or a specific tag/commit
|
||||
)
|
||||
|
||||
# Make TinyTest available
|
||||
FetchContent_MakeAvailable(tinytest)
|
||||
|
||||
# Create the example executable
|
||||
add_executable(tinytest_example
|
||||
example.cpp
|
||||
)
|
||||
|
||||
# Link against the TinyTest library
|
||||
target_link_libraries(tinytest_example
|
||||
PRIVATE
|
||||
tinytest_lib
|
||||
)
|
||||
|
||||
# Enable CTest
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
add_test(NAME example_test COMMAND tinytest_example)
|
||||
75
examples/standalone/README.md
Normal file
75
examples/standalone/README.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# TinyTest Standalone Example
|
||||
|
||||
This example demonstrates how to use TinyTest in a standalone project using CMake's FetchContent to automatically download and build TinyTest.
|
||||
|
||||
## What This Example Demonstrates
|
||||
|
||||
- How to use CMake's FetchContent to automatically download TinyTest
|
||||
- How to create test suites for different types of functions
|
||||
- How to define test cases with expected results
|
||||
- How to execute tests and collect the results
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- CMake 3.14 or higher
|
||||
- C++17 compatible compiler
|
||||
- Git (for FetchContent to download TinyTest)
|
||||
|
||||
## 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. CMake is configured to use FetchContent to download TinyTest
|
||||
2. The example executable is linked against the TinyTest library
|
||||
3. CTest is enabled to run the tests
|
||||
4. Several functions are defined and tested:
|
||||
- `add`: Adds two integers
|
||||
- `concatenate`: Concatenates two strings
|
||||
- `isPrime`: Checks if a number is prime
|
||||
- `vectorSum`: Calculates the sum of a vector of integers
|
||||
|
||||
## Code Structure
|
||||
|
||||
- `example.cpp`: Contains the functions to test and test suites
|
||||
- `CMakeLists.txt`: Configures the build system with FetchContent
|
||||
- `build.sh`/`build.bat`: Scripts to build and run the example
|
||||
|
||||
## Understanding the Integration
|
||||
|
||||
The key part of the integration is in the CMakeLists.txt file:
|
||||
|
||||
```cmake
|
||||
# Use FetchContent to download TinyTest
|
||||
FetchContent_Declare(
|
||||
tinytest
|
||||
GIT_REPOSITORY https://github.com/headhunter45/TinyTest.git
|
||||
GIT_TAG main
|
||||
)
|
||||
|
||||
# Make TinyTest available
|
||||
FetchContent_MakeAvailable(tinytest)
|
||||
|
||||
# Link against the TinyTest library
|
||||
target_link_libraries(tinytest_example
|
||||
PRIVATE
|
||||
tinytest_lib
|
||||
)
|
||||
```
|
||||
|
||||
This approach automatically downloads, builds, and links against TinyTest without requiring a separate installation step.
|
||||
22
examples/standalone/build.bat
Normal file
22
examples/standalone/build.bat
Normal file
@@ -0,0 +1,22 @@
|
||||
@echo off
|
||||
:: Build script for the standalone TinyTest example on Windows
|
||||
|
||||
echo Building TinyTest standalone example
|
||||
|
||||
:: Create build directory
|
||||
if not exist build mkdir build
|
||||
cd build
|
||||
|
||||
:: Configure the project
|
||||
echo Configuring the project...
|
||||
cmake ..
|
||||
|
||||
:: 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 Standalone example has been built and tested successfully!
|
||||
25
examples/standalone/build.sh
Executable file
25
examples/standalone/build.sh
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
# Build script for the standalone TinyTest example
|
||||
|
||||
# Exit on error
|
||||
set -e
|
||||
|
||||
echo "Building TinyTest standalone example"
|
||||
|
||||
# Create build directory
|
||||
mkdir -p build
|
||||
cd build
|
||||
|
||||
# Configure the project
|
||||
echo "Configuring the project..."
|
||||
cmake ..
|
||||
|
||||
# Build the project
|
||||
echo "Building the project..."
|
||||
cmake --build .
|
||||
|
||||
# Run the tests
|
||||
echo "Running the tests..."
|
||||
ctest --output-on-failure
|
||||
|
||||
echo "Standalone example has been built and tested successfully!"
|
||||
129
examples/standalone/example.cpp
Normal file
129
examples/standalone/example.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "tinytest.h"
|
||||
|
||||
// Function to test: Simple add function
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// Function to test: Concatenate strings
|
||||
std::string concatenate(const std::string& a, const std::string& b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// Function to test: Check if a number is prime
|
||||
bool isPrime(int n) {
|
||||
if (n <= 1) return false;
|
||||
if (n <= 3) return true;
|
||||
if (n % 2 == 0 || n % 3 == 0) return false;
|
||||
|
||||
for (int i = 5; i * i <= n; i += 6) {
|
||||
if (n % i == 0 || n % (i + 2) == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Function to test: Vector sum
|
||||
int vectorSum(const std::vector<int>& vec) {
|
||||
int sum = 0;
|
||||
for (int v : vec) {
|
||||
sum += v;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Create test suites using TinyTest
|
||||
|
||||
// Test suite for add function
|
||||
auto addSuite = TinyTest::MakeTestSuite(
|
||||
"AddFunction", // Suite name
|
||||
add, // Function to test
|
||||
{ // Test cases - each defined using MakeTest
|
||||
TinyTest::MakeTest("should add two positive numbers",
|
||||
5, // Expected result
|
||||
std::make_tuple(2, 3)), // Input parameters
|
||||
|
||||
TinyTest::MakeTest("should add a positive and negative number",
|
||||
-1,
|
||||
std::make_tuple(2, -3)),
|
||||
|
||||
TinyTest::MakeTest("should add two negative numbers",
|
||||
-5,
|
||||
std::make_tuple(-2, -3))
|
||||
}
|
||||
);
|
||||
|
||||
// Test suite for concatenate function
|
||||
auto concatSuite = TinyTest::MakeTestSuite(
|
||||
"ConcatenateFunction",
|
||||
concatenate,
|
||||
{
|
||||
TinyTest::MakeTest("should concatenate two strings",
|
||||
std::string("HelloWorld"),
|
||||
std::make_tuple(std::string("Hello"), std::string("World"))),
|
||||
|
||||
TinyTest::MakeTest("should concatenate with empty string",
|
||||
std::string("Hello"),
|
||||
std::make_tuple(std::string("Hello"), std::string("")))
|
||||
}
|
||||
);
|
||||
|
||||
// Test suite for isPrime function
|
||||
auto primeSuite = TinyTest::MakeTestSuite(
|
||||
"IsPrimeFunction",
|
||||
isPrime,
|
||||
{
|
||||
TinyTest::MakeTest("should identify 2 as prime",
|
||||
true,
|
||||
std::make_tuple(2)),
|
||||
|
||||
TinyTest::MakeTest("should identify 4 as not prime",
|
||||
false,
|
||||
std::make_tuple(4)),
|
||||
|
||||
TinyTest::MakeTest("should identify 17 as prime",
|
||||
true,
|
||||
std::make_tuple(17)),
|
||||
|
||||
TinyTest::MakeTest("should identify negative numbers as not prime",
|
||||
false,
|
||||
std::make_tuple(-7))
|
||||
}
|
||||
);
|
||||
|
||||
// Test suite for vectorSum function with a custom compare function
|
||||
auto sumSuite = TinyTest::MakeTestSuite(
|
||||
"VectorSumFunction",
|
||||
vectorSum,
|
||||
{
|
||||
TinyTest::MakeTest("should sum empty vector to 0",
|
||||
0,
|
||||
std::make_tuple(std::vector<int>{})),
|
||||
|
||||
TinyTest::MakeTest("should sum single element vector",
|
||||
5,
|
||||
std::make_tuple(std::vector<int>{5})),
|
||||
|
||||
TinyTest::MakeTest("should sum multiple elements",
|
||||
10,
|
||||
std::make_tuple(std::vector<int>{1, 2, 3, 4}))
|
||||
}
|
||||
);
|
||||
|
||||
// Execute all test suites and get the results
|
||||
TinyTest::TestResults results;
|
||||
results += TinyTest::ExecuteSuite(addSuite);
|
||||
results += TinyTest::ExecuteSuite(concatSuite);
|
||||
results += TinyTest::ExecuteSuite(primeSuite);
|
||||
results += TinyTest::ExecuteSuite(sumSuite);
|
||||
|
||||
// Print the summary of results
|
||||
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