From df819d120610b7d3737f8b8e2ad03b7e88fb924c Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sat, 3 Aug 2024 19:25:12 -0400 Subject: Add error kind and error message checks to empty tests Add conditional logging for valid path in errors Remove error ending newlines --- src/openvic-dataloader/DiagnosticLogger.hpp | 7 ++++++- tests/src/csv/Parser.cpp | 30 +++++++++++++++++++++++++++- tests/src/v2script/Parser.cpp | 31 ++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/openvic-dataloader/DiagnosticLogger.hpp b/src/openvic-dataloader/DiagnosticLogger.hpp index 8c491ca..479a7a0 100644 --- a/src/openvic-dataloader/DiagnosticLogger.hpp +++ b/src/openvic-dataloader/DiagnosticLogger.hpp @@ -398,8 +398,13 @@ namespace ovdl { [&](auto out, lexy::visualization_options) { return lexy::_detail::write_str(out, fmt::format(fmt, std::forward(args)...).c_str()); }); - impl.write_path(iter, file().path()); + if constexpr (!std::same_as) { + if (file().path() != nullptr && file().path()[0] != '\0') { + impl.write_path(iter, file().path()); + } + } + output.pop_back(); auto message = intern(output); error->_set_message(message); if (!error->is_linked_in_tree()) diff --git a/tests/src/csv/Parser.cpp b/tests/src/csv/Parser.cpp index 88a1474..e800f3c 100644 --- a/tests/src/csv/Parser.cpp +++ b/tests/src/csv/Parser.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -161,19 +162,46 @@ TEST_CASE("CSV File (HasCstr) Handle String Parse", "[csv-file-parse][handle-str } TEST_CASE("CSV File (const char*) Handle Empty Path String Parse", "[csv-file-parse][handle-string][char-ptr][empty-path]") { + static constexpr auto error_fmt = +#ifdef __APPLE__ + "error: OS file error for '{}'."; +#elif defined(_WIN32) + "error: OS file error for '{}'."; +#else + "error: File '{}' not found."; +#endif + std::error_code fs_err; + const auto fs_path = std::filesystem::weakly_canonical("", fs_err); + Parser parser(ovdl::detail::cnull); parser.load_from_file(""); CHECK_OR_RETURN(!parser.get_errors().empty()); + + auto error = parser.get_errors().front(); + CHECK_OR_RETURN(error != nullptr); + + CHECK_OR_RETURN(error->kind() == ovdl::error::ErrorKind::BufferError); + CHECK_OR_RETURN(parser.error(error) == fmt::format(error_fmt, fs_path.string())); } TEST_CASE("CSV File (const char*) Handle Non-existent Path String Parse", "[csv-file-parse][handle-string][char-ptr][nonexistent-path]") { + static constexpr auto path = "./Idontexist"; + std::error_code fs_err; + const auto fs_path = std::filesystem::weakly_canonical(path, fs_err); + Parser parser(ovdl::detail::cnull); - parser.load_from_file("./Idontexist"); + parser.load_from_file(path); CHECK_OR_RETURN(!parser.get_errors().empty()); + + auto error = parser.get_errors().front(); + CHECK_OR_RETURN(error != nullptr); + + CHECK_OR_RETURN(error->kind() == ovdl::error::ErrorKind::BufferError); + CHECK_OR_RETURN(parser.error(error) == fmt::format("error: File '{}' not found.", fs_path.string())); } TEST_CASE("CSV Parse", "[csv-parse]") { diff --git a/tests/src/v2script/Parser.cpp b/tests/src/v2script/Parser.cpp index 856e5fb..45d9f44 100644 --- a/tests/src/v2script/Parser.cpp +++ b/tests/src/v2script/Parser.cpp @@ -7,6 +7,8 @@ #include +#include + #include "Helper.hpp" #include #include @@ -99,19 +101,46 @@ TEST_CASE("V2Script File (HasCstr) Simple Parse", "[v2script-file-simple-parse][ } TEST_CASE("V2Script File (const char*) Handle Empty Path String Parse", "[v2script-file-parse][handle-string][char-ptr][empty-path]") { + static constexpr auto error_fmt = +#ifdef __APPLE__ + "error: OS file error for '{}'."; +#elif defined(_WIN32) + "error: OS file error for '{}'."; +#else + "error: File '{}' not found."; +#endif + std::error_code fs_err; + const auto fs_path = std::filesystem::weakly_canonical("", fs_err); + Parser parser(ovdl::detail::cnull); parser.load_from_file(""); CHECK_OR_RETURN(!parser.get_errors().empty()); + + auto error = parser.get_errors().front(); + CHECK_OR_RETURN(error != nullptr); + + CHECK_OR_RETURN(error->kind() == ovdl::error::ErrorKind::BufferError); + CHECK_OR_RETURN(parser.error(error) == fmt::format(error_fmt, fs_path.string())); } TEST_CASE("V2Script File (const char*) Handle Non-existent Path String Parse", "[v2script-file-parse][handle-string][char-ptr][nonexistent-path]") { + static constexpr auto path = "./Idontexist"; + std::error_code fs_err; + const auto fs_path = std::filesystem::weakly_canonical(path, fs_err); + Parser parser(ovdl::detail::cnull); - parser.load_from_file("./Idontexist"); + parser.load_from_file(path); CHECK_OR_RETURN(!parser.get_errors().empty()); + + auto error = parser.get_errors().front(); + CHECK_OR_RETURN(error != nullptr); + + CHECK_OR_RETURN(error->kind() == ovdl::error::ErrorKind::BufferError); + CHECK_OR_RETURN(parser.error(error) == fmt::format("error: File '{}' not found.", fs_path.string())); } TEST_CASE("V2Script Identifier Simple Parse", "[v2script-id-simple-parse]") { -- cgit v1.2.3-56-ga3b1