BareGit
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>

#include <gtest/gtest.h>

#include "macrodown.h"

using namespace macrodown;

namespace
{

const std::filesystem::path SUITE_PATH =
    std::filesystem::path(MACRODOWN_SOURCE_DIR) / "tests" / "md-html-suite";

std::string readFile(const std::filesystem::path& path)
{
    std::ifstream stream(path, std::ios::binary);
    if(!stream)
    {
        return {};
    }
    return {std::istreambuf_iterator<char>(stream),
            std::istreambuf_iterator<char>()};
}

std::vector<std::filesystem::path> suiteFiles()
{
    std::vector<std::filesystem::path> paths;
    for(const auto& entry : std::filesystem::directory_iterator(SUITE_PATH))
    {
        if(entry.is_regular_file() && entry.path().extension() == ".md")
        {
            paths.push_back(entry.path());
        }
    }
    std::ranges::sort(paths);
    return paths;
}

std::string testName(
    const testing::TestParamInfo<std::filesystem::path>& information)
{
    std::string name = information.param.stem().string();
    for(char& character : name)
    {
        if(!std::isalnum(static_cast<unsigned char>(character)))
        {
            character = '_';
        }
    }
    return name;
}

class MdHtmlSuiteTest :
    public testing::TestWithParam<std::filesystem::path>
{};

TEST_P(MdHtmlSuiteTest, RendersExpectedHtml)
{
    const auto markdown_path = GetParam();
    auto expected_path = markdown_path;
    expected_path.replace_extension(".out");

    ASSERT_TRUE(std::filesystem::exists(expected_path));

    MacroDown macrodown;
    const std::string markdown = readFile(markdown_path);
    const std::string expected = readFile(expected_path);
    const std::string actual = macrodown.render(*macrodown.parse(markdown));

    EXPECT_EQ(actual, expected);
}

INSTANTIATE_TEST_SUITE_P(MdHtmlSuite, MdHtmlSuiteTest,
                         testing::ValuesIn(suiteFiles()), testName);

} // namespace