1 use textwrap::word_separators::{AsciiSpace, WordSeparator};
2 use textwrap::word_splitters::{HyphenSplitter, NoHyphenation, WordSplitter};
3 use textwrap::wrap_algorithms::{FirstFit, WrapAlgorithm};
4 use textwrap::Options;
5
6 /// Cleaned up type name.
type_name<T: ?Sized>(_val: &T) -> String7 fn type_name<T: ?Sized>(_val: &T) -> String {
8 std::any::type_name::<T>().replace("alloc::boxed::Box", "Box")
9 }
10
11 #[test]
12 #[cfg(not(feature = "smawk"))]
13 #[cfg(not(feature = "unicode-linebreak"))]
static_hyphensplitter()14 fn static_hyphensplitter() {
15 // Inferring the full type.
16 let options = Options::new(10);
17 assert_eq!(
18 type_name(&options),
19 format!(
20 "textwrap::Options<{}, {}, {}>",
21 "textwrap::wrap_algorithms::FirstFit",
22 "textwrap::word_separators::AsciiSpace",
23 "textwrap::word_splitters::HyphenSplitter"
24 )
25 );
26
27 // Inferring part of the type.
28 let options: Options<_, _, HyphenSplitter> = Options::new(10);
29 assert_eq!(
30 type_name(&options),
31 format!(
32 "textwrap::Options<{}, {}, {}>",
33 "textwrap::wrap_algorithms::FirstFit",
34 "textwrap::word_separators::AsciiSpace",
35 "textwrap::word_splitters::HyphenSplitter"
36 )
37 );
38
39 // Explicitly making all parameters inferred.
40 let options: Options<_, _, _> = Options::new(10);
41 assert_eq!(
42 type_name(&options),
43 format!(
44 "textwrap::Options<{}, {}, {}>",
45 "textwrap::wrap_algorithms::FirstFit",
46 "textwrap::word_separators::AsciiSpace",
47 "textwrap::word_splitters::HyphenSplitter"
48 )
49 );
50 }
51
52 #[test]
box_static_nohyphenation()53 fn box_static_nohyphenation() {
54 // Inferred static type.
55 let options = Options::new(10)
56 .wrap_algorithm(Box::new(FirstFit))
57 .word_splitter(Box::new(NoHyphenation))
58 .word_separator(Box::new(AsciiSpace));
59 assert_eq!(
60 type_name(&options),
61 format!(
62 "textwrap::Options<{}, {}, {}>",
63 "Box<textwrap::wrap_algorithms::FirstFit>",
64 "Box<textwrap::word_separators::AsciiSpace>",
65 "Box<textwrap::word_splitters::NoHyphenation>"
66 )
67 );
68 }
69
70 #[test]
box_dyn_wordsplitter()71 fn box_dyn_wordsplitter() {
72 // Inferred dynamic type due to default type parameter.
73 let options = Options::new(10)
74 .wrap_algorithm(Box::new(FirstFit) as Box<dyn WrapAlgorithm>)
75 .word_splitter(Box::new(HyphenSplitter) as Box<dyn WordSplitter>)
76 .word_separator(Box::new(AsciiSpace) as Box<dyn WordSeparator>);
77 assert_eq!(
78 type_name(&options),
79 format!(
80 "textwrap::Options<{}, {}, {}>",
81 "Box<dyn textwrap::wrap_algorithms::WrapAlgorithm>",
82 "Box<dyn textwrap::word_separators::WordSeparator>",
83 "Box<dyn textwrap::word_splitters::WordSplitter>"
84 )
85 );
86 }
87