Debian Codegraph @

tests/all/utf_8_characters.rs

raw symbols SCIP
1 use comfy_table::*;
2 use pretty_assertions::assert_eq;
3
4 #[test]
5 /// UTF-8 symbols that are longer than a single character are properly handled.
6 /// This means, that comfy-table detects that they're longer than 1 character and styles/arranges
7 /// the table accordingly.
8 fn multi_character_utf8_symbols() {
9 let mut table = Table::new();
10 table
11 .set_header(vec!["Header1", "Header2", "Header3"])
12 .add_row(vec![
13 "This is a text",
14 "This is another text",
15 "This is the third text",
16 ])
17 .add_row(vec![
18 "This is another text",
19 "Now\nadd some\nmulti line stuff",
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 | ✅ |
31 | | add some | |
32 | | multi line stuff | |
33 +----------------------+----------------------+------------------------+";
34 assert_eq!(expected, "\n".to_string() + &table.to_string());
35 }
36
37 #[test]
38 fn multi_character_utf8_word_splitting() {
39 let mut table = Table::new();
40 table
41 .set_width(8)
42 .set_content_arrangement(ContentArrangement::Dynamic)
43 .set_header(vec!["test"])
44 .add_row(vec!["abc✅def"]);
45
46 println!("{table}");
47 let expected = "
48 +------+
49 | test |
50 +======+
51 | abc |
52 | ✅de |
53 | f |
54 +------+";
55 println!("{expected}");
56 assert_eq!(expected, "\n".to_string() + &table.to_string());
57 }
58
59 #[test]
60 fn multi_character_cjk_word_splitting() {
61 let mut table = Table::new();
62 table
63 .set_width(8)
64 .set_content_arrangement(ContentArrangement::Dynamic)
65 .set_header(vec!["test"])
66 .add_row(vec!["abc新年快乐edf"]);
67
68 println!("{table}");
69 let expected = "
70 +------+
71 | test |
72 +======+
73 | abc |
74 | 新年 |
75 | 快乐 |
76 | edf |
77 +------+";
78 println!("{expected}");
79 assert_eq!(expected, "\n".to_string() + &table.to_string());
80 }
81
82 /// Handle emojis that'd joined via the "zero-width joiner" character U+200D and contain variant
83 /// selectors.
84 ///
85 /// Those composite emojis should be handled as a single grapheme and thereby have their width
86 /// calculated based on the grapheme length instead of the individual chars.
87 ///
88 /// This is also a regression test, as previously emojis were split in the middle of the joiner
89 /// sequence, resulting in two different emojis on different lines.
90 #[test]
91 fn zwj_utf8_word_splitting() {
92 let mut table = Table::new();
93 table
94 .set_width(8)
95 .set_content_arrangement(ContentArrangement::Dynamic)
96 .set_header(vec!["test"])
97 .add_row(vec!["ab🙂‍↕️def"]);
98
99 println!("{table}");
100 let expected = "
101 +------+
102 | test |
103 +======+
104 | ab🙂‍↕️ |
105 | def |
106 +------+";
107 println!("{expected}");
108 assert_eq!(expected, "\n".to_string() + &table.to_string());
109 }