Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error kind and error message checks to empty tests #55

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/openvic-dataloader/DiagnosticLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,13 @@ namespace ovdl {
[&](auto out, lexy::visualization_options) {
return lexy::_detail::write_str(out, fmt::format(fmt, std::forward<Args>(args)...).c_str());
});
impl.write_path(iter, file().path());
if constexpr (!std::same_as<T, error::BufferError>) {
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())
Expand Down
30 changes: 29 additions & 1 deletion tests/src/csv/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <fstream>
#include <string_view>

#include <openvic-dataloader/Error.hpp>
#include <openvic-dataloader/csv/LineObject.hpp>
#include <openvic-dataloader/csv/Parser.hpp>

Expand Down Expand Up @@ -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]") {
Expand Down
31 changes: 30 additions & 1 deletion tests/src/v2script/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <dryad/node.hpp>

#include <fmt/core.h>

#include "Helper.hpp"
#include <detail/NullBuff.hpp>
#include <range/v3/iterator/operations.hpp>
Expand Down Expand Up @@ -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]") {
Expand Down