Browse Source

[3555] CSVRow.append() implemented.

Tomek Mrugalski 10 years ago
parent
commit
0984c85a59
2 changed files with 33 additions and 0 deletions
  1. 14 0
      src/lib/util/csv_file.h
  2. 19 0
      src/lib/util/tests/csv_file_unittest.cc

+ 14 - 0
src/lib/util/csv_file.h

@@ -178,6 +178,20 @@ public:
         writeAt(at, value.c_str());
     }
 
+    /// @brief Appends the value as a new column.
+    ///
+    /// @param value Value to be written.
+    /// @tparam T Type of the value being written.
+    template<typename T>
+    void append(const T value) {
+        try {
+            values_.push_back(boost::lexical_cast<std::string>(value));
+        } catch (const boost::bad_lexical_cast& ex) {
+            isc_throw(CSVFileError, "unable to stringify the value to be "
+                      "appended to the CSV file row.");
+        }
+    }
+
     /// @brief Replaces the value at specified index.
     ///
     /// This function is used to set values to be rendered using

+ 19 - 0
src/lib/util/tests/csv_file_unittest.cc

@@ -90,6 +90,25 @@ TEST(CSVRow, writeAt) {
     EXPECT_THROW(row.writeAt(3, "foo"), CSVFileError);
 }
 
+// Checks whether writeAt() and append() can be mixed together.
+TEST(CSVRow, append) {
+    CSVRow row(3);
+
+    EXPECT_EQ(3, row.getValuesCount());
+
+    row.writeAt(0, "alpha");
+    ASSERT_NO_THROW(row.append("delta"));
+    EXPECT_EQ(4, row.getValuesCount());
+    row.writeAt(1, "beta");
+    row.writeAt(2, "gamma");
+    ASSERT_NO_THROW(row.append("epsilon"));
+    EXPECT_EQ(5, row.getValuesCount());
+
+    std::string text;
+    ASSERT_NO_THROW(text = row.render());
+    EXPECT_EQ("alpha,beta,gamma,delta,epsilon", text);
+}
+
 /// @brief Test fixture class for testing operations on CSV file.
 ///
 /// It implements basic operations on files, such as reading writing