Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jobs:
mcpp test -p mcpplibs.mtest.manifest
mcpp test -p mtest.mtest
mcpp test -p conformance
mcpp test -p mcpplibs.mtest.doctest
- name: Every implementation passes the conformance suite
run: |
set -euo pipefail
Expand Down
21 changes: 19 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# Changelog

## [Unreleased]

## [0.11.4] - 2026-11-23

- **The doctest adapter reports what doctest knows** (#29). Identifiers are distinct in a program
(P3.2): of two cases with one name, in doctest's order (file, then line), the later is
`<suite>/<name>#2`, and `--split case` gives each its process. A case decorated `should_fail` or
`expected_failures(n)` is listed `expect: "fail"` and ends `xfail` when it fails as expected, and
`xpass` when nothing fails (P5.3); `may_fail` states no expectation and stays `pass`. A case's
`doctest::timeout` is listed as its `timeout` (O5). An exception or a crash fails at the case's
own place, not at `:0`. The guide states these rules, that doctest's queries under a runner
report to the report, and that a file whose `main` comes from a header declares it.
- **The protocol's variables stay out of what a case starts** (P2.2, #29). The doctest and GoogleTest
adapters read `MTEST_PROTOCOL`, `MTEST_MODE`, `MTEST_REPORT`, `MTEST_SELECT` and the other
variables of the handshake, then remove them from the C library's environment before any case
runs, so a test program a case starts no longer appends to its parent's report. `mcpplibs.os`
states no change to a program's own environment, so the adapters call the host's C library
(mcpplibs/os#29); the library `mcpplibs.mtest` still strips them only from what `e2e::run`
starts. The doctest adapter's own test checks it under `mcpp test`.
- The conformance suite holds an implementation below level 3 that expresses `expected to fail` to
its `xfail`; the doctest fixture expresses it.
- CI builds and tests with mcpp 2026.11.22.1 and xlings 2026.11.21.1, the xlings that release
bundles (`.github/versions.env`).

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main() {

```toml
[dev-dependencies]
mcpplibs.mtest = "0.11.3"
mcpplibs.mtest = "0.11.4"
```

A failure names its place and both sides (`expected 2, actual 3`). Under a runner the same program
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main() {

```toml
[dev-dependencies]
mcpplibs.mtest = "0.11.3"
mcpplibs.mtest = "0.11.4"
```

失败时给出位置与两侧的值(`expected 2, actual 3`)。在运行器之下,同一程序按协议报告;单独运行时每个用例输出一行。
Expand Down
24 changes: 21 additions & 3 deletions adapters/doctest/docs/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package `mcpplibs.mtest.doctest` supplies doctest's implementation and the progr

```toml
[dev-dependencies]
mcpplibs.mtest.doctest = "0.11.3"
mcpplibs.mtest.doctest = "0.11.4"
```

```cpp
Expand All @@ -20,6 +20,24 @@ TEST_SUITE("parse") {
`DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN`: the implementation is compiled once, in the package, and not
in each file.
- A case's identifier is `<suite>/<name>` (`parse/an empty line`), or `<name>` outside a
`TEST_SUITE`; it is what `mcpp test --case` and `MTEST_SELECT` name.
`TEST_SUITE`; it is what `mcpp test --case` and `MTEST_SELECT` name. Identifiers are distinct in a
program: of two cases with one name, in doctest's order (file, then line), the later is
`<suite>/<name>#2`, a third `#3`; the case's `name` stays doctest's.
- A case skipped by `doctest::skip()` is reported skipped, its `doctest::description` the reason.
- With no runner the program is doctest's own main and takes doctest's options.
- A case decorated `doctest::should_fail()` or `doctest::expected_failures(n)` is listed as expected
to fail: it ends `xfail` when it fails (`expected_failures`: exactly `n` times), and `xpass`, which
fails the run, when nothing fails. A case it fails otherwise (`expected_failures`: another number
of times) fails. `doctest::may_fail()` states no expectation: the case passes either way.
- `doctest::timeout(s)` is listed as the case's `timeout`: doctest judges it only when the case
returns, and a runner that starts a process per case ends the case at it.
- A case that throws or crashes fails at its own place, the `TEST_CASE` line.
- With no runner the program is doctest's own main and takes doctest's options. Under a runner
(`MTEST_PROTOCOL`), doctest's queries (`--list-test-cases`) report through the protocol: the names
go to the report, not to standard output.
- Under a runner the adapter reads the protocol's variables (`MTEST_PROTOCOL`, `MTEST_REPORT`, ...)
and removes them from the program's environment before any case runs (P2.2): a program a case
starts, by `std::system` or otherwise, is not taken for the runner's, and a case reads none of
them.
- The package's `main` is linked into a test file whose text defines none. A file whose `main` comes
from a header (`<boost/test/included/unit_test.hpp>`) declares it, `int main(int, char*[]);`, so
that it is not linked twice.
6 changes: 5 additions & 1 deletion adapters/doctest/fixture/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// The conformance fixture of the doctest adapter (spec/conformance.md), level 1: the cases doctest
// can state, named `<suite>/<name>` (`conformance/passes`), with no tags, kinds or metrics.
// can state, named `<suite>/<name>` (`conformance/passes`), with no tags, kinds or metrics; the
// expected failure is doctest's `should_fail`.
// doctest's implementation and the main are the adapter's (src/protocol.cpp, src/main.cpp).
#include <doctest/doctest.h>

Expand All @@ -9,6 +10,9 @@ TEST_SUITE("conformance") {
CHECK(1 == 2);
}
TEST_CASE("skips" * doctest::skip() * doctest::description("not here")) {}
TEST_CASE("expected to fail" * doctest::should_fail()) {
CHECK(1 == 2);
}
TEST_CASE("measures") {}
TEST_CASE("covers") {}
TEST_CASE("holds") {}
Expand Down
12 changes: 7 additions & 5 deletions adapters/doctest/mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
# neither (no `DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN`):
#
# [dev-dependencies]
# mcpplibs.mtest.doctest = "0.11.3"
# mcpplibs.mtest.doctest = "0.11.4"
#
# A case's identifier is `<suite>/<name>` (P3.2), or `<name>` outside a `TEST_SUITE`. doctest's
# macros exist only in its header, so this package's one source includes it, and nlohmann/json's
# beside it, as the GoogleTest adapter does.
# A case's identifier is `<suite>/<name>` (P3.2), or `<name>` outside a `TEST_SUITE`, with `#<n>` for
# the second and later case of one name; `should_fail` and `expected_failures` are expected failures,
# `timeout` the case's timeout (docs/writing-tests.md). doctest's macros exist only in its header,
# so this package's one source includes it, and nlohmann/json's beside it, as the GoogleTest adapter
# does.
[package]
namespace = "mcpplibs.mtest"
name = "doctest"
version = "0.11.3"
version = "0.11.4"
description = "doctest speaking the mtest protocol: doctest's implementation and a test main that lists, selects and reports its cases"
license = "Apache-2.0"

Expand Down
109 changes: 97 additions & 12 deletions adapters/doctest/src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// writes the registered test cases as cases without running any (P3); a selection (MTEST_SELECT)
// runs exactly the named cases and an identifier no case has is `missing` (P4.1); a reporter writes
// each case's begin, failures and end (P5). A case doctest skips by its decorator is reported
// skipped, with its description as the reason.
// skipped, with its description as the reason; one `should_fail` or `expected_failures` decorates
// is expected to fail (`xfail`, `xpass`); `timeout` is stated to the runner (O5).
#define DOCTEST_CONFIG_IMPLEMENT
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <doctest/doctest.h>
Expand All @@ -28,6 +30,37 @@ std::string env(const char* name) {
return v ? v : "";
}

// P2.2: the programs a case starts do not get the protocol's variables, so a test program a case
// starts is not taken for the runner's; a case that runs one as its runner states them itself.
// Once read, they leave the C library's environment, which `std::system`, `posix_spawn` and a
// started program inherit, before any case runs. A case therefore reads none of them.
constexpr const char* protocol_variables[] = {"MTEST_PROTOCOL", "MTEST_MODE",
"MTEST_REPORT", "MTEST_SELECT",
"MTEST_ISOLATION", "MTEST_TMPDIR",
"MTEST_SEED", "MTEST_MONITOR",
"MTEST_MONITOR_SAMPLE", "MTEST_SERVE"};

// The C library's own call, as each host names it: mcpplibs.os states no change to a program's own
// environment (mcpplibs/os#29).
void remove_variable(const char* name) {
#if defined(_WIN32)
_putenv_s(name, "");
#else
unsetenv(name);
#endif
}

// What the runner stated (P2.1), read once and then removed.
struct handshake {
std::string protocol, mode, report, select;
};
handshake take_handshake() {
handshake h{env("MTEST_PROTOCOL"), env("MTEST_MODE"), env("MTEST_REPORT"), env("MTEST_SELECT")};
for (const char* name : protocol_variables)
remove_variable(name);
return h;
}

// The report (P5.1): one event per line, written whole.
struct report_file {
std::ofstream out;
Expand All @@ -53,11 +86,55 @@ std::map<std::string, int>& counts() {
return c;
}

std::string id_of(const doctest::TestCaseData& t) {
std::string name_of(const doctest::TestCaseData& t) {
const std::string suite = t.m_test_suite ? t.m_test_suite : "";
return suite.empty() ? std::string(t.m_name) : suite + "/" + t.m_name;
}

// The identifiers of the registered cases (P3.2: distinct in a program). doctest accepts two cases
// of one name; in doctest's order (file, then line) the first is `<suite>/<name>` and each later
// one `<suite>/<name>#<n>`, n from 2.
std::map<const doctest::TestCaseData*, std::string> assign_identifiers() {
std::vector<const doctest::TestCaseData*> cases;
for (const auto& t : doctest::detail::getRegisteredTests())
cases.push_back(&t);
std::stable_sort(cases.begin(), cases.end(), [](const auto* a, const auto* b) {
const int file = a->m_file.compare(b->m_file);
return file != 0 ? file < 0 : a->m_line < b->m_line;
});
std::map<const doctest::TestCaseData*, std::string> ids;
std::set<std::string> taken;
for (const auto* t : cases) {
const auto name = name_of(*t);
auto id = name;
for (int n = 2; taken.contains(id); ++n)
id = name + "#" + std::to_string(n);
taken.insert(id);
ids.emplace(t, id);
}
return ids;
}

std::string id_of(const doctest::TestCaseData& t) {
static const auto ids = assign_identifiers();
const auto found = ids.find(&t);
return found != ids.end() ? found->second : name_of(t);
}

// A case's status (P5.3) from doctest's reasons: `should_fail` and `expected_failures(n)` state an
// expectation, `may_fail` states none.
std::string status_of(const doctest::CurrentTestCaseStats& st) {
namespace reason = doctest::TestCaseFailureReason;
if (st.failure_flags & (reason::ShouldHaveFailedAndDid | reason::FailedExactlyNumTimes))
return "xfail";
if (st.failure_flags & reason::ShouldHaveFailedButDidnt)
return "xpass";
if ((st.failure_flags & reason::DidntFailExactlyNumTimes) &&
st.numAssertsFailedCurrentTest == 0)
return "xpass";
return st.testCaseSuccess ? "pass" : "fail";
}

const auto began = std::chrono::steady_clock::now();
long long ms_since(std::chrono::steady_clock::time_point at) {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() -
Expand All @@ -77,29 +154,34 @@ class mtest_reporter : public doctest::IReporter {
if (t.m_skip) {
e["expect"] = "skip";
e["reason"] = t.m_description && *t.m_description ? t.m_description : "skipped";
}
} else if (t.m_should_fail || t.m_expected_failures > 0)
e["expect"] = "fail";
if (t.m_timeout > 0)
e["timeout"] = t.m_timeout;
report().write(e);
}
}
void test_run_start() override {}
void test_run_end(const doctest::TestRunStats&) override {}
void test_case_start(const doctest::TestCaseData& t) override {
case_ = &t;
current_ = id_of(t);
started_ = std::chrono::steady_clock::now();
report().write({{"e", "begin"}, {"id", current_}, {"t", ms_since(began)}});
}
void test_case_reenter(const doctest::TestCaseData&) override {}
void test_case_end(const doctest::CurrentTestCaseStats& st) override {
const std::string status = st.testCaseSuccess ? "pass" : "fail";
const std::string status = status_of(st);
++counts()[status];
report().write(
{{"e", "end"}, {"id", current_}, {"status", status}, {"ms", ms_since(started_)}});
}
// doctest gives an exception or a crash no place; the case's own is its place.
void test_case_exception(const doctest::TestCaseException& e) override {
report().write({{"e", "failure"},
{"id", current_},
{"file", ""},
{"line", 0},
{"file", case_ ? case_->m_file.c_str() : ""},
{"line", case_ ? case_->m_line : 0},
{"message", std::string(e.is_crash ? "crashed: " : "threw: ") +
e.error_string.c_str()},
{"fatal", true}});
Expand Down Expand Up @@ -137,6 +219,7 @@ class mtest_reporter : public doctest::IReporter {
{{"e", "end"}, {"id", id}, {"status", "skip"}, {"ms", 0}, {"reason", reason}});
}
private:
const doctest::TestCaseData* case_ = nullptr;
std::string current_;
std::chrono::steady_clock::time_point started_;
};
Expand All @@ -151,8 +234,9 @@ extern "C++" int mcpplibs_mtest_doctest_main(int argc, char** argv);
int mcpplibs_mtest_doctest_main(int argc, char** argv) {
doctest::Context context;
context.applyCommandLine(argc, argv);
const std::string path = env("MTEST_REPORT");
if (env("MTEST_PROTOCOL").empty() || path.empty())
const auto runner = take_handshake();
const std::string& path = runner.report;
if (runner.protocol.empty() || path.empty())
return context.run();
const char* volatile keep = mcpplibs_mtest_doctest_marker;
(void)keep;
Expand All @@ -162,12 +246,12 @@ int mcpplibs_mtest_doctest_main(int argc, char** argv) {
{"protocol", 1},
{"level", 1},
{"framework", "doctest"},
{"version", "0.11.3"},
{"version", "0.11.4"},
{"isolation", {"process", "case"}},
{"guide", "https://github.com/mcpp-community/mtest/blob/main/adapters/doctest/docs/"
"writing-tests.md"}});
// The list is the registry's: doctest's own listing leaves out a case its decorator skips.
if (env("MTEST_MODE") == "list") {
if (runner.mode == "list") {
std::vector<const doctest::TestCaseData*> all;
for (const auto& t : doctest::detail::getRegisteredTests())
all.push_back(&t);
Expand All @@ -182,7 +266,7 @@ int mcpplibs_mtest_doctest_main(int argc, char** argv) {
const char* options[] = {argc > 0 ? argv[0] : "test", "--reporters=mtest"};
context.applyCommandLine(2, options);
bool missing = false;
if (const auto select = env("MTEST_SELECT"); !select.empty()) {
if (const auto& select = runner.select; !select.empty()) {
std::set<std::string> known;
for (const auto& t : doctest::detail::getRegisteredTests())
known.insert(id_of(t));
Expand All @@ -206,7 +290,8 @@ int mcpplibs_mtest_doctest_main(int argc, char** argv) {
const_cast<doctest::detail::TestCase&>(t).m_skip = true;
}
const int status = context.run();
const bool failed = status != 0 || missing || counts()["fail"] > 0;
const bool failed =
status != 0 || missing || counts()["fail"] > 0 || counts().contains("xpass");
report().write({{"e", "done"}, {"status", failed ? "fail" : "pass"}, {"counts", counts()}});
return failed ? 1 : 0;
}
28 changes: 28 additions & 0 deletions adapters/doctest/tests/handshake.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// The adapter under its own runner (P2.2): a case and a program it starts see none of the
// protocol's variables, which the runner stated to this program.
#include <cstdlib>
#include <doctest/doctest.h>

namespace {

// A command of the host's shell that fails when the program it starts has MTEST_PROTOCOL or
// MTEST_REPORT: `cmd` where the host has one (COMSPEC), `sh` elsewhere.
const char* command_seeing_protocol() {
return std::getenv("COMSPEC")
? "if defined MTEST_PROTOCOL (exit 1) else if defined MTEST_REPORT (exit 1)"
: "test -z \"${MTEST_PROTOCOL}${MTEST_REPORT}\"";
}

} // namespace

TEST_SUITE("handshake") {
TEST_CASE("a case sees no protocol variable") {
CHECK(std::getenv("MTEST_PROTOCOL") == nullptr);
CHECK(std::getenv("MTEST_MODE") == nullptr);
CHECK(std::getenv("MTEST_REPORT") == nullptr);
CHECK(std::getenv("MTEST_SELECT") == nullptr);
}
TEST_CASE("a program a case starts sees no protocol variable") {
CHECK(std::system(command_seeing_protocol()) == 0);
}
}
4 changes: 4 additions & 0 deletions adapters/gtest/docs/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ TEST(Parsing, Fields) {
- A case's identifier is `<suite>/<name>`: `Parsing/EmptyLine`.
- `mcpp test --case 'Parsing/*'` runs those cases; `mcpp test --cases` lists them; a failure is named
with its place.
- Under a runner the adapter reads the protocol's variables (`MTEST_PROTOCOL`, `MTEST_REPORT`, ...)
and removes them from the program's environment before any case runs (P2.2): a program a case
starts, by `std::system` or otherwise, is not taken for the runner's, and a case reads none of
them.

More: [mtest](https://github.com/mcpp-community/mtest), [GoogleTest](https://google.github.io/googletest/).
2 changes: 1 addition & 1 deletion adapters/gtest/mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[package]
namespace = "mcpplibs.mtest"
name = "gtest"
version = "0.11.3"
version = "0.11.4"
description = "GoogleTest speaking the mtest protocol: a test main that lists, selects and reports GoogleTest's cases"
license = "Apache-2.0"

Expand Down
Loading
Loading