summaryrefslogtreecommitdiffstats
path: root/src/csv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/csv.rs')
-rw-r--r--src/csv.rs75
1 files changed, 73 insertions, 2 deletions
diff --git a/src/csv.rs b/src/csv.rs
index cb8ec72..55f10e2 100644
--- a/src/csv.rs
+++ b/src/csv.rs
@@ -56,9 +56,9 @@ impl super::Table {
Self::init(reader
.records()
.map(|row| {
- super::row::Row::new(row.unwrap()
+ super::Row::new(row.unwrap()
.into_iter()
- .map(|cell| super::cell::Cell::new(&cell))
+ .map(|cell| super::Cell::new(&cell))
.collect())
})
.collect())
@@ -76,4 +76,75 @@ impl super::Table {
pub fn to_csv_writer<W: Write>(&self, writer: Writer<W>) -> Result<Writer<W>> {
self.as_ref().to_csv_writer(writer)
}
+}
+
+
+#[cfg(test)]
+mod tests {
+ use {Table, Row, Cell};
+
+ static CSV_S: &'static str = "ABC,DEFG,HIJKLMN\n\
+ foobar,bar,foo\n\
+ foobar2,bar2,foo2\n";
+
+ fn test_table() -> Table {
+ let mut table = Table::new();
+ table
+ .add_row(Row::new(vec![Cell::new("ABC"), Cell::new("DEFG"), Cell::new("HIJKLMN")]));
+ table.add_row(Row::new(vec![Cell::new("foobar"), Cell::new("bar"), Cell::new("foo")]));
+ table.add_row(Row::new(vec![Cell::new("foobar2"),
+ Cell::new("bar2"),
+ Cell::new("foo2")]));
+ table
+ }
+
+ #[test]
+ fn from() {
+ assert_eq!(test_table().to_string().replace("\r\n", "\n"),
+ Table::from_csv_string(CSV_S)
+ .unwrap()
+ .to_string()
+ .replace("\r\n", "\n"));
+ }
+
+ #[test]
+ fn to() {
+ assert_eq!(
+ String::from_utf8(
+ test_table()
+ .to_csv(Vec::new())
+ .unwrap()
+ .into_inner()
+ .unwrap()
+ ).unwrap(),
+ CSV_S);
+ }
+
+ #[test]
+ fn trans() {
+ assert_eq!(
+ Table::from_csv_string(
+ &String::from_utf8(
+ test_table()
+ .to_csv(Vec::new())
+ .unwrap()
+ .into_inner()
+ .unwrap()
+ ).unwrap()
+ ).unwrap()
+ .to_string()
+ .replace("\r\n", "\n"),
+ test_table().to_string().replace("\r\n", "\n"));
+ }
+
+ #[test]
+ fn extend_table() {
+ let mut table = Table::new();
+ table.add_row(Row::new(vec![Cell::new("ABC"), Cell::new("DEFG"), Cell::new("HIJKLMN")]));
+ table.extend(vec![vec!["A", "B", "C"]]);
+ let t2 = table.clone();
+ table.extend(t2.rows);
+ assert_eq!(table.get_row(1).unwrap().get_cell(2).unwrap().get_content(), "C");
+ assert_eq!(table.get_row(2).unwrap().get_cell(1).unwrap().get_content(), "DEFG");
+ }
} \ No newline at end of file