Skip to main content

Compile-time test discovery and execution for C++ headers using Tree-sitter

Project description

cpp-compile-test

A fast, scriptable tool for compile-time testing of C++ code.
Designed to parse C++ header files for test case definitions, and validate that each one does or does not compile, based on expected behavior.

This tool is particularly useful for:

  • Libraries with SFINAE/constraint-based logic
  • Metaprogramming-heavy APIs
  • Validating invalid/ill-formed usage
  • Compile-time regression testing

Features

  • Parses test structs using Tree-sitter C++ grammar
  • Extracts test metadata via static constexpr fields
  • Supports expected compile or failure behaviors
  • Runs tests in parallel for fast execution
  • Customizable compiler settings via JSON or CLI
  • Supports cl, gcc, clang, or any custom compiler
  • First-class support for MSVC, including pre-launch PowerShell script

Installation

From PyPI:

pip install cpp-compile-test

Usage

Basic CLI Example

cpp-compile-test --config msvc_settings.json --header my_tests.hpp --source main.cpp
  • --config: Path to a JSON file with compiler settings
  • --header: Header file containing test case structs
  • --source: Source file used for actual test compilation (should reference the test struct)

Example Test Struct (in C++)

struct simple_cast_test {
   static constexpr const char* id = "simple_cast";
   static constexpr bool expect_error = false;
   static constexpr const char* description = "Cast a length to a base_dimension length";

   template<typename = void>
   static void run() {
      Length<Meters> myLength{10.0};
      BaseDimension<UnitExponent<Meters>> other = myLength;
   }
};

Example msvc_settings.json

{
  "compiler": "cl",
  "compiler_type": "cl",
  "flags": [
    "/std:c++20",
    "/nologo",
    "/c",
    "/EHsc",
    "/Ipath\to\headers"
  ]
}

Testing

To run unit tests:

pytest

Development

Regenerate model from JSON schema

python scripts/generate_schema_models.py

Make sure to install:

pip install datamodel-code-generator

Setting up MSVC environment (PowerShell)

.\scripts\setup_msvc_env.ps1

Roadmap

  • GitLab CI pipeline (build, lint, test)
  • Pylint / Ruff integration
  • Automated schema-to-model regeneration
  • PyPI publishing from GitLab pipeline

Contributing

Contributions welcome! Submit issues, PRs, or feature requests.


License

Apache 2.0 License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpp_compile_test-1.0.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cpp_compile_test-1.0.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file cpp_compile_test-1.0.0.tar.gz.

File metadata

  • Download URL: cpp_compile_test-1.0.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cpp_compile_test-1.0.0.tar.gz
Algorithm Hash digest
SHA256 09ea28cdb4faf659d15f3a7500efc397b0a42fe6fa670a9b531d1a44611e2a07
MD5 233f366a87475383551cda6804eb64c0
BLAKE2b-256 6a7533b9680b4cdbb3782564e70884dd080f77edb0f31ff65c9e9df4d3a61843

See more details on using hashes here.

File details

Details for the file cpp_compile_test-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cpp_compile_test-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a424103d477df1cac45a1fb66af30a8bf0431363b5a246daa2d95ecde5f1190
MD5 66102f52082e9d325409b23a0de84d9a
BLAKE2b-256 acc6c5ff8950faad8735cc39366e5206d5dda811b8ecc596f45a830b2e151c19

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page