diff options
author | George L. Albany <Megacake1234@gmail.com> | 2024-06-22 21:58:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-22 21:58:35 +0200 |
commit | deed8ec0ae23651529a58125012c1b4aab015d02 (patch) | |
tree | 51ca6d5948e92be37b9ee6674cb96801d2cd03f8 /src | |
parent | 8b623bf4087aa360842ad31145d4ab6946cee9aa (diff) | |
parent | 1a694a8b26a441b12547057d6e0be61a111cced3 (diff) |
Merge pull request #49 from OpenVicProject/add/unit-testing
Add unit testing
Diffstat (limited to 'src')
-rw-r--r-- | src/openvic-dataloader/csv/Parser.cpp | 20 | ||||
-rw-r--r-- | src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp | 10 | ||||
-rw-r--r-- | src/openvic-dataloader/v2script/Parser.cpp | 9 |
3 files changed, 30 insertions, 9 deletions
diff --git a/src/openvic-dataloader/csv/Parser.cpp b/src/openvic-dataloader/csv/Parser.cpp index 5dbee32..8fe8b17 100644 --- a/src/openvic-dataloader/csv/Parser.cpp +++ b/src/openvic-dataloader/csv/Parser.cpp @@ -3,6 +3,7 @@ #include <type_traits> #include <vector> +#include <openvic-dataloader/Error.hpp> #include <openvic-dataloader/NodeLocation.hpp> #include <openvic-dataloader/csv/LineObject.hpp> #include <openvic-dataloader/csv/Parser.hpp> @@ -15,6 +16,8 @@ #include <lexy/input/buffer.hpp> #include <lexy/input/file.hpp> +#include <dryad/node.hpp> + #include "CsvGrammar.hpp" #include "CsvParseState.hpp" #include "detail/NullBuff.hpp" @@ -53,6 +56,13 @@ struct Parser::ParseHandler final : detail::BasicFileParseHandler<CsvParseState> return _lines; } + Parser::error_range get_errors() { + using iterator = typename decltype(std::declval<const error::Root*>()->children())::iterator; + if (!is_valid()) + return dryad::make_node_range<error::Error>(iterator::from_ptr(nullptr), iterator::from_ptr(nullptr)); + return parse_state().logger().get_errors(); + } + private: std::vector<csv::LineObject> _lines; }; @@ -126,19 +136,19 @@ constexpr void Parser::_run_load_func(detail::LoadCallback<ParseHandler, Args... } } -constexpr Parser& Parser::load_from_buffer(const char* data, std::size_t size, std::optional<detail::Encoding> encoding_fallback) { +Parser& Parser::load_from_buffer(const char* data, std::size_t size, std::optional<detail::Encoding> encoding_fallback) { // Type can't be deduced? _run_load_func(std::mem_fn(&ParseHandler::load_buffer_size), data, size, encoding_fallback); return *this; } -constexpr Parser& Parser::load_from_buffer(const char* start, const char* end, std::optional<detail::Encoding> encoding_fallback) { +Parser& Parser::load_from_buffer(const char* start, const char* end, std::optional<detail::Encoding> encoding_fallback) { // Type can't be deduced? _run_load_func(std::mem_fn(&ParseHandler::load_buffer), start, end, encoding_fallback); return *this; } -constexpr Parser& Parser::load_from_string(const std::string_view string, std::optional<detail::Encoding> encoding_fallback) { +Parser& Parser::load_from_string(const std::string_view string, std::optional<detail::Encoding> encoding_fallback) { return load_from_buffer(string.data(), string.size(), encoding_fallback); } @@ -166,7 +176,7 @@ bool Parser::parse_csv(bool handle_strings) { }(); _has_error = _parse_handler->parse_state().logger().errored(); _has_warning = _parse_handler->parse_state().logger().warned(); - if (!errors->empty()) { + if (errors && !errors->empty()) { _has_error = true; _has_fatal_error = true; if (&_error_stream.get() != &detail::cnull) { @@ -182,7 +192,7 @@ const std::vector<csv::LineObject>& Parser::get_lines() const { } typename Parser::error_range Parser::get_errors() const { - return _parse_handler->parse_state().logger().get_errors(); + return _parse_handler->get_errors(); } const FilePosition Parser::get_error_position(const error::Error* error) const { diff --git a/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp b/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp index 5a98b40..71985f3 100644 --- a/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp +++ b/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp @@ -1,4 +1,4 @@ -#include "openvic-dataloader/v2script/AbstractSyntaxTree.hpp" +#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> #include <lexy/dsl/option.hpp> #include <lexy/encoding.hpp> @@ -31,6 +31,10 @@ ListValue::ListValue(dryad::node_ctor ctor, AssignStatementList statements) : no } } +ListValue::ListValue(dryad::node_ctor ctor) : node_base(ctor) { + _last_statement = nullptr; +} + FileTree::FileTree(dryad::node_ctor ctor, StatementList statements) : node_base(ctor) { insert_child_list_after(nullptr, statements); if (statements.empty()) { @@ -49,6 +53,10 @@ FileTree::FileTree(dryad::node_ctor ctor, AssignStatementList statements) : node } } +FileTree::FileTree(dryad::node_ctor ctor) : node_base(ctor) { + _last_node = nullptr; +} + std::string FileAbstractSyntaxTree::make_list_visualizer() const { const int INDENT_SIZE = 2; diff --git a/src/openvic-dataloader/v2script/Parser.cpp b/src/openvic-dataloader/v2script/Parser.cpp index 23dada7..a0003b1 100644 --- a/src/openvic-dataloader/v2script/Parser.cpp +++ b/src/openvic-dataloader/v2script/Parser.cpp @@ -76,6 +76,9 @@ struct Parser::ParseHandler final : detail::BasicStateParseHandler<v2script::ast } Parser::error_range get_errors() { + using iterator = typename decltype(std::declval<const error::Root*>()->children())::iterator; + if (!is_valid()) + return dryad::make_node_range<error::Error>(iterator::from_ptr(nullptr), iterator::from_ptr(nullptr)); return parse_state().logger().get_errors(); } }; @@ -149,19 +152,19 @@ constexpr void Parser::_run_load_func(detail::LoadCallback<Parser::ParseHandler* } } -constexpr Parser& Parser::load_from_buffer(const char* data, std::size_t size, std::optional<detail::Encoding> encoding_fallback) { +Parser& Parser::load_from_buffer(const char* data, std::size_t size, std::optional<detail::Encoding> encoding_fallback) { // Type can't be deduced? _run_load_func(std::mem_fn(&ParseHandler::load_buffer_size), data, size, encoding_fallback); return *this; } -constexpr Parser& Parser::load_from_buffer(const char* start, const char* end, std::optional<detail::Encoding> encoding_fallback) { +Parser& Parser::load_from_buffer(const char* start, const char* end, std::optional<detail::Encoding> encoding_fallback) { // Type can't be deduced? _run_load_func(std::mem_fn(&ParseHandler::load_buffer), start, end, encoding_fallback); return *this; } -constexpr Parser& Parser::load_from_string(const std::string_view string, std::optional<detail::Encoding> encoding_fallback) { +Parser& Parser::load_from_string(const std::string_view string, std::optional<detail::Encoding> encoding_fallback) { return load_from_buffer(string.data(), string.size(), encoding_fallback); } |