From 7f59ccb21194ada2fd99850c2122f5bbba1da449 Mon Sep 17 00:00:00 2001 From: ZheFeng7110 <194236111+ZheFeng7110@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:37:17 +0000 Subject: [PATCH 1/2] doctest: distinct identifiers, xfail and xpass, timeout, the place of an exception (#29) Of two cases with one name the later is /#2. should_fail and expected_failures(n) are listed expect: fail and end xfail or xpass; may_fail stays pass. doctest::timeout is listed as the case's timeout. An exception or a crash fails at the case's place. The conformance suite holds an implementation below level 3 that expresses an expected failure to its xfail. 0.11.4. --- CHANGELOG.md | 14 +++++- README.md | 2 +- README.zh-CN.md | 2 +- adapters/doctest/docs/writing-tests.md | 20 ++++++-- adapters/doctest/fixture/main.cpp | 6 ++- adapters/doctest/mcpp.toml | 12 +++-- adapters/doctest/src/protocol.cpp | 69 +++++++++++++++++++++++--- adapters/gtest/mcpp.toml | 2 +- adapters/gtest/src/protocol.cpp | 2 +- capi/mcpp.toml | 2 +- capi/src/mtest.cpp | 2 +- conformance/mcpp.toml | 2 +- conformance/tests/protocol.cpp | 5 +- docs/adapters.md | 2 +- engine/mcpp.toml | 2 +- engine/src/commands.cpp | 2 +- engine/src/main.cpp | 4 +- engine/src/service.cpp | 2 +- lib/docs/writing-tests.md | 2 +- lib/mcpp.toml | 2 +- lib/src/mtest.cpp | 2 +- manifest/mcpp.toml | 2 +- 22 files changed, 122 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8311254..1e7a4cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,17 @@ # 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 + `/#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 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`). diff --git a/README.md b/README.md index 034c5b7..08db44b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README.zh-CN.md b/README.zh-CN.md index edd675a..5966054 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -37,7 +37,7 @@ int main() { ```toml [dev-dependencies] -mcpplibs.mtest = "0.11.3" +mcpplibs.mtest = "0.11.4" ``` 失败时给出位置与两侧的值(`expected 2, actual 3`)。在运行器之下,同一程序按协议报告;单独运行时每个用例输出一行。 diff --git a/adapters/doctest/docs/writing-tests.md b/adapters/doctest/docs/writing-tests.md index 321e1eb..8474442 100644 --- a/adapters/doctest/docs/writing-tests.md +++ b/adapters/doctest/docs/writing-tests.md @@ -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 @@ -20,6 +20,20 @@ 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 `/` (`parse/an empty line`), or `` 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 + `/#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. +- The package's `main` is linked into a test file whose text defines none. A file whose `main` comes + from a header (``) declares it, `int main(int, char*[]);`, so + that it is not linked twice. diff --git a/adapters/doctest/fixture/main.cpp b/adapters/doctest/fixture/main.cpp index 7c74be1..52f675b 100644 --- a/adapters/doctest/fixture/main.cpp +++ b/adapters/doctest/fixture/main.cpp @@ -1,5 +1,6 @@ // The conformance fixture of the doctest adapter (spec/conformance.md), level 1: the cases doctest -// can state, named `/` (`conformance/passes`), with no tags, kinds or metrics. +// can state, named `/` (`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 @@ -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") {} diff --git a/adapters/doctest/mcpp.toml b/adapters/doctest/mcpp.toml index e07b66f..e5b804b 100644 --- a/adapters/doctest/mcpp.toml +++ b/adapters/doctest/mcpp.toml @@ -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 `/` (P3.2), or `` 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 `/` (P3.2), or `` outside a `TEST_SUITE`, with `#` 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" diff --git a/adapters/doctest/src/protocol.cpp b/adapters/doctest/src/protocol.cpp index 930de7e..02b8b38 100644 --- a/adapters/doctest/src/protocol.cpp +++ b/adapters/doctest/src/protocol.cpp @@ -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 #include #include #include @@ -53,11 +55,55 @@ std::map& 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 `/` and each later +// one `/#`, n from 2. +std::map assign_identifiers() { + std::vector 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 ids; + std::set 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::steady_clock::now() - @@ -77,29 +123,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}}); @@ -137,6 +188,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_; }; @@ -162,7 +214,7 @@ 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"}}); @@ -206,7 +258,8 @@ int mcpplibs_mtest_doctest_main(int argc, char** argv) { const_cast(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; } diff --git a/adapters/gtest/mcpp.toml b/adapters/gtest/mcpp.toml index e8d2385..b848b58 100644 --- a/adapters/gtest/mcpp.toml +++ b/adapters/gtest/mcpp.toml @@ -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" diff --git a/adapters/gtest/src/protocol.cpp b/adapters/gtest/src/protocol.cpp index d79f5a3..7df059d 100644 --- a/adapters/gtest/src/protocol.cpp +++ b/adapters/gtest/src/protocol.cpp @@ -128,7 +128,7 @@ int mcpplibs_mtest_gtest_main(int argc, char** argv) { {"protocol", 1}, {"level", 1}, {"framework", "googletest"}, - {"version", "0.11.3"}, + {"version", "0.11.4"}, {"isolation", {"process", "case"}}, {"guide", "https://github.com/mcpp-community/mtest/blob/main/adapters/gtest/docs/" "writing-tests.md"}}); diff --git a/capi/mcpp.toml b/capi/mcpp.toml index c1c5caf..cf6718f 100644 --- a/capi/mcpp.toml +++ b/capi/mcpp.toml @@ -4,7 +4,7 @@ [package] namespace = "mcpplibs.mtest" name = "capi" -version = "0.11.3" +version = "0.11.4" description = "mtest's C ABI: the protocol's events for any language with a C FFI" license = "Apache-2.0" diff --git a/capi/src/mtest.cpp b/capi/src/mtest.cpp index 9d397a2..b305440 100644 --- a/capi/src/mtest.cpp +++ b/capi/src/mtest.cpp @@ -120,7 +120,7 @@ void mtest_init(const char* framework, int level) { {"protocol", 1}, {"level", std::clamp(level, 1, 3)}, {"framework", text(framework)}, - {"version", "0.11.3"}, + {"version", "0.11.4"}, {"isolation", {"process", "case"}}}); } diff --git a/conformance/mcpp.toml b/conformance/mcpp.toml index 9652d40..4a7d0ac 100644 --- a/conformance/mcpp.toml +++ b/conformance/mcpp.toml @@ -8,7 +8,7 @@ [package] namespace = "mcpplibs.mtest" name = "conformance" -version = "0.11.3" +version = "0.11.4" description = "The conformance suite of the mtest protocol, for every implementation of its program side" license = "Apache-2.0" diff --git a/conformance/tests/protocol.cpp b/conformance/tests/protocol.cpp index fd76f2d..a843249 100644 --- a/conformance/tests/protocol.cpp +++ b/conformance/tests/protocol.cpp @@ -279,10 +279,11 @@ int main() { {id("measures"), "pass"}, {id("covers"), "pass"}, {id("holds"), "pass"}}; - if (level(r) >= 3) { + // An implementation below level 3 that expresses an expected failure is held to it. + if (level(r) >= 3 || find(r, "end", id("expected to fail"))) want[id("expected to fail")] = "xfail"; + if (level(r) >= 3) want[id("bounded")] = "fail"; - } for (const auto& [case_id, status] : want) { auto e = find(r, "end", case_id); expect(e.has_value()) << "no end for " << case_id; diff --git a/docs/adapters.md b/docs/adapters.md index 5edd8a6..86909d5 100644 --- a/docs/adapters.md +++ b/docs/adapters.md @@ -8,7 +8,7 @@ conformance suite (`MTEST_CTS_PROGRAM`, a command: a program and its arguments, |---|---|---| | `mcpplibs.mtest` (C++) | 3 | `import mcpplibs.mtest;` — the library itself | | GoogleTest | 1 | `mcpplibs.mtest.gtest` as a dev-dependency, whose entry begins a test file (mcpp 2026.11.8.1+; earlier: `[test] main = { from = "mcpplibs.mtest.gtest/main" }` and `compat.gtest` as a dev-dependency too); identifiers are `/` | -| doctest | 1 | `mcpplibs.mtest.doctest` as a dev-dependency: doctest's implementation and the entry that begins a test file, compiled once; a test file includes `` and defines no `DOCTEST_CONFIG_IMPLEMENT*` (0.11.0); identifiers are `/`, or `` outside a suite | +| doctest | 1 | `mcpplibs.mtest.doctest` as a dev-dependency: doctest's implementation and the entry that begins a test file, compiled once; a test file includes `` and defines no `DOCTEST_CONFIG_IMPLEMENT*` (0.11.0); identifiers are `/`, or `` outside a suite, `#2` and on for a later case of a name taken; `should_fail` and `expected_failures` end `xfail` or `xpass`, `timeout` is stated (0.11.4) | | Bash | 3 | `. mtest.sh`, then `mtest_case ID FUNCTION [--tag T ...]` and `mtest_report` | | Python | 3 | `import mtest_report as mt`, `@mt.case("id")`, `mt.report()` | | C ABI | 3 | `include/mtest.h` and `libmtest`, for any language with a C FFI | diff --git a/engine/mcpp.toml b/engine/mcpp.toml index 83b5dfd..a1e5ff0 100644 --- a/engine/mcpp.toml +++ b/engine/mcpp.toml @@ -10,7 +10,7 @@ [package] namespace = "mtest" name = "mtest" -version = "0.11.3" +version = "0.11.4" description = "The mtest engine: run, schedule, store, explain and analyse tests of any language" license = "Apache-2.0" authors = ["mcpp-community"] diff --git a/engine/src/commands.cpp b/engine/src/commands.cpp index ab1b795..d96e527 100644 --- a/engine/src/commands.cpp +++ b/engine/src/commands.cpp @@ -2011,7 +2011,7 @@ json catalog() { plain({{"out", {{"type", "string"}}}, {"argv", {{"type", "array"}, {"items", {{"type", "string"}}}}}}), true); - return {{"schema", "mtest.catalog/1"}, {"version", "0.11.3"}, {"tools", tools}}; + return {{"schema", "mtest.catalog/1"}, {"version", "0.11.4"}, {"tools", tools}}; } } // namespace mcpplibs::mtest::engine diff --git a/engine/src/main.cpp b/engine/src/main.cpp index d14522f..42a0bae 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -98,7 +98,7 @@ const json* tool_named(const json& tools, std::string_view name) { cmdline::App application(const json& tools) { auto app = cmdline::App("mtest") - .version("0.11.3") + .version("0.11.4") .description("run tests of any language, and ask about their runs") .option(cmdline::Option("json").global().help("print the answer's data")); for (const auto& t : tools) @@ -253,7 +253,7 @@ int main(int argc, char** argv) { const std::string name(parsed->subcommand_name()); const auto& sub = parsed->subcommand()->get(); if (name == "version") { - std::println("mtest 0.11.3"); + std::println("mtest 0.11.4"); return 0; } const auto cwd = std::filesystem::current_path(); diff --git a/engine/src/service.cpp b/engine/src/service.cpp index 950bb5e..cb2bb30 100644 --- a/engine/src/service.cpp +++ b/engine/src/service.cpp @@ -56,7 +56,7 @@ int serve(const std::filesystem::path& cwd) { {"result", {{"protocolVersion", params.value("protocolVersion", std::string("2025-06-18"))}, {"capabilities", {{"tools", {{"listChanged", false}}}}}, - {"serverInfo", {{"name", "mtest"}, {"version", "0.11.3"}}}, + {"serverInfo", {{"name", "mtest"}, {"version", "0.11.4"}}}, {"instructions", "Run tests and ask about runs: run, plan, explain, history, analyze, graph, " "gate, ci. A test session (test/plan, test/run, test/add, test/cancel, " diff --git a/lib/docs/writing-tests.md b/lib/docs/writing-tests.md index 2447082..94b3012 100644 --- a/lib/docs/writing-tests.md +++ b/lib/docs/writing-tests.md @@ -53,7 +53,7 @@ E2E cli{"cli", resource{"serial:home"}, timeout{std::chrono::minutes{2}}, [] { / The entry enters the child before any case; the copy runs no case and reports nothing. The files of one directory are then one program (mcpp `[test] programs = "directory"`) however many of them start copies. -- The manifest names the library once: `[dev-dependencies] mcpplibs.mtest = "0.11.3"`. +- The manifest names the library once: `[dev-dependencies] mcpplibs.mtest = "0.11.4"`. - Identifiers are stable: a selection, a history and a shard name a case by them. ## Expectations diff --git a/lib/mcpp.toml b/lib/mcpp.toml index 3711731..0f5d05c 100644 --- a/lib/mcpp.toml +++ b/lib/mcpp.toml @@ -17,7 +17,7 @@ [package] namespace = "mcpplibs" name = "mtest" -version = "0.11.3" +version = "0.11.4" description = "The C++ test library of mtest: cases, expectations, fixtures, bench, conformance and end-to-end helpers, with no macro and no header, speaking the mtest protocol" license = "Apache-2.0" authors = ["mcpp-community"] diff --git a/lib/src/mtest.cpp b/lib/src/mtest.cpp index 5b9562b..accd46d 100644 --- a/lib/src/mtest.cpp +++ b/lib/src/mtest.cpp @@ -345,7 +345,7 @@ void write_program() { {"protocol", 1}, {"level", 3}, {"framework", "mcpplibs.mtest"}, - {"version", "0.11.3"}, + {"version", "0.11.4"}, {"isolation", {"process", "case"}}, // P4.3: a selection's `!` and `?` lines are understood. {"selection", {"except", "optional"}}, diff --git a/manifest/mcpp.toml b/manifest/mcpp.toml index a3db01b..f1d1156 100644 --- a/manifest/mcpp.toml +++ b/manifest/mcpp.toml @@ -8,7 +8,7 @@ [package] namespace = "mcpplibs.mtest" name = "manifest" -version = "0.11.3" +version = "0.11.4" description = "The static case manifest of mtest: the cases a source file declares, read without building it" license = "Apache-2.0" authors = ["mcpp-community"] From 762e53535402aaaf0ea2b6b528533261718711e7 Mon Sep 17 00:00:00 2001 From: ZheFeng7110 <194236111+ZheFeng7110@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:47:35 +0000 Subject: [PATCH 2/2] doctest, gtest: the protocol's variables stay out of what a case starts (P2.2, #29) The adapters read the handshake once, then remove its variables from the C library's environment before any case runs, so a test program a case starts does not append to its parent's report. mcpplibs.os states no change to a program's own environment (mcpplibs/os#29), so the adapters call the host's C library. The doctest adapter's own test checks a case and a program it starts under mcpp test; CI runs it. --- .github/workflows/ci.yml | 1 + CHANGELOG.md | 7 +++++ adapters/doctest/docs/writing-tests.md | 4 +++ adapters/doctest/src/protocol.cpp | 40 +++++++++++++++++++++++--- adapters/doctest/tests/handshake.cpp | 28 ++++++++++++++++++ adapters/gtest/docs/writing-tests.md | 4 +++ adapters/gtest/src/protocol.cpp | 40 +++++++++++++++++++++++--- docs/adapters.md | 4 +-- 8 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 adapters/doctest/tests/handshake.cpp diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77805e7..121a289 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e7a4cf..e14e535 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,13 @@ `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 diff --git a/adapters/doctest/docs/writing-tests.md b/adapters/doctest/docs/writing-tests.md index 8474442..48383c8 100644 --- a/adapters/doctest/docs/writing-tests.md +++ b/adapters/doctest/docs/writing-tests.md @@ -34,6 +34,10 @@ TEST_SUITE("parse") { - 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 (``) declares it, `int main(int, char*[]);`, so that it is not linked twice. diff --git a/adapters/doctest/src/protocol.cpp b/adapters/doctest/src/protocol.cpp index 02b8b38..82867d9 100644 --- a/adapters/doctest/src/protocol.cpp +++ b/adapters/doctest/src/protocol.cpp @@ -30,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; @@ -203,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; @@ -219,7 +251,7 @@ int mcpplibs_mtest_doctest_main(int argc, char** argv) { {"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 all; for (const auto& t : doctest::detail::getRegisteredTests()) all.push_back(&t); @@ -234,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 known; for (const auto& t : doctest::detail::getRegisteredTests()) known.insert(id_of(t)); diff --git a/adapters/doctest/tests/handshake.cpp b/adapters/doctest/tests/handshake.cpp new file mode 100644 index 0000000..96ca93f --- /dev/null +++ b/adapters/doctest/tests/handshake.cpp @@ -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 +#include + +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); + } +} diff --git a/adapters/gtest/docs/writing-tests.md b/adapters/gtest/docs/writing-tests.md index 8551785..61f5965 100644 --- a/adapters/gtest/docs/writing-tests.md +++ b/adapters/gtest/docs/writing-tests.md @@ -22,5 +22,9 @@ TEST(Parsing, Fields) { - A case's identifier is `/`: `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/). diff --git a/adapters/gtest/src/protocol.cpp b/adapters/gtest/src/protocol.cpp index 7df059d..b469046 100644 --- a/adapters/gtest/src/protocol.cpp +++ b/adapters/gtest/src/protocol.cpp @@ -28,6 +28,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; +} + class Report { public: explicit Report(const std::string& path) : out_(path, std::ios::app | std::ios::binary) {} @@ -118,8 +149,9 @@ extern "C++" int mcpplibs_mtest_gtest_main(int argc, char** argv); int mcpplibs_mtest_gtest_main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); - const std::string report_path = env("MTEST_REPORT"); - if (env("MTEST_PROTOCOL").empty() || report_path.empty()) + const auto runner = take_handshake(); + const std::string& report_path = runner.report; + if (runner.protocol.empty() || report_path.empty()) return RUN_ALL_TESTS(); const char* volatile keep = mcpplibs_mtest_gtest_marker; (void)keep; @@ -133,7 +165,7 @@ int mcpplibs_mtest_gtest_main(int argc, char** argv) { {"guide", "https://github.com/mcpp-community/mtest/blob/main/adapters/gtest/docs/" "writing-tests.md"}}); const auto tests = all_tests(); - if (env("MTEST_MODE") == "list") { + if (runner.mode == "list") { for (const auto* t : tests) { event e = {{"e", "case"}, {"id", id_of(*t)}, {"name", t->name()}, {"file", t->file() ? t->file() : ""}, @@ -146,7 +178,7 @@ int mcpplibs_mtest_gtest_main(int argc, char** argv) { return 0; } bool missing = false; - if (const auto select = env("MTEST_SELECT"); !select.empty()) { + if (const auto& select = runner.select; !select.empty()) { std::set known; for (const auto* t : tests) known.insert(id_of(*t)); diff --git a/docs/adapters.md b/docs/adapters.md index 86909d5..1ac1053 100644 --- a/docs/adapters.md +++ b/docs/adapters.md @@ -7,8 +7,8 @@ conformance suite (`MTEST_CTS_PROGRAM`, a command: a program and its arguments, | Adapter | Level | Use | |---|---|---| | `mcpplibs.mtest` (C++) | 3 | `import mcpplibs.mtest;` — the library itself | -| GoogleTest | 1 | `mcpplibs.mtest.gtest` as a dev-dependency, whose entry begins a test file (mcpp 2026.11.8.1+; earlier: `[test] main = { from = "mcpplibs.mtest.gtest/main" }` and `compat.gtest` as a dev-dependency too); identifiers are `/` | -| doctest | 1 | `mcpplibs.mtest.doctest` as a dev-dependency: doctest's implementation and the entry that begins a test file, compiled once; a test file includes `` and defines no `DOCTEST_CONFIG_IMPLEMENT*` (0.11.0); identifiers are `/`, or `` outside a suite, `#2` and on for a later case of a name taken; `should_fail` and `expected_failures` end `xfail` or `xpass`, `timeout` is stated (0.11.4) | +| GoogleTest | 1 | `mcpplibs.mtest.gtest` as a dev-dependency, whose entry begins a test file (mcpp 2026.11.8.1+; earlier: `[test] main = { from = "mcpplibs.mtest.gtest/main" }` and `compat.gtest` as a dev-dependency too); identifiers are `/`; the protocol's variables are removed before a case runs (0.11.4) | +| doctest | 1 | `mcpplibs.mtest.doctest` as a dev-dependency: doctest's implementation and the entry that begins a test file, compiled once; a test file includes `` and defines no `DOCTEST_CONFIG_IMPLEMENT*` (0.11.0); identifiers are `/`, or `` outside a suite, `#2` and on for a later case of a name taken; `should_fail` and `expected_failures` end `xfail` or `xpass`, `timeout` is stated; the protocol's variables are removed before a case runs (0.11.4) | | Bash | 3 | `. mtest.sh`, then `mtest_case ID FUNCTION [--tag T ...]` and `mtest_report` | | Python | 3 | `import mtest_report as mt`, `@mt.case("id")`, `mt.report()` | | C ABI | 3 | `include/mtest.h` and `libmtest`, for any language with a C FFI |