]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1603] CSVFileTest.parse*WithoutTrailingBlankLine
authorAndrei Pavel <andrei@isc.org>
Fri, 18 Dec 2020 12:24:08 +0000 (14:24 +0200)
committerAndrei Pavel <andrei@isc.org>
Mon, 4 Jan 2021 17:43:53 +0000 (17:43 +0000)
src/lib/util/tests/csv_file_unittest.cc

index 7ae6b643bf77ae1f56fa79dc8fbdf94085eda4f6..17c2161db879e385f38fefa0159fd34b5a977724 100644 (file)
@@ -579,4 +579,65 @@ TEST_F(CSVFileTest, exists) {
     EXPECT_FALSE(csv->exists());
 }
 
+// Check that a single header without a trailing blank line can be parsed.
+TEST_F(CSVFileTest, parseHeaderWithoutTrailingBlankLine) {
+    // Create a new CSV file that only contains a header without a new line.
+    writeFile("animal,age,color");
+
+    // Open this file and check that the header is parsed.
+    CSVFile csv(testfile_);
+    ASSERT_NO_THROW(csv.open());
+    ASSERT_EQ(3, csv.getColumnCount());
+    EXPECT_EQ("animal", csv.getColumnName(0));
+    EXPECT_EQ("age", csv.getColumnName(1));
+    EXPECT_EQ("color", csv.getColumnName(2));
+
+    // Attempt to read the next row which doesn't exist.
+    CSVRow row;
+    ASSERT_TRUE(csv.next(row));
+    EXPECT_EQ(CSVFile::EMPTY_ROW(), row);
+
+    // Close the file.
+    csv.close();
+}
+
+// Check that content without a trailing blank line can be parsed.
+TEST_F(CSVFileTest, parseContentWithoutTrailingBlankLine) {
+    // Now create a new CSV file that contains header plus data, but the last
+    // line is missing a new line.
+    writeFile("animal,age,color\n"
+              "cat,4,white\n"
+              "lion,8,yellow");
+
+    // Open this file and check that the header is parsed.
+    CSVFile csv(testfile_);
+    ASSERT_NO_THROW(csv.open());
+    ASSERT_EQ(3, csv.getColumnCount());
+    EXPECT_EQ("animal", csv.getColumnName(0));
+    EXPECT_EQ("age", csv.getColumnName(1));
+    EXPECT_EQ("color", csv.getColumnName(2));
+
+    // Check the first data row.
+    CSVRow row;
+    ASSERT_TRUE(csv.next(row));
+    EXPECT_EQ("cat", row.readAt(0));
+    EXPECT_EQ("4", row.readAt(1));
+    EXPECT_EQ("white", row.readAt(2));
+    EXPECT_EQ("success", csv.getReadMsg());
+
+    // Check the second data row.
+    ASSERT_TRUE(csv.next(row));
+    EXPECT_EQ("lion", row.readAt(0));
+    EXPECT_EQ("8", row.readAt(1));
+    EXPECT_EQ("yellow", row.readAt(2));
+    EXPECT_EQ("success", csv.getReadMsg());
+
+    // Attempt to read the next row which doesn't exist.
+    ASSERT_TRUE(csv.next(row));
+    EXPECT_EQ(CSVFile::EMPTY_ROW(), row);
+
+    // Close the file.
+    csv.close();
+}
+
 } // end of anonymous namespace