From e316558a7b41cd9df7d6e8c9091f29b110602e45 Mon Sep 17 00:00:00 2001 From: ado Date: Tue, 8 Aug 2023 11:07:26 +0200 Subject: [PATCH] Add parser test for invalid rows with header --- test/test_parser.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/test/test_parser.cpp b/test/test_parser.cpp index 94e6c4c..e7089ff 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -1491,6 +1491,104 @@ TEST_CASE("parser test invalid header fields usage") { test_invalid_fields({"Int,String,Int", "1,hi,3"}, {"Int", "String", "Int"}); } +template +void test_invalid_rows_with_header() { + unique_file_name f{"test_parser"}; + { + std::ofstream out{f.name}; + out << "Int,String,Double" << std::endl; + out << "1,line1,2.34" << std::endl; + out << "2,line2" << std::endl; + out << "3,line3,67.8" << std::endl; + out << "4,line4,67.8,9" << std::endl; + out << "5,line5,9.10" << std::endl; + out << "six,line6,10.11" << std::endl; + } + + { + ss::parser p{f.name}; + + p.use_fields("Int", "String", "Double"); + using data = std::tuple; + std::vector i; + + CHECK(p.valid()); + + while (!p.eof()) { + try { + const auto& t = p.template get_next(); + if (p.valid()) { + i.push_back(t); + } + } catch (const ss::exception&) { + continue; + } + } + + std::vector expected = {{1, "line1", 2.34}, + {3, "line3", 67.8}, + {5, "line5", 9.10}}; + CHECK_EQ(i, expected); + } + + { + ss::parser p{f.name}; + + p.use_fields("Double", "Int"); + using data = std::tuple; + std::vector i; + + CHECK(p.valid()); + + while (!p.eof()) { + try { + const auto& t = p.template get_next(); + if (p.valid()) { + i.push_back(t); + } + } catch (const ss::exception&) { + continue; + } + } + + std::vector expected = {{2.34, 1}, {67.8, 3}, {9.10, 5}}; + CHECK_EQ(i, expected); + } + + { + ss::parser p{f.name}; + + p.use_fields("String", "Double"); + using data = std::tuple; + std::vector i; + + CHECK(p.valid()); + + while (!p.eof()) { + try { + const auto& t = p.template get_next(); + if (p.valid()) { + i.push_back(t); + } + } catch (const ss::exception&) { + continue; + } + } + + std::vector expected = {{"line1", 2.34}, + {"line3", 67.8}, + {"line5", 9.10}, + {"line6", 10.11}}; + CHECK_EQ(i, expected); + } +} + +TEST_CASE("parser test invalid rows with header") { + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); +} + template void test_ignore_empty_impl(const std::vector& data) { unique_file_name f{"test_parser"};