Debian Codegraph @

src/ansi/console_tests.rs

raw symbols SCIP
1 // This file contains some unit tests copied from the `console` project:
2 // https://github.com/mitsuhiko/console
3 //
4 // The MIT License (MIT)
5
6 // Copyright (c) 2017 Armin Ronacher <[email protected]>
7
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17
18 #[cfg(test)]
19 mod tests {
20 use console::{self, style};
21
22 use crate::ansi::{measure_text_width, truncate_str};
23
24 #[test]
25 fn test_text_width() {
26 let s = style("foo")
27 .red()
28 .on_black()
29 .bold()
30 .force_styling(true)
31 .to_string();
32 assert_eq!(measure_text_width(&s), 3);
33 }
34
35 #[test]
36 fn test_truncate_str() {
37 let s = format!("foo {}", style("bar").red().force_styling(true));
38 assert_eq!(
39 &truncate_str(&s, 5, ""),
40 &format!("foo {}", style("b").red().force_styling(true))
41 );
42 let s = format!("foo {}", style("bar").red().force_styling(true));
43 // DED: I'm changing this test assertion: delta does not move `!` inside the styled region.
44 // assert_eq!(
45 // &truncate_str(&s, 5, "!"),
46 // &format!("foo {}", style("!").red().force_styling(true))
47 // );
48 assert_eq!(
49 &truncate_str(&s, 5, "!"),
50 &format!("foo {}!", style("").red().force_styling(true))
51 );
52 let s = format!("foo {} baz", style("bar").red().force_styling(true));
53 assert_eq!(
54 &truncate_str(&s, 10, "..."),
55 &format!("foo {}...", style("bar").red().force_styling(true))
56 );
57 // `バ`(width = 2) will be truncate to 1, we use space to fill
58 let s = format!("foo {}", style("バー").red().force_styling(true));
59 assert_eq!(
60 &truncate_str(&s, 5, ""),
61 &format!("foo {}", style(" ").red().force_styling(true))
62 );
63 let s = format!("foo {}", style("バー").red().force_styling(true));
64 assert_eq!(
65 &truncate_str(&s, 6, ""),
66 &format!("foo {}", style("バ").red().force_styling(true))
67 );
68 }
69
70 #[test]
71 fn test_truncate_str_no_ansi() {
72 assert_eq!(&truncate_str("foo bar", 5, ""), "foo b");
73 assert_eq!(&truncate_str("foo bar", 5, "!"), "foo !");
74 assert_eq!(&truncate_str("foo bar baz", 10, "..."), "foo bar...");
75 }
76 }