diff options
author | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2017-06-05 19:46:55 +0200 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2017-06-05 19:46:55 +0200 |
commit | ca4a6f7e96ccfd476536a6829504c61b11c18da0 (patch) | |
tree | 37a57fb4328dbaca38b00ad475ebca715e9631b5 /src | |
parent | 1b9555870a24db4181d7d9918f58b6b13dbf17a2 (diff) |
Added UT & fixed warnings
+ fixed according to @hcpl comments
Diffstat (limited to 'src')
-rw-r--r-- | src/format.rs | 25 | ||||
-rw-r--r-- | src/lib.rs | 18 | ||||
-rw-r--r-- | src/row.rs | 3 |
3 files changed, 28 insertions, 18 deletions
diff --git a/src/format.rs b/src/format.rs index e1dcd46..6b83da7 100644 --- a/src/format.rs +++ b/src/format.rs @@ -150,7 +150,7 @@ impl TableFormat { bottom_sep: None, pad_left: 0, pad_right: 0, - indent: 0 + indent: 0, } } @@ -207,12 +207,12 @@ impl TableFormat { } } - /// Set global indentation in spaces used when rendering a table + /// Set global indentation in spaces used when rendering a table pub fn indent(&mut self, spaces: usize) { self.indent = spaces; } - /// Get global indentation in spaces used when rendering a table + /// Get global indentation in spaces used when rendering a table pub fn get_indent(&self) -> usize { self.indent } @@ -225,7 +225,8 @@ impl TableFormat { -> Result<(), Error> { match *self.get_sep_for_line(pos) { Some(ref l) => { - out.write_all(&vec![b' '; self.get_indent()]); + //TODO: Wrap this into dedicated function one day + try!(out.write_all(&vec![b' '; self.get_indent()])); l._print(out, col_width, self.get_padding(), @@ -276,16 +277,6 @@ impl FormatBuilder { FormatBuilder { format: Box::new(TableFormat::new()) } } - /// Creates a new builder initialized with provided format - pub fn from_format(format: TableFormat) -> FormatBuilder { - FormatBuilder { format: Box::new(format) } - } - - /// Consumes the builder and returns the generated `TableFormat` - pub fn into_table_format(self) -> TableFormat { - *self.format - } - /// Set left and right padding pub fn padding(mut self, left: usize, right: usize) -> Self { self.format.padding(left, right); @@ -316,7 +307,7 @@ impl FormatBuilder { self } - /// Set global indentation in spaces used when rendering a table + /// Set global indentation in spaces used when rendering a table pub fn indent(mut self, spaces: usize) -> Self { self.format.indent(spaces); self @@ -330,13 +321,13 @@ impl FormatBuilder { impl Into<TableFormat> for FormatBuilder { fn into(self) -> TableFormat { - self.into_table_format() + *self.format } } impl From<TableFormat> for FormatBuilder { fn from(fmt: TableFormat) -> Self { - Self::from_format(fmt) + FormatBuilder { format: Box::new(fmt) } } } @@ -767,6 +767,24 @@ mod tests { } #[test] + fn indent() { + let mut table = Table::new(); + table.add_row(Row::new(vec![Cell::new("a"), Cell::new("bc"), Cell::new("def")])); + table.add_row(Row::new(vec![Cell::new("def"), Cell::new("bc"), Cell::new("a")])); + table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")])); + table.get_format().indent(8); + let out = r" +-----+----+-----+ + | t1 | t2 | t3 | + +=====+====+=====+ + | a | bc | def | + +-----+----+-----+ + | def | bc | a | + +-----+----+-----+ +"; + assert_eq!(table.to_string().replace("\r\n", "\n"), out); + } + + #[test] fn slices() { let mut table = Table::new(); table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")])); @@ -119,7 +119,8 @@ impl Row { where F: Fn(&Cell, &mut T, usize, usize, bool) -> Result<(), Error> { for i in 0..self.get_height() { - out.write_all(&vec![b' '; format.get_indent()]); + //TODO: Wrap this into dedicated function one day + try!(out.write_all(&vec![b' '; format.get_indent()])); try!(format.print_column_separator(out, ColumnPosition::Left)); let (lp, rp) = format.get_padding(); for j in 0..col_width.len() { |