Skip to content

Commit

Permalink
Add unit testing framework. (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Lin Zhihao <[email protected]>
Co-authored-by: Kirk Rodrigues <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2024
1 parent 0b9e45c commit d9a686e
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 11 deletions.
25 changes: 19 additions & 6 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ on:
- "CMakeLists.txt"
- "examples/**/*"
- "src/**/*"
- "tests/**/*"
- "tools/deps-install/ubuntu/**/*"
push:
paths:
- ".github/workflows/build.yaml"
- "CMakeLists.txt"
- "examples/**/*"
- "src/**/*"
- "tests/**/*"
- "tools/deps-install/ubuntu/**/*"
workflow_call:

concurrency:
Expand All @@ -30,12 +34,21 @@ jobs:
steps:
- uses: "actions/checkout@v4"

- run: "cmake -B ./build -DCMAKE_BUILD_TYPE=${{matrix.build_type}}"
- name: "Install Catch2 on macOS"
if: "matrix.os == 'macos-latest'"
run: "brew install catch2"

- run: "cmake --build ./build --config ${{matrix.build_type}}"
- name: "Install Catch2 on Ubuntu"
if: "matrix.os == 'ubuntu-latest'"
run: "./tools/deps-install/ubuntu/install-catch2.sh 3.6.0"

- run: "cmake --install ./build --prefix ./install"
- name: "Build Executables"
run: |-
cmake -B ./build -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
cmake --build ./build --config ${{matrix.build_type}}
cmake --install ./build --prefix ./install
cmake -S examples -B ./examples/build -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
cmake --build ./examples/build --config ${{matrix.build_type}}
- run: "cmake -S examples -B ./examples/build -DCMAKE_BUILD_TYPE=${{matrix.build_type}}"

- run: "cmake --build ./examples/build --config ${{matrix.build_type}}"
- name: "Run Unit Tests"
run: "ctest --test-dir ./build"
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ project(log_surgeon
LANGUAGES CXX
)

include(GNUInstallDirs)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
find_package(Catch2 3 REQUIRED)
include(Catch)
include(CTest)
endif()

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(default_build_type "Release")
Expand Down Expand Up @@ -144,3 +150,7 @@ install(
DESTINATION
${LCHIP_INSTALL_CONFIG_DIR}
)

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_subdirectory(tests)
endif()
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,33 @@ Requirements:

* CMake
* GCC >= 10 or Clang >= 7
* [Catch2] >= 3
* On Ubuntu <= 20.04, you can install it using:
```shell
sudo tools/deps-install/ubuntu/install-catch2.sh 3.6.0
```
* On Ubuntu >= 22.04, you can install it using:
```shell
sudo apt-get update
sudo apt-get install catch2
```
* On macOS, you can install it using:
```shell
brew install catch2
```

From the repo's root, run:
```shell
# Generate the CMake project
cmake -S . -B build
cmake -S . -B build -DBUILD_TESTING=OFF
# Build the project
cmake --build ./build -j
# Install the project to ~/.local
cmake --install ./build --prefix ~/.local
```
To build the debug version replace the first command with:
`cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Debug`
To build the debug version and tests replace the first command with:
`cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON`
## Documentation and examples
Expand All @@ -105,6 +119,13 @@ To build the debug version replace the first command with:
* `log-surgeon`'s [design objectives](docs/design-objectives.md)
* [examples](examples) contains programs demonstrating usage of the library.

## Testing

To run unit tests, run:
```shell
cmake --build ./build --target test
```

## Linting

Before submitting a PR, ensure you've run our linting tools and either fixed any violations or
Expand Down Expand Up @@ -156,6 +177,7 @@ The following are issues we're aware of and working on:
it also be a part of the variable.
* Support for submatch extraction will be coming in a future release.
[Catch2]: https://github.com/catchorg/Catch2/tree/devel
[feature-req]: https://github.com/y-scope/log-surgeon/issues/new?assignees=&labels=enhancement&template=feature-request.yml
[lint]: https://github.com/y-scope/log-surgeon/blob/main/.github/workflows/lint.yml
[Task]: https://taskfile.dev/
2 changes: 1 addition & 1 deletion lint-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ tasks:
cmds:
- |-
. "{{.G_LINT_VENV_DIR}}/bin/activate"
find src examples \
find src examples tests \
-type f \
\( -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" -o -iname "*.tpp" \) \
-print0 | \
Expand Down
29 changes: 29 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
set(
SOURCES_LOG_SURGEON
../src/log_surgeon/FileReader.cpp
../src/log_surgeon/FileReader.hpp
../src/log_surgeon/finite_automata/RegexAST.hpp
../src/log_surgeon/finite_automata/RegexAST.tpp
../src/log_surgeon/finite_automata/RegexNFA.hpp
../src/log_surgeon/finite_automata/RegexNFA.tpp
../src/log_surgeon/LALR1Parser.cpp
../src/log_surgeon/LALR1Parser.hpp
../src/log_surgeon/LALR1Parser.tpp
../src/log_surgeon/ParserInputBuffer.hpp
../src/log_surgeon/ParserInputBuffer.cpp
../src/log_surgeon/Schema.hpp
../src/log_surgeon/Schema.cpp
../src/log_surgeon/SchemaParser.cpp
../src/log_surgeon/SchemaParser.hpp
../src/log_surgeon/Token.cpp
../src/log_surgeon/Token.hpp
)

set(SOURCES_TESTS test-lexer.cpp)

add_executable(unit-test ${SOURCES_LOG_SURGEON} ${SOURCES_TESTS})
target_link_libraries(unit-test PRIVATE Catch2::Catch2WithMain)
target_include_directories(unit-test PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_compile_features(unit-test PRIVATE cxx_std_20)

catch_discover_tests(unit-test)
20 changes: 20 additions & 0 deletions tests/test-lexer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <catch2/catch_test_macros.hpp>

#include <log_surgeon/Schema.hpp>
#include <log_surgeon/SchemaParser.hpp>

TEST_CASE("Test the Schema class", "[Schema]") {
log_surgeon::Schema schema;
schema.add_variable("myNumber", "123", -1);
auto const schema_ast = schema.release_schema_ast_ptr();
REQUIRE(schema_ast->m_schema_vars.size() == 1);
REQUIRE(schema.release_schema_ast_ptr()->m_schema_vars.empty());

auto& schema_var_ast_ptr = schema_ast->m_schema_vars[0];
REQUIRE(nullptr != schema_var_ast_ptr);
auto& schema_var_ast = dynamic_cast<log_surgeon::SchemaVarAST&>(*schema_var_ast_ptr);
REQUIRE("myNumber" == schema_var_ast.m_name);

auto& regex_ast_cat = schema_var_ast.m_regex_ptr;
REQUIRE(nullptr != regex_ast_cat);
}
99 changes: 99 additions & 0 deletions tools/deps-install/ubuntu/install-catch2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env bash

# Dependencies:
# - cmake
# - curl
# - g++
# NOTE: Dependencies should be installed outside the script to allow the script to be largely
# distro-agnostic

# Exit on any error
set -e

# Error on undefined variable
set -u

cUsage="Usage: ${BASH_SOURCE[0]} <version>[ <.deb output directory>]"
if [ "$#" -lt 1 ]; then
echo "$cUsage"
exit
fi
version=$1

package_name=catch2
temp_dir="/tmp/${package_name}-installation"
deb_output_dir="$temp_dir"
if [[ "$#" -gt 1 ]]; then
deb_output_dir="$(readlink -f "$2")"
if [ ! -d "$deb_output_dir" ]; then
echo "$deb_output_dir does not exist or is not a directory"
exit
fi
fi

# Check if already installed
set +e
dpkg -l ${package_name} | grep "$version"
installed=$?
set -e
if [ $installed -eq 0 ]; then
# Nothing to do
exit
fi

echo "Checking for elevated privileges..."
install_cmd_args=()
if [ ${EUID:-$(id -u)} -ne 0 ]; then
sudo echo "Script can elevate privileges."
install_cmd_args+=("sudo")
fi

# Get number of cpu cores
num_cpus=$(grep -c ^processor /proc/cpuinfo)

# Download
mkdir -p "$temp_dir"
cd "$temp_dir"
extracted_dir="${temp_dir}/Catch2-${version}"
if [ ! -e "${extracted_dir}" ]; then
tar_filename="v${version}.tar.gz"
if [ ! -e "${tar_filename}" ]; then
curl \
-fsSL \
"https://github.com/catchorg/Catch2/archive/refs/tags/${tar_filename}" \
-o "${tar_filename}"
fi

tar -xf "${tar_filename}"
fi

# Build
cd "$extracted_dir"
cmake -B build -S . -DBUILD_TESTING=OFF
cmake --build build --parallel "$num_cpus"

# Check if checkinstall is installed
set +e
command -v checkinstall
checkinstall_installed=$?
set -e

# Install
if [ $checkinstall_installed -eq 0 ]; then
install_cmd_args+=(
checkinstall
--pkgname "$package_name"
--pkgversion "$version"
--provides "$package_name"
--nodoc
-y
--pakdir "$deb_output_dir"
)
fi
install_cmd_args+=(
cmake --install build
)
"${install_cmd_args[@]}"

# Clean up
rm -rf "$temp_dir"

0 comments on commit d9a686e

Please sign in to comment.