#include "fesa/core/diagnostic.hpp"

#include <gtest/gtest.h>

#include <filesystem>
#include <string>
#include <utility>
#include <vector>

namespace {

fesa::Diagnostic makeDiagnostic(
    std::string file,
    std::size_t line,
    std::string keyword,
    std::string entityIdentity,
    std::string code,
    std::string message) {
    return fesa::Diagnostic{
        fesa::Severity::error,
        std::move(code),
        {std::filesystem::path{std::move(file)}, line},
        std::move(keyword),
        std::move(entityIdentity),
        std::move(message)};
}

} // namespace

TEST(CoreDiagnostics, DiagnosticsSortDeterministically) {
    std::vector<fesa::Diagnostic> diagnostics{
        makeDiagnostic("b.inp", 1U, "*NODE", "I.1", "a", "file-b"),
        makeDiagnostic("a.inp", 3U, "*NODE", "I.1", "z", "code-z"),
        makeDiagnostic("a.inp", 3U, "*NODE", "I.1", "a", "first-equal"),
        makeDiagnostic("a.inp", 3U, "*NODE", "I.1", "a", "second-equal"),
        makeDiagnostic("a.inp", 3U, "*NODE", "I.2", "a", "entity-2"),
        makeDiagnostic("a.inp", 3U, "*BOUNDARY", "I.1", "a", "keyword"),
        makeDiagnostic("a.inp", 2U, "*NODE", "I.1", "a", "line")};

    fesa::sortDiagnostics(diagnostics);

    ASSERT_EQ(diagnostics.size(), 7U);
    EXPECT_EQ(diagnostics[0].message, "line");
    EXPECT_EQ(diagnostics[1].message, "keyword");
    EXPECT_EQ(diagnostics[2].message, "first-equal");
    EXPECT_EQ(diagnostics[3].message, "second-equal");
    EXPECT_EQ(diagnostics[4].message, "code-z");
    EXPECT_EQ(diagnostics[5].message, "entity-2");
    EXPECT_EQ(diagnostics[6].message, "file-b");

    const fesa::Diagnostic& exact = diagnostics[2];
    EXPECT_EQ(exact.severity, fesa::Severity::error);
    EXPECT_EQ(exact.code, "a");
    EXPECT_EQ(exact.location.file, std::filesystem::path{"a.inp"});
    EXPECT_EQ(exact.location.line, 3U);
    EXPECT_EQ(exact.keyword, "*NODE");
    EXPECT_EQ(exact.entityIdentity, "I.1");
    EXPECT_EQ(exact.message, "first-equal");
}
