[M1] add util::Error, Result<T>, format()
This commit is contained in:
69
tests/util_error.cpp
Normal file
69
tests/util_error.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
import cargoxx.util;
|
||||
import std;
|
||||
|
||||
using cargoxx::util::Error;
|
||||
using cargoxx::util::ErrorCode;
|
||||
using cargoxx::util::format;
|
||||
|
||||
TEST_CASE("format renders code, message, and trailing newline", "[util][format]") {
|
||||
Error e{ErrorCode::ManifestNotFound, "manifest not found", "", std::nullopt, std::nullopt};
|
||||
REQUIRE(format(e) == "error[E0001]: manifest not found\n");
|
||||
}
|
||||
|
||||
TEST_CASE("format pads error codes to four digits", "[util][format]") {
|
||||
Error e{ErrorCode::Internal, "boom", "", std::nullopt, std::nullopt};
|
||||
REQUIRE(format(e) == "error[E0100]: boom\n");
|
||||
}
|
||||
|
||||
TEST_CASE("format includes location with line:col when present", "[util][format]") {
|
||||
Error e{
|
||||
ErrorCode::ManifestParseError,
|
||||
"syntax error",
|
||||
"",
|
||||
std::filesystem::path{"Cargoxx.toml"},
|
||||
std::pair{7, 1},
|
||||
};
|
||||
REQUIRE(format(e) == "error[E0002]: syntax error\n"
|
||||
" --> Cargoxx.toml:7:1\n");
|
||||
}
|
||||
|
||||
TEST_CASE("format includes location without line:col when only path is set", "[util][format]") {
|
||||
Error e{
|
||||
ErrorCode::LayoutNoTarget,
|
||||
"no target found",
|
||||
"",
|
||||
std::filesystem::path{"./"},
|
||||
std::nullopt,
|
||||
};
|
||||
REQUIRE(format(e) == "error[E0020]: no target found\n"
|
||||
" --> ./\n");
|
||||
}
|
||||
|
||||
TEST_CASE("format appends a hint line when hint is non-empty", "[util][format]") {
|
||||
Error e{
|
||||
ErrorCode::LayoutNoTarget,
|
||||
"no target found",
|
||||
"run `cargoxx new --lib <name>` to create a library project",
|
||||
std::nullopt,
|
||||
std::nullopt,
|
||||
};
|
||||
REQUIRE(format(e) ==
|
||||
"error[E0020]: no target found\n"
|
||||
" hint: run `cargoxx new --lib <name>` to create a library project\n");
|
||||
}
|
||||
|
||||
TEST_CASE("format combines location and hint", "[util][format]") {
|
||||
Error e{
|
||||
ErrorCode::LinkdbUnknownPackage,
|
||||
"package not in link database",
|
||||
"file an issue or supply a manual recipe",
|
||||
std::filesystem::path{"Cargoxx.toml"},
|
||||
std::pair{7, 1},
|
||||
};
|
||||
REQUIRE(format(e) ==
|
||||
"error[E0060]: package not in link database\n"
|
||||
" --> Cargoxx.toml:7:1\n"
|
||||
" hint: file an issue or supply a manual recipe\n");
|
||||
}
|
||||
Reference in New Issue
Block a user