/* * This file is a part of PikoTools * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name Tomasz Sowa nor the names of contributors to this * project may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "csvparser.h" #include "csv/csvparser.h" #include "test.h" namespace pt { namespace pt_csvparser_tests { void test_csvparser(const char * input_str, const char * expected_json) { CSVParser csv_parser; Space space; std::string json; CSVParser::Status status = csv_parser.parse(input_str, space); space.serialize_to_json_to(json); std::cout << "csv parsed as: " << json << std::endl; test(json.c_str(), expected_json); } void test_csvparser1() { const char * input_str = ""; const char * expected_json = R"json([[]])json"; test_csvparser(input_str, expected_json); } void test_csvparser2() { const char * input_str = ","; const char * expected_json = R"json([["",""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser3() { const char * input_str = "field1"; const char * expected_json = R"json([["field1"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser4() { const char * input_str = R"csvstring(field1,field2,field3)csvstring"; const char * expected_json = R"json([["field1","field2","field3"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser5() { const char * input_str = "\n"; const char * expected_json = R"json([[""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser6() { const char * input_str = "\r\n"; const char * expected_json = R"json([[""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser7() { const char * input_str = "field1\r\n"; const char * expected_json = R"json([["field1"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser8() { const char * input_str = ",\r\n"; const char * expected_json = R"json([["",""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser9() { const char * input_str = "field1\r\nfield2"; const char * expected_json = R"json([["field1"],["field2"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser10() { const char * input_str = "field1,field2\r\nfield3,field4"; const char * expected_json = R"json([["field1","field2"],["field3","field4"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser11() { const char * input_str = "field1,field2\r\nfield3,field4\r\n"; const char * expected_json = R"json([["field1","field2"],["field3","field4"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser12() { const char * input_str = "field1,field2\nfield3,field4\n"; const char * expected_json = R"json([["field1","field2"],["field3","field4"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser13() { const char * input_str = R"csv("")csv"; const char * expected_json = R"json([[""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser14() { const char * input_str = "\"\"\n"; const char * expected_json = R"json([[""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser15() { const char * input_str = "\"\"\r\n"; const char * expected_json = R"json([[""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser16() { const char * input_str = "\"\",\r\n"; const char * expected_json = R"json([["",""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser17() { const char * input_str = "\"\",\n"; const char * expected_json = R"json([["",""]])json"; test_csvparser(input_str, expected_json); } void test_csvparser18() { const char * input_str = "\"field1\""; const char * expected_json = R"json([["field1"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser19() { const char * input_str = "\"field1, with comma\""; const char * expected_json = R"json([["field1, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser20() { const char * input_str = "\"field1, with comma\"\r\n"; const char * expected_json = R"json([["field1, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser21() { const char * input_str = "\"field1, with comma\"\n"; const char * expected_json = R"json([["field1, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser22() { const char * input_str = "\"field1, with comma\",\"field2\""; const char * expected_json = R"json([["field1, with comma","field2"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser23() { const char * input_str = "\"field1, with comma\",\"field2\"\r\n\"field3\",\"field4, with comma\""; const char * expected_json = R"json([["field1, with comma","field2"],["field3","field4, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser24() { const char * input_str = "\"field1, with comma\",\"field2\"\r\n\"field3\",\"field4, with comma\"\r\n"; const char * expected_json = R"json([["field1, with comma","field2"],["field3","field4, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser25() { const char * input_str = "\"field1, with comma\",\"field2 with \"\" double quote\"\r\n\"field3\",\"field4, with comma\""; const char * expected_json = R"json([["field1, with comma","field2 with \" double quote"],["field3","field4, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser26() { const char * input_str = "\"field1, with comma\",\"field2 with \"\" double quote\"\n\"field3\",\"field4, with comma\"\n"; const char * expected_json = R"json([["field1, with comma","field2 with \" double quote"],["field3","field4, with comma"]])json"; test_csvparser(input_str, expected_json); } void test_csvparser27() { const char * input_str = "\"field1, with comma\",\"field2 with \"\" double quote\"syntax error\n\"field3\",\"field4, with comma\"\n"; const char * expected_json = R"json([["field1, with comma","field2 with \" double quote"],["syntax error"],["field3","field4, with comma"]])json"; test_csvparser(input_str, expected_json); } void make_tests() { reset_test_counter("CSVParser"); test_csvparser1(); test_csvparser2(); test_csvparser3(); test_csvparser4(); test_csvparser5(); test_csvparser6(); test_csvparser7(); test_csvparser8(); test_csvparser9(); test_csvparser10(); test_csvparser11(); test_csvparser12(); test_csvparser13(); test_csvparser14(); test_csvparser15(); test_csvparser16(); test_csvparser17(); test_csvparser18(); test_csvparser19(); test_csvparser20(); test_csvparser21(); test_csvparser22(); test_csvparser23(); test_csvparser24(); test_csvparser25(); test_csvparser26(); test_csvparser27(); } } }