Skip to content

Commit 8f05bb1

Browse files
joeykleingersimikejackson
authored andcommitted
Add unit test that verifies that quotes are removed from strings.
Signed-off-by: Joey Kleingers <[email protected]>
1 parent d512e8e commit 8f05bb1

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/Plugins/SimplnxCore/test/ReadCSVFileTest.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <catch2/catch.hpp>
1414

1515
#include <fstream>
16+
#include <regex>
1617

1718
namespace fs = std::filesystem;
1819
using namespace nx::core;
@@ -552,4 +553,69 @@ TEST_CASE("SimplnxCore::ReadCSVFileFilter (Case 7): Valid filter execution - Str
552553
REQUIRE(array->at(i) == v[i]);
553554
}
554555
}
556+
}
557+
558+
TEST_CASE("SimplnxCore::ReadCSVFileFilter (Case 8): Valid filter execution - Mixed quoted strings and integers")
559+
{
560+
UnitTest::LoadPlugins();
561+
fs::create_directories(k_TestInput.parent_path());
562+
563+
// Create CSV with single and double quoted strings and integer strings
564+
std::ofstream file(k_TestInput);
565+
REQUIRE(file.is_open());
566+
file << "SQ,DQ,Num\n";
567+
std::vector<std::tuple<std::string, std::string, std::string>> rows = {{"'Alice'", "\"Alice\"", "1"}, {"'Bob'", "\"Bob\"", "2"}, {"'Charlie'", "\"Charlie\"", "3"}};
568+
for(size_t i = 0; i < rows.size(); ++i)
569+
{
570+
file << std::get<0>(rows[i]) << "," << std::get<1>(rows[i]) << "," << std::get<2>(rows[i]);
571+
if(i < rows.size() - 1)
572+
{
573+
file << "\n";
574+
}
575+
}
576+
file.close();
577+
578+
// Set up filter arguments
579+
std::vector<std::string> dummy = {"1", "2", "3"};
580+
Arguments args = createArguments(k_TestInput.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {"SQ", "DQ", "Num"}, {CSVType::string, CSVType::string, CSVType::string}, {false, false, false},
581+
{static_cast<usize>(dummy.size())}, dummy, "New Group");
582+
583+
ReadCSVFileFilter filter;
584+
DataStructure dataStructure;
585+
auto preflightResult = filter.preflight(dataStructure, args);
586+
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);
587+
588+
auto executeResult = filter.execute(dataStructure, args);
589+
SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result);
590+
591+
const std::regex re(R"(^['"]+|['"]+$)"); // Remove quotes and double quotes
592+
593+
// Verify single quoted column
594+
const StringArray* sqArray = dataStructure.getDataAs<StringArray>(DataPath({"New Group", "SQ"}));
595+
REQUIRE(sqArray != nullptr);
596+
REQUIRE(sqArray->getSize() == rows.size());
597+
for(usize i = 0; i < sqArray->getSize(); ++i)
598+
{
599+
auto str = std::regex_replace(std::get<0>(rows[i]), re, "");
600+
REQUIRE(sqArray->at(i) == str);
601+
}
602+
603+
// Verify double quoted column
604+
const StringArray* dqArray = dataStructure.getDataAs<StringArray>(DataPath({"New Group", "DQ"}));
605+
REQUIRE(dqArray != nullptr);
606+
REQUIRE(dqArray->getSize() == rows.size());
607+
for(usize i = 0; i < dqArray->getSize(); ++i)
608+
{
609+
auto str = std::regex_replace(std::get<1>(rows[i]), re, "");
610+
REQUIRE(sqArray->at(i) == str);
611+
}
612+
613+
// Verify integer column
614+
const StringArray* numArray = dataStructure.getDataAs<StringArray>(DataPath({"New Group", "Num"}));
615+
REQUIRE(numArray != nullptr);
616+
REQUIRE(numArray->getSize() == rows.size());
617+
for(usize i = 0; i < numArray->getSize(); ++i)
618+
{
619+
REQUIRE(numArray->at(i) == std::get<2>(rows[i]));
620+
}
555621
}

0 commit comments

Comments
 (0)