Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1d61176
fix(matchers): the default description puts one space before the expe…
stellar-aria Nov 14, 2026
128ddd0
fix(matchers): every matcher reports a custom failure message above i…
stellar-aria Nov 14, 2026
8a10f29
feat(pretty): print enums, byte-sized integers and optionals by value
stellar-aria Nov 14, 2026
0579c65
fix(formatters): the verbose formatter prints a failed expectation's …
stellar-aria Nov 14, 2026
a34cffd
feat(matchers)!: expect() keeps a C string as its pointer and the str…
stellar-aria Nov 14, 2026
0ca47e5
fix(matchers): to_start_with and to_end_with on a range never read pa…
stellar-aria Nov 14, 2026
c338d74
fix(formatters): progress lists each failure with its file and line, …
stellar-aria Nov 14, 2026
4fbc540
fix(pretty): a container prints its bools, bytes, enums, optionals an…
stellar-aria Nov 14, 2026
ae7a071
fix(matchers): a null C string to look for fails the search matchers …
stellar-aria Nov 14, 2026
aed3688
docs: describe C strings, custom messages, failure locations and how …
stellar-aria Nov 14, 2026
b5c6cff
build: version 1.1.0
stellar-aria Nov 14, 2026
ce144d4
fix(matchers): ordering matchers compare mixed-sign integers by value…
stellar-aria Nov 14, 2026
19f6830
fix(matchers): to_contain on an empty collection searches without rea…
stellar-aria Nov 14, 2026
a93536e
fix(let): LetBase has a virtual destructor
stellar-aria Nov 14, 2026
aa99b22
fix(argparse): the junit output stream is owned and closed
stellar-aria Nov 14, 2026
b795b97
fix(pretty): type names on MSVC drop the enum, class, struct and unio…
stellar-aria Nov 15, 2026
0b80c5a
fix(pretty): an address prints as 0x and lowercase hex on every platf…
stellar-aria Nov 15, 2026
92a367a
fix(pretty): a std::vector<bool> prints its elements as true and fals…
stellar-aria Nov 15, 2026
c0d4f91
test(pretty): the optional vector's element is written as an optional…
stellar-aria Nov 15, 2026
412c43a
ci: drop the macos-14 runner, which GitHub no longer provides
stellar-aria Nov 15, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ jobs:
fail-fast: false
matrix:
compiler: [native, llvm-20, gcc-14]
os: [ubuntu-latest, windows-latest, macos-14, macos-15]
os: [ubuntu-latest, windows-latest, macos-15]
exclude:
- os: windows-latest
compiler: gcc-14
- os: macos-14
compiler: native # AppleClang is too old

steps:
- name: Checkout code
Expand All @@ -39,7 +37,7 @@ jobs:
echo "CC=$LLVM_PREFIX/bin/clang" >> $GITHUB_ENV
echo "CXX=$LLVM_PREFIX/bin/clang++" >> $GITHUB_ENV
echo "LDFLAGS=-L$LLVM_PREFIX/lib/c++ -Wl,-rpath,$LLVM_PREFIX/lib/c++" >> $GITHUB_ENV
if: ${{ matrix.compiler == 'llvm-20' && (matrix.os == 'macos-14' || matrix.os == 'macos-15') }}
if: ${{ matrix.compiler == 'llvm-20' && matrix.os == 'macos-15' }}

- name: Install LLVM and Clang (Ubuntu)
run: |
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)

project(c++spec
VERSION 1.0.0
VERSION 1.1.0
DESCRIPTION "BDD testing for C++"
LANGUAGES CXX
)
Expand Down
73 changes: 73 additions & 0 deletions docs/syntax/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,76 @@ This allows creating delayed "thunks" that can be created earlier and then passe
auto val = [] { return 5; };
expect(val).to_equal(5);
```

## C strings

`expect` keeps a C string (a `char*`, a `const char*`, or a char array, which decays to one) as
the pointer it is, so pointer matchers such as `to_be_null` work on it:

```cpp
const char* name = nullptr;
expect(name).to_be_null();
```

The string matchers (`to_equal`, `to_contain`, `to_start_with`, `to_end_with`, `to_match` and
`to_partially_match`) read every piece of text through `CppSpec::string_view_of()`, whatever
form it is in: a `std::string`, a `std::string_view`, a char array or a char pointer. Two C
strings are therefore compared by their text, not by their addresses, and a C string equals a
`std::string` holding the same text.

A null C string is not text:

- it equals only another null C string, and differs from every piece of text, the empty string
included, so `expect(name).not_().to_equal("")` passes;
- the matchers that search text (`to_contain`, `to_start_with`, `to_end_with`, `to_match`,
`to_partially_match`) fail on it **both** positive and negated, whether it is the actual
value or the text or pattern looked for, and say so below their message
(`nullptr is not text, so there is nothing to search` or `... nothing to look for`);
- it prints as `nullptr`.

A char array given to `expect` decays to a pointer, so it is read as a C string, up to its first
NUL: it must be NUL-terminated. Wrap a fixed-size buffer that may not be in a
`std::string_view(buffer, size)`.

A `volatile char*`, a `signed char*`, an `unsigned char*` and a pointer to a wide character
type are not C strings: they are compared as pointers and printed as addresses (`nullptr` when
null).

## Custom messages

Every matcher takes an optional last argument, a message that describes the expectation. When
the expectation fails, the message is printed **above** the matcher's own text, which still
shows both values:

```cpp
expect(gain).to_equal(2, "the gain");
```

```
the gain
expected (int) => 2
got (int) => 1
Compared using `==`
```

## How values are printed

A failure message prints each value by its `operator<<` where it has one, with these
exceptions, which also apply to the elements of a standard container, built-in array, pair or
tuple, and of any other range with no `operator<<` of its own (a range type with its own
`operator<<` is printed through it):

| Value | Printed as |
| ----- | ---------- |
| `bool` | `true` / `false` |
| `std::int8_t`, `std::uint8_t` (`signed char`, `unsigned char`) | the number: `65`, `-3` |
| `wchar_t`, `char8_t`, `char16_t`, `char32_t` | the number of the code unit: `97` |
| `char` | the character: `A` |
| an enum with no `operator<<` (scoped, `std::byte` included) | its type and number: `Colour(2)`, `std::byte(65)` |
| `std::optional<T>` | its value, or `std::nullopt` |
| `std::optional<std::optional<T>>` holding an optional | `std::optional(...)`, so `std::optional(std::nullopt)` reads apart from `std::nullopt` |
| a C string | its text, or `nullptr` |
| a pointer to `signed char`, `unsigned char` or a wide character | its address, or `nullptr` |

So a failed comparison of two byte buffers reads `[65, 67]` against `[65, 66]`, and a container
keeps its delimiters: `[1, 2]`, `{1, 2}` for a set, `(1, 2)` for a pair or tuple.
50 changes: 50 additions & 0 deletions docs/syntax/matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ expect(x).to_equal(0); // passes when x == 0
expect(x).not_().to_equal(0); // passes when x != 0
```

Every matcher also takes an optional last argument, a message printed above the matcher's own
text when the expectation fails (see [Expect](expect.md#custom-messages)):

```cpp
expect(gain).to_equal(2, "the gain");
```

Text in any form (`std::string`, `std::string_view`, a char array or a C string) is read
through `CppSpec::string_view_of()`; see [Expect](expect.md#c-strings) for how C strings, null
ones included, are compared and printed.

---

## Equality & boolean
Expand All @@ -21,6 +32,9 @@ expect(1 + 1).to_equal(2);
expect(std::string{"hi"}).to_equal("hi");
```

Two pieces of text are compared as text, whatever form each is in, so two C strings with the same
text at different addresses are equal. A null C string equals only another null C string.

### to_be_true / to_be_false

Strict boolean check (value must be exactly `true` or `false`):
Expand Down Expand Up @@ -50,6 +64,9 @@ expect(p).to_be_null();

int x = 1;
expect(&x).not_().to_be_null();

const char* name = nullptr; // a C string stays a pointer, so this works too
expect(name).to_be_null();
```

---
Expand All @@ -63,8 +80,13 @@ Passes when the container holds the given element (uses `std::ranges::find`):
```cpp
expect(std::vector<int>{1, 2, 3}).to_contain(2);
expect(std::string{"hello"}).to_contain('e');
expect("hello").to_contain("ell");
```

On text, a null C string, as the text searched or the text looked for, fails both positive and
negated. In a container that is not text, such as `std::vector<const char*>`, a null pointer is an
element like any other.

### to_contain (multiple elements)

Passes when **all** listed elements are present. Negated form passes when **none** are present:
Expand All @@ -80,9 +102,16 @@ For strings, checks the leading prefix. For containers, checks the leading sub-s

```cpp
expect(std::string{"hello world"}).to_start_with("hello");
expect(std::string_view{"hello world"}).to_start_with(std::string{"hello"});
expect(std::vector<int>{1, 2, 3, 4}).to_start_with({1, 2});
expect(std::vector<char>{'a', 'b', 'c'}).to_start_with("ab");
```

The text may be in any form `string_view_of()` reads. Of the ranges that are not text, only a range
of `char`, such as `std::vector<char>`, takes text to compare with; for any other range,
`to_start_with("ab")` does not compile. A null C string, on either side, fails both positive and
negated.

### to_end_with

For strings, checks the trailing suffix. For containers, checks the trailing sub-sequence:
Expand All @@ -92,6 +121,8 @@ expect(std::string{"hello world"}).to_end_with("world");
expect(std::vector<int>{1, 2, 3, 4}).to_end_with({3, 4});
```

As with `to_start_with`, a null C string on either side fails both positive and negated.

---

## Numeric
Expand All @@ -105,6 +136,8 @@ expect(5).to_be_between(1, 10);
expect(5).to_be_between(1, 10, RangeMode::exclusive); // strict bounds
```

Negated, it passes only for a value below the lower bound or above the upper one.

### to_be_within

Floating-point proximity check. Use `.of(expected)` to specify the target:
Expand All @@ -120,6 +153,12 @@ expect(3).to_be_less_than(5);
expect(7).to_be_greater_than(4);
```

`to_be_less_than`, `to_be_greater_than` and `to_be_between` compare two integers by value, as `std::cmp_less` does,
so a negative number is below every unsigned one: `expect(-1).to_be_less_than(0u)` passes. `bool` and the character
types, and every non-integer type, are compared with the operators instead. Negated, each checks the strict opposite
(`not_().to_be_greater_than(x)` is `actual <= x`), so NaN fails these matchers negated or not, and a type compared
negated needs that opposite operator too.

---

## Strings
Expand All @@ -136,6 +175,17 @@ expect(std::string{"HELLO"}).to_match(std::regex("hello", std::regex::icase));
Note: `to_match` uses `std::regex_match` which requires the **entire** string to match the
pattern.

### to_partially_match

Passes when the pattern matches **somewhere** in the string (`std::regex_search`):

```cpp
expect(std::string{"hello123"}).to_partially_match("[0-9]+");
```

For both, the pattern may be given as text in any form `string_view_of()` reads. A null C string,
as the text or as the pattern, fails both positive and negated.

---

## Custom predicate
Expand Down
13 changes: 13 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ Pass a formatter to `CppSpec::parse`:
CppSpec::parse(argc, argv, CppSpec::Formatters::verbose)
```

`verbose` and `progress` (the default) start each failure message with the file and line of the
failing expectation, so an editor or terminal can jump to it:

```
spec/gain_spec.cpp:12: the gain
expected (int) => 2
got (int) => 1
Compared using `==`
```

`progress` lists only the expectations that failed. The `tap` and `junit` formatters report the
file and line in their own fields.

## Example

```cpp
Expand Down
19 changes: 16 additions & 3 deletions include/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <argparse/argparse.hpp>
#include <fstream>
#include <memory>
#include <string>
#include <string_view>
#include "formatters/junit_xml.hpp"
Expand All @@ -22,6 +23,19 @@ inline std::string file_name(std::string_view path) {
return std::string{file};
}

/**
* @brief The file that --output-junit names, with the JUnit XML formatter that writes to it
*
* The formatter is declared after the stream, so it is destroyed first: its destructor writes the XML to a stream
* that is still open, and the stream is closed after it.
*/
struct JUnitOutputFile {
std::ofstream stream;
Formatters::JUnitXML formatter{stream, false};

explicit JUnitOutputFile(const std::string& path) : stream(path) {}
};

inline Runner parse(int argc, char** const argv) {
std::filesystem::path executable_path = argv[0];
std::string executable_name = executable_path.filename().string();
Expand Down Expand Up @@ -61,9 +75,8 @@ inline Runner parse(int argc, char** const argv) {

auto junit_output_filepath = program.get<std::string>("--output-junit");
if (!junit_output_filepath.empty()) {
// open file stream
auto* file_stream = new std::ofstream(junit_output_filepath);
auto junit_output = std::make_shared<Formatters::JUnitXML>(*file_stream, false);
auto junit_file = std::make_shared<JUnitOutputFile>(junit_output_filepath);
std::shared_ptr<Formatters::BaseFormatter> junit_output{junit_file, &junit_file->formatter};
return Runner{formatter, junit_output};
}
return Runner{formatter};
Expand Down
60 changes: 48 additions & 12 deletions include/expectations/expectation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,23 @@ class Expectation {

/*-------- to... ----------*/

void to_end_with(std::string ending, std::string msg = "");
template <Util::is_string_like T>
requires Util::compares_start_and_end<A, T>
void to_end_with(T ending, std::string msg = "");
void to_fail(std::string msg = "");
void to_fail_with(std::string failure_message, std::string msg = "");
void to_match(std::regex regex, std::string msg = "");
void to_match(std::string str, std::string msg = "");
template <Util::is_string_like T>
void to_match(T pattern, std::string msg = "");
void to_partially_match(std::regex regex, std::string msg = "");
void to_partially_match(std::string str, std::string msg = "");
template <Util::is_string_like T>
void to_partially_match(T pattern, std::string msg = "");
template <typename F>
requires std::invocable<F, A> && std::convertible_to<std::invoke_result_t<F, A>, bool>
void to_satisfy(F test, std::string msg = "");
void to_start_with(std::string start, std::string msg = "");
template <Util::is_string_like T>
requires Util::compares_start_and_end<A, T>
void to_start_with(T start, std::string msg = "");

template <typename U>
void to_contain(std::initializer_list<U> expected, std::string msg = "");
Expand Down Expand Up @@ -328,19 +334,33 @@ void Expectation<A>::to_fail_with(std::string failure_message, std::string msg)
Matchers::FailWith<A>(*this, failure_message).set_message(std::move(msg)).run();
}

/**
* @brief Match using the Matchers::Match matcher, with a pattern given as text.
*
* @param pattern the pattern, in any form string_view_of() reads; a null C string fails, negated or not.
* @param msg Optional message to give on failure.
*/
template <typename A>
void Expectation<A>::to_match(std::string str, std::string msg) {
Matchers::Match<A>(*this, str).set_message(std::move(msg)).run();
template <Util::is_string_like T>
void Expectation<A>::to_match(T pattern, std::string msg) {
Matchers::Match<A>(*this, pattern).set_message(std::move(msg)).run();
}

template <typename A>
void Expectation<A>::to_match(std::regex regex, std::string msg) {
Matchers::Match<A>(*this, regex).set_message(std::move(msg)).run();
}

/**
* @brief Match using the Matchers::MatchPartial matcher, with a pattern given as text.
*
* @param pattern the pattern, in any form string_view_of() reads; a null C string fails, negated or not.
* @param msg Optional message to give on failure.
*/
template <typename A>
void Expectation<A>::to_partially_match(std::string str, std::string msg) {
Matchers::MatchPartial<A>(*this, str).set_message(std::move(msg)).run();
template <Util::is_string_like T>
void Expectation<A>::to_partially_match(T pattern, std::string msg) {
Matchers::MatchPartial<A>(*this, pattern).set_message(std::move(msg)).run();
}

template <typename A>
Expand All @@ -364,9 +384,17 @@ void Expectation<A>::to_satisfy(F test, std::string msg) {
Matchers::Satisfy<A>(*this, std::function<bool(A)>(std::move(test))).set_message(std::move(msg)).run();
}

/**
* @brief Match using the Matchers::StartWith matcher, with the start given as text.
*
* @param start the text, in any form string_view_of() reads; a null C string fails, negated or not.
* @param msg Optional message to give on failure.
*/
template <typename A>
void Expectation<A>::to_start_with(std::string start, std::string msg) {
Matchers::StartWith<std::string, std::string>(*this, start).set_message(std::move(msg)).run();
template <Util::is_string_like T>
requires Util::compares_start_and_end<A, T>
void Expectation<A>::to_start_with(T start, std::string msg) {
Matchers::StartWith<A, T>(*this, start).set_message(std::move(msg)).run();
}

template <typename A>
Expand All @@ -375,9 +403,17 @@ void Expectation<A>::to_start_with(std::initializer_list<U> start_sequence, std:
Matchers::StartWith<A, std::initializer_list<U>>(*this, start_sequence).set_message(std::move(msg)).run();
}

/**
* @brief Match using the Matchers::EndWith matcher, with the ending given as text.
*
* @param ending the text, in any form string_view_of() reads; a null C string fails, negated or not.
* @param msg Optional message to give on failure.
*/
template <typename A>
void Expectation<A>::to_end_with(std::string ending, std::string msg) {
Matchers::EndWith<std::string, std::string>(*this, ending).set_message(std::move(msg)).run();
template <Util::is_string_like T>
requires Util::compares_start_and_end<A, T>
void Expectation<A>::to_end_with(T ending, std::string msg) {
Matchers::EndWith<A, T>(*this, ending).set_message(std::move(msg)).run();
}

template <typename A>
Expand Down
Loading
Loading