Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

develroot / tools / Tools.tests.cpp

4.8 KiB · Raw · Log

// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <Progmasoft/Catch3/Matchers/String.hpp>
#include <Progmasoft/Catch3/TestMacros.hpp>
#include <limits>
#include <string>

#include "tools/Amalgamation.hpp"
#include "tools/Repository.hpp"
#include "tools/RepositoryReport.hpp"
#include "tools/Versions.hpp"

using namespace Progmasoft::Catch3::Tools;
using Catch::Matchers::ContainsSubstring;

TEST_CASE("Versions are MAJOR.MINOR.PATCH", "[tools]")
{
    CHECK(ParseVersion("0.4.0") == Version{ 0, 4, 0 });
    CHECK(ParseVersion("10.20.30") == Version{ 10, 20, 30 });
    CHECK(FormatVersion(Version{ 0, 4, 0 }) == "0.4.0");
    for (const char *wrong :
         { "0.3", "0.3.0.2", "01.3.0", "0.3.0.", "a.3.0", "", "0.3.0-rc" })
    {
        INFO(wrong);
        CHECK_FALSE(ParseVersion(wrong));
    }
}

TEST_CASE("The next patch release follows a SemVer or a four-number version",
          "[tools]")
{
    const auto next = [](const char *version) {
        const auto parsed = NextPatch(version);
        return parsed ? FormatVersion(*parsed) : std::string{ "none" };
    };
    CHECK(next("0.4.0") == "0.4.1");
    CHECK(next("1.2.9") == "1.2.10");
    // After the four-number versions: the fourth number goes.
    CHECK(next("0.3.0.2") == "0.3.1");
    CHECK(next("0.3") == "none");
    const auto largest = std::to_string(std::numeric_limits<unsigned>::max());
    CHECK(next(("0.4." + largest).c_str()) == "none");
    CHECK(next(("0.3." + largest + ".2").c_str()) == "none");
    CHECK(next("0.3.0.2.1") == "none");
}

TEST_CASE("A version site is read and written in place", "[tools]")
{
    const VersionSite site{ "file", "version: '", "'" };
    const std::string text = "name\n  version: '1.2.3',\n  version: '9'\n";
    CHECK(ReadVersionSite(text, site) == "1.2.3");
    CHECK(WriteVersionSite(text, site, "1.2.3.4")
          == "name\n  version: '1.2.3.4',\n  version: '9'\n");
    CHECK_FALSE(ReadVersionSite("name", site));
    CHECK_FALSE(WriteVersionSite("name", site, "1"));
}

TEST_CASE("Release notes are found by their heading", "[tools]")
{
    const std::string notes = "# Release notes\n\n## 0.2\n\nNew.\n\n"
                              "### Part\n\n* One.\n\n\n## 0.1\n\nOld.\n";
    CHECK(ReleaseNotesOf(notes, "0.2") == "New.\n\n### Part\n\n* One.\n");
    CHECK(ReleaseNotesOf(notes, "0.1") == "Old.\n");
    CHECK_FALSE(ReleaseNotesOf(notes, "0.3"));
}

TEST_CASE("A release is found by the version it was published as", "[tools]")
{
    const std::string notes
        = "# Notes\n\n## 0.4.0\n\nThree.\n\n\n## 0.3.0.2\n\nFour.\n";
    CHECK(ReleaseNotesOf(notes, "0.4.0") == "Three.\n");
    CHECK(ReleaseNotesOf(notes, "0.3.0.2") == "Four.\n");
    CHECK_FALSE(ReleaseNotesOf(notes, "0.4.0.0"));
    CHECK_FALSE(ReleaseNotesOf(notes, "0.3.0"));
}

TEST_CASE("Git that could not say is not a clean checkout", "[tools]")
{
    RepositoryReport report;
    report.GitError = "unreadable";
    CHECK_THAT(ReportToJson(report), ContainsSubstring("\"dirty\": null"));
    CHECK_THAT(FormatReport(report),
               ContainsSubstring("Tracked changes: unknown"));
    report.Dirty = false;
    CHECK_THAT(ReportToJson(report), ContainsSubstring("\"dirty\": false"));
}

TEST_CASE("Includes are read as they are written", "[tools]")
{
    const auto angled = ParseInclude("  #include <a/b.hpp> // why\n");
    REQUIRE(angled);
    CHECK(angled->Name == "a/b.hpp");
    CHECK(angled->Angled);
    const auto quoted = ParseInclude("#include \"b.hpp\"\n");
    REQUIRE(quoted);
    CHECK_FALSE(quoted->Angled);
    CHECK_FALSE(ParseInclude("#  include <a.hpp>\n"));
    CHECK_FALSE(ParseInclude("// #include <a.hpp>\n"));
    CHECK_FALSE(ParseInclude("#include MACRO\n"));
}

TEST_CASE("The engine version comes from its declaration", "[tools]")
{
    CHECK(ParseEngineVersion("  static Version version( 3, 16, 1, \"\", 0 );")
          == "3.16.1");
    CHECK(ParseEngineVersion("static Version version( 3, 17, 0, \"rc\", 2 );")
          == "3.17.0-rc.2");
    CHECK_FALSE(ParseEngineVersion("nothing here"));
}

TEST_CASE("A report becomes JSON", "[tools]")
{
    RepositoryReport report;
    report.Root = "C:\\repos\\catch3";
    report.Os = "windows";
    report.Branch = "devel";
    report.Dirty = true;
    report.Tools.push_back({ "git", "/usr/bin/git", true });
    report.Tools.push_back({ "ninja", "", false });
    CHECK(ReportToJson(report) == R"({
  "root": "C:\\repos\\catch3",
  "os": "windows",
  "branch": "devel",
  "dirty": true,
  "tools": [
    {
      "name": "git",
      "path": "/usr/bin/git",
      "available": true
    },
    {
      "name": "ninja",
      "available": false
    }
  ]
}
)");
}

TEST_CASE("Text helpers keep line ends", "[tools]")
{
    CHECK(SplitLines("a\nb") == std::vector<std::string>{ "a\n", "b" });
    CHECK(SplitLines("").empty());
    CHECK(Trimmed("  a b\r\n") == "a b");
    CHECK(Trimmed(" \n").empty());
}