| 1 | use crate::Alignment; |
| 2 | use crate::TabWriter; |
| 3 | use std::io::Write; |
| 4 | |
| 5 | fn ordie<T, E: ToString>(r: Result<T, E>) -> T { |
| 6 | match r { |
| 7 | Ok(r) => r, |
| 8 | Err(e) => panic!("{}", e.to_string()), |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | fn readable_str(s: &str) -> String { |
| 13 | s.replace(" ", "·").replace("\t", "<TAB>") |
| 14 | } |
| 15 | |
| 16 | fn tabw() -> TabWriter<Vec<u8>> { |
| 17 | TabWriter::new(Vec::new()) |
| 18 | } |
| 19 | |
| 20 | fn tabify(mut tw: TabWriter<Vec<u8>>, s: &str) -> String { |
| 21 | ordie(write!(&mut tw, "{}", s)); |
| 22 | ordie(tw.flush()); |
| 23 | ordie(String::from_utf8(tw.into_inner().unwrap())) |
| 24 | } |
| 25 | |
| 26 | fn iseq(tw: TabWriter<Vec<u8>>, s: &str, expected: &str) { |
| 27 | let written = tabify(tw, s); |
| 28 | if expected != written { |
| 29 | panic!( |
| 30 | "\n\nexpected:\n-----\n{}\n-----\ngot:\n-----\n{}\n-----\n\n", |
| 31 | readable_str(expected), |
| 32 | readable_str(&written) |
| 33 | ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn test_no_cells() { |
| 39 | iseq(tabw(), "foo\nbar\nfubar", "foo\nbar\nfubar"); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn test_no_cells_trailing() { |
| 44 | iseq(tabw(), "foo\nbar\nfubar\n", "foo\nbar\nfubar\n"); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn test_no_cells_prior() { |
| 49 | iseq(tabw(), "\nfoo\nbar\nfubar", "\nfoo\nbar\nfubar"); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn test_empty() { |
| 54 | iseq(tabw(), "", ""); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn test_empty_lines() { |
| 59 | iseq(tabw(), "\n\n\n\n", "\n\n\n\n"); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_empty_cell() { |
| 64 | iseq(tabw().padding(0).minwidth(2), "\t\n", " \n"); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn test_empty_cell_no_min() { |
| 69 | iseq(tabw().padding(0).minwidth(0), "\t\n", "\n"); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn test_empty_cells() { |
| 74 | iseq(tabw().padding(0).minwidth(2), "\t\t\n", " \n"); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn test_empty_cells_no_min() { |
| 79 | iseq(tabw().padding(0).minwidth(0), "\t\t\n", "\n"); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn test_empty_cells_ignore_trailing() { |
| 84 | iseq(tabw().padding(0).minwidth(2), "\t\t\t", " "); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn test_one_cell() { |
| 89 | iseq(tabw().padding(2).minwidth(2), "a\tb\nxx\tyy", "a b\nxx yy"); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn test_one_cell_right() { |
| 94 | iseq( |
| 95 | tabw().padding(2).minwidth(2).alignment(Alignment::Right), |
| 96 | "a\tb\nxx\tyy", |
| 97 | " a b\nxx yy", |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn test_one_cell_center() { |
| 103 | iseq( |
| 104 | tabw().padding(2).minwidth(2).alignment(Alignment::Center), |
| 105 | "a\tb\nxx\tyy", |
| 106 | "a b\nxx yy", |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn test_no_padding() { |
| 112 | iseq(tabw().padding(0).minwidth(2), "a\tb\nxx\tyy", "a b\nxxyy"); |
| 113 | } |
| 114 | |
| 115 | // See: https://github.com/BurntSushi/tabwriter/issues/26 |
| 116 | #[test] |
| 117 | fn test_no_padding_one_row() { |
| 118 | iseq(tabw().padding(0).minwidth(2), "a\tb\n", "a b\n"); |
| 119 | iseq(tabw().padding(0).minwidth(1), "a\tb\n", "ab\n"); |
| 120 | iseq(tabw().padding(0).minwidth(0), "a\tb\n", "ab\n"); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn test_minwidth() { |
| 125 | iseq(tabw().minwidth(5).padding(0), "a\tb\nxx\tyy", "a b\nxx yy"); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn test_contiguous_columns() { |
| 130 | iseq( |
| 131 | tabw().padding(1).minwidth(0), |
| 132 | "x\tfoo\tx\nx\tfoofoo\tx\n\nx\tfoofoofoo\tx", |
| 133 | "x foo x\nx foofoo x\n\nx foofoofoo x", |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn test_table_right() { |
| 139 | iseq( |
| 140 | tabw().padding(1).minwidth(0).alignment(Alignment::Right), |
| 141 | "x\tfoo\txx\t\nxx\tfoofoo\tx\t\n", |
| 142 | " x foo xx \nxx foofoo x \n", |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn test_table_center() { |
| 148 | iseq( |
| 149 | tabw().padding(1).minwidth(0).alignment(Alignment::Center), |
| 150 | "x\tfoo\txx\t\nxx\tfoofoo\tx\t\n", |
| 151 | "x foo xx \nxx foofoo x \n", |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn test_contiguous_columns_right() { |
| 157 | iseq( |
| 158 | tabw().padding(1).minwidth(0).alignment(Alignment::Right), |
| 159 | "x\tfoo\tx\nx\tfoofoo\tx\n\nx\tfoofoofoo\tx", |
| 160 | "x foo x\nx foofoo x\n\nx foofoofoo x", |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn test_contiguous_columns_center() { |
| 166 | iseq( |
| 167 | tabw().padding(1).minwidth(0).alignment(Alignment::Center), |
| 168 | "x\tfoo\tx\nx\tfoofoo\tx\n\nx\tfoofoofoo\tx", |
| 169 | "x foo x\nx foofoo x\n\nx foofoofoo x", |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn test_unicode() { |
| 175 | iseq( |
| 176 | tabw().padding(2).minwidth(2), |
| 177 | "a\tÞykkvibær\tz\naaaa\tïn Bou Chella\tzzzz\na\tBâb el Ahmar\tz", |
| 178 | "a Þykkvibær z\n\ |
| 179 | aaaa ïn Bou Chella zzzz\n\ |
| 180 | a Bâb el Ahmar z", |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn test_contiguous_columns_complex() { |
| 186 | iseq( |
| 187 | tabw().padding(1).minwidth(3), |
| 188 | " |
| 189 | fn foobar() { |
| 190 | let mut x = 1+1; // addition |
| 191 | x += 1; // increment in place |
| 192 | let y = x * x * x * x; // multiply! |
| 193 | |
| 194 | y += 1; // this is another group |
| 195 | y += 2 * 2; // that is separately aligned |
| 196 | } |
| 197 | ", |
| 198 | " |
| 199 | fn foobar() { |
| 200 | let mut x = 1+1; // addition |
| 201 | x += 1; // increment in place |
| 202 | let y = x * x * x * x; // multiply! |
| 203 | |
| 204 | y += 1; // this is another group |
| 205 | y += 2 * 2; // that is separately aligned |
| 206 | } |
| 207 | ", |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | // This tests that ANSI formatting is handled automatically when the ansi_formatting |
| 212 | // feature is enabled without any other configuration. |
| 213 | #[test] |
| 214 | #[cfg(feature = "ansi_formatting")] |
| 215 | fn ansi_formatting_by_feature() { |
| 216 | let output = "foo\tbar\tfoobar\n\ |
| 217 | \x1b[31mföÅ\x1b[0m\t\x1b[32mbär\x1b[0m\t\x1b[36mfoobar\x1b[0m\n\ |
| 218 | \x1b[34mfoo\tbar\tfoobar\n\x1b[0m"; |
| 219 | |
| 220 | iseq( |
| 221 | tabw(), |
| 222 | &output[..], |
| 223 | "foo bar foobar\n\ |
| 224 | \x1b[31mföÅ\x1b[0m \x1b[32mbär\x1b[0m \x1b[36mfoobar\x1b[0m\n\ |
| 225 | \x1b[34mfoo bar foobar\n\x1b[0m", |
| 226 | ) |
| 227 | } |
| 228 | |
| 229 | // This tests that ANSI formatting is handled when explicitly opted into, |
| 230 | // regardless of whether the ansi_formatting feature is enabled. |
| 231 | #[test] |
| 232 | fn ansi_formatting_by_config() { |
| 233 | let output = "foo\tbar\tfoobar\n\ |
| 234 | \x1b[31mföÅ\x1b[0m\t\x1b[32mbär\x1b[0m\t\x1b[36mfoobar\x1b[0m\n\ |
| 235 | \x1b[34mfoo\tbar\tfoobar\n\x1b[0m"; |
| 236 | |
| 237 | iseq( |
| 238 | tabw().ansi(true), |
| 239 | &output[..], |
| 240 | "foo bar foobar\n\ |
| 241 | \x1b[31mföÅ\x1b[0m \x1b[32mbär\x1b[0m \x1b[36mfoobar\x1b[0m\n\ |
| 242 | \x1b[34mfoo bar foobar\n\x1b[0m", |
| 243 | ) |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn tab_indent() { |
| 248 | iseq( |
| 249 | tabw().tab_indent(true).padding(1), |
| 250 | " |
| 251 | type cell struct { |
| 252 | \tsize\tint\t// cell size in bytes |
| 253 | \twidth\tint\t// cell width in runes |
| 254 | \thtab\tbool\t// true if the cell is terminated by an htab ('\\t') |
| 255 | } |
| 256 | ", |
| 257 | " |
| 258 | type cell struct { |
| 259 | \tsize int // cell size in bytes |
| 260 | \twidth int // cell width in runes |
| 261 | \thtab bool // true if the cell is terminated by an htab ('\\t') |
| 262 | } |
| 263 | ", |
| 264 | ) |
| 265 | } |