| 1 | use comfy_table::*; |
| 2 | use pretty_assertions::assert_eq; |
| 3 | |
| 4 | #[test] |
| 5 | fn add_predicate_single_true() { |
| 6 | let mut table = Table::new(); |
| 7 | table |
| 8 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 9 | .add_row(vec![ |
| 10 | "This is a text", |
| 11 | "This is another text", |
| 12 | "This is the third text", |
| 13 | ]) |
| 14 | .add_row_if( |
| 15 | |_, _| true, |
| 16 | &vec![ |
| 17 | "This is another text", |
| 18 | "Now\nadd some\nmulti line stuff", |
| 19 | "This is awesome", |
| 20 | ], |
| 21 | ); |
| 22 | |
| 23 | println!("{table}"); |
| 24 | let expected = " |
| 25 | +----------------------+----------------------+------------------------+ |
| 26 | | Header1 | Header2 | Header3 | |
| 27 | +======================================================================+ |
| 28 | | This is a text | This is another text | This is the third text | |
| 29 | |----------------------+----------------------+------------------------| |
| 30 | | This is another text | Now | This is awesome | |
| 31 | | | add some | | |
| 32 | | | multi line stuff | | |
| 33 | +----------------------+----------------------+------------------------+"; |
| 34 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn add_predicate_single_false() { |
| 39 | let mut table = Table::new(); |
| 40 | table |
| 41 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 42 | .add_row(vec![ |
| 43 | "This is a text", |
| 44 | "This is another text", |
| 45 | "This is the third text", |
| 46 | ]) |
| 47 | .add_row_if( |
| 48 | |_, _| false, |
| 49 | &vec![ |
| 50 | "This is another text", |
| 51 | "Now\nadd some\nmulti line stuff", |
| 52 | "This is awesome", |
| 53 | ], |
| 54 | ); |
| 55 | |
| 56 | println!("{table}"); |
| 57 | let expected = " |
| 58 | +----------------+----------------------+------------------------+ |
| 59 | | Header1 | Header2 | Header3 | |
| 60 | +================================================================+ |
| 61 | | This is a text | This is another text | This is the third text | |
| 62 | +----------------+----------------------+------------------------+"; |
| 63 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn add_predicate_single_mixed() { |
| 68 | let mut table = Table::new(); |
| 69 | table |
| 70 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 71 | .add_row(vec![ |
| 72 | "This is a text", |
| 73 | "This is another text", |
| 74 | "This is the third text", |
| 75 | ]) |
| 76 | .add_row_if( |
| 77 | |_, _| false, |
| 78 | &vec!["I won't get displayed", "Me neither", "Same here!"], |
| 79 | ) |
| 80 | .add_row_if( |
| 81 | |_, _| true, |
| 82 | &vec![ |
| 83 | "This is another text", |
| 84 | "Now\nadd some\nmulti line stuff", |
| 85 | "This is awesome", |
| 86 | ], |
| 87 | ); |
| 88 | |
| 89 | println!("{table}"); |
| 90 | let expected = " |
| 91 | +----------------------+----------------------+------------------------+ |
| 92 | | Header1 | Header2 | Header3 | |
| 93 | +======================================================================+ |
| 94 | | This is a text | This is another text | This is the third text | |
| 95 | |----------------------+----------------------+------------------------| |
| 96 | | This is another text | Now | This is awesome | |
| 97 | | | add some | | |
| 98 | | | multi line stuff | | |
| 99 | +----------------------+----------------------+------------------------+"; |
| 100 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn add_predicate_single_wrong_row_count() { |
| 105 | let mut table = Table::new(); |
| 106 | table |
| 107 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 108 | .add_row(vec![ |
| 109 | "This is a text", |
| 110 | "This is another text", |
| 111 | "This is the third text", |
| 112 | ]) |
| 113 | .add_row_if( |
| 114 | |_, row| row.len() == 2, |
| 115 | &vec![ |
| 116 | "This is another text", |
| 117 | "Now\nadd some\nmulti line stuff", |
| 118 | "This is awesome", |
| 119 | ], |
| 120 | ); |
| 121 | |
| 122 | println!("{table}"); |
| 123 | let expected = " |
| 124 | +----------------+----------------------+------------------------+ |
| 125 | | Header1 | Header2 | Header3 | |
| 126 | +================================================================+ |
| 127 | | This is a text | This is another text | This is the third text | |
| 128 | +----------------+----------------------+------------------------+"; |
| 129 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn add_predicate_multi_true() { |
| 134 | let mut table = Table::new(); |
| 135 | let rows = vec![ |
| 136 | Row::from(&vec![ |
| 137 | "This is a text", |
| 138 | "This is another text", |
| 139 | "This is the third text", |
| 140 | ]), |
| 141 | Row::from(&vec![ |
| 142 | "This is another text", |
| 143 | "Now\nadd some\nmulti line stuff", |
| 144 | "This is awesome", |
| 145 | ]), |
| 146 | ]; |
| 147 | |
| 148 | table |
| 149 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 150 | .add_rows_if(|_, _| true, rows); |
| 151 | |
| 152 | println!("{table}"); |
| 153 | let expected = " |
| 154 | +----------------------+----------------------+------------------------+ |
| 155 | | Header1 | Header2 | Header3 | |
| 156 | +======================================================================+ |
| 157 | | This is a text | This is another text | This is the third text | |
| 158 | |----------------------+----------------------+------------------------| |
| 159 | | This is another text | Now | This is awesome | |
| 160 | | | add some | | |
| 161 | | | multi line stuff | | |
| 162 | +----------------------+----------------------+------------------------+"; |
| 163 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn add_predicate_multi_false() { |
| 168 | let mut table = Table::new(); |
| 169 | table |
| 170 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 171 | .add_row(vec![ |
| 172 | "This is a text", |
| 173 | "This is another text", |
| 174 | "This is the third text", |
| 175 | ]) |
| 176 | .add_rows_if( |
| 177 | |_, _| false, |
| 178 | vec![Row::from(&vec![ |
| 179 | "This is another text", |
| 180 | "Now\nadd some\nmulti line stuff", |
| 181 | "This is awesome", |
| 182 | ])], |
| 183 | ); |
| 184 | |
| 185 | println!("{table}"); |
| 186 | let expected = " |
| 187 | +----------------+----------------------+------------------------+ |
| 188 | | Header1 | Header2 | Header3 | |
| 189 | +================================================================+ |
| 190 | | This is a text | This is another text | This is the third text | |
| 191 | +----------------+----------------------+------------------------+"; |
| 192 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn add_predicate_multi_mixed() { |
| 197 | let mut table = Table::new(); |
| 198 | let rows = vec![ |
| 199 | Row::from(&vec![ |
| 200 | "This is a text", |
| 201 | "This is another text", |
| 202 | "This is the third text", |
| 203 | ]), |
| 204 | Row::from(&vec![ |
| 205 | "This is another text", |
| 206 | "Now\nadd some\nmulti line stuff", |
| 207 | "This is awesome", |
| 208 | ]), |
| 209 | ]; |
| 210 | |
| 211 | table |
| 212 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 213 | .add_rows_if(|_, _| true, rows) |
| 214 | .add_rows_if( |
| 215 | |_, _| false, |
| 216 | vec![Row::from(&vec![ |
| 217 | "I won't get displayed", |
| 218 | "Me neither", |
| 219 | "Same here!", |
| 220 | ])], |
| 221 | ); |
| 222 | |
| 223 | println!("{table}"); |
| 224 | let expected = " |
| 225 | +----------------------+----------------------+------------------------+ |
| 226 | | Header1 | Header2 | Header3 | |
| 227 | +======================================================================+ |
| 228 | | This is a text | This is another text | This is the third text | |
| 229 | |----------------------+----------------------+------------------------| |
| 230 | | This is another text | Now | This is awesome | |
| 231 | | | add some | | |
| 232 | | | multi line stuff | | |
| 233 | +----------------------+----------------------+------------------------+"; |
| 234 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | fn add_predicate_multi_wrong_rows_count() { |
| 239 | let mut table = Table::new(); |
| 240 | table |
| 241 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 242 | .add_row(vec![ |
| 243 | "This is a text", |
| 244 | "This is another text", |
| 245 | "This is the third text", |
| 246 | ]) |
| 247 | .add_rows_if( |
| 248 | |_, rows| rows.len() == 2, |
| 249 | vec![Row::from(&vec![ |
| 250 | "This is another text", |
| 251 | "Now\nadd some\nmulti line stuff", |
| 252 | "This is awesome", |
| 253 | ])], |
| 254 | ); |
| 255 | |
| 256 | println!("{table}"); |
| 257 | let expected = " |
| 258 | +----------------+----------------------+------------------------+ |
| 259 | | Header1 | Header2 | Header3 | |
| 260 | +================================================================+ |
| 261 | | This is a text | This is another text | This is the third text | |
| 262 | +----------------+----------------------+------------------------+"; |
| 263 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 264 | } |