Debian Codegraph @

debian/rust-vendor/displaydoc/tests/happy.rs

raw
1 use displaydoc::Display;
2
3 #[cfg(feature = "std")]
4 use std::path::PathBuf;
5
6 #[derive(Display)]
7 /// Just a basic struct {thing}
8 struct HappyStruct {
9 thing: &'static str,
10 }
11
12 #[derive(Display)]
13 #[ignore_extra_doc_attributes]
14 /// Just a basic struct {thing}
15 /// and this line should not get ignored
16 struct HappyStruct2 {
17 thing: &'static str,
18 }
19
20 #[derive(Display)]
21 /// Really fancy first line with thing: {thing}
22 /// Really cool second line
23 struct HappyMultiLine {
24 thing: &'static str,
25 }
26
27 #[derive(Display)]
28 #[ignore_extra_doc_attributes]
29 /// multi
30 /// line
31 ///
32 /// new paragraph should be ignored
33 struct HappyMultilineWithIgnore;
34
35 #[derive(Display)]
36 enum MixedBlockAndLineComments {
37 /**
38 * hello
39 * block
40 * comment
41 */
42 /// line comment
43 BlockFirst,
44 /// line comment
45 /**
46 * hello
47 * block
48 * comment
49 */
50 LineFirst,
51 /**
52 * block
53 * comment
54 */
55 /**
56 * block
57 * comment2
58 */
59 DoubleBlock,
60 }
61
62 #[derive(Display)]
63 enum Happy {
64 /// I really like Variant1
65 Variant1,
66 /// Variant2 is pretty swell 2
67 Variant2,
68 /// Variant3 is okay {sometimes}
69 Variant3 { sometimes: &'static str },
70 /**
71 * Variant4 wants to have a lot of lines
72 *
73 * Lets see how this works out for it
74 */
75 Variant4,
76 /// Variant5 has a parameter {0} and some regular comments
77 // A regular comment that won't get picked
78 Variant5(u32),
79
80 /// The path {0}
81 #[cfg(feature = "std")]
82 Variant6(PathBuf),
83
84 /// These docs are ignored
85 #[displaydoc("Variant7 has a parameter {0} and uses #[displaydoc]")]
86 /// These docs are also ignored
87 Variant7(u32),
88 }
89
90 // Used for testing indented doc comments
91 mod inner_mod {
92 use super::Display;
93
94 #[derive(Display)]
95 pub enum InnerHappy {
96 /// I really like Variant1
97 Variant1,
98 /// Variant2 is pretty swell 2
99 Variant2,
100 /// Variant3 is okay {sometimes}
101 Variant3 { sometimes: &'static str },
102 /**
103 * Variant4 wants to have a lot of lines
104 *
105 * Lets see how this works out for it
106 */
107 Variant4,
108 /// Variant5 has a parameter {0} and some regular comments
109 // A regular comment that won't get picked
110 Variant5(u32),
111
112 /** what happens if we
113 * put text on the first line?
114 */
115 Variant6,
116
117 /**
118 what happens if we don't use *?
119 */
120 Variant7,
121
122 /**
123 *
124 * what about extra new lines?
125 */
126 Variant8,
127 }
128 }
129
130 fn assert_display<T: std::fmt::Display>(input: T, expected: &'static str) {
131 let out = format!("{}", input);
132 assert_eq!(expected, out);
133 }
134
135 #[test]
136 fn does_it_print() {
137 assert_display(Happy::Variant1, "I really like Variant1");
138 assert_display(Happy::Variant2, "Variant2 is pretty swell 2");
139 assert_display(Happy::Variant3 { sometimes: "hi" }, "Variant3 is okay hi");
140 assert_display(
141 Happy::Variant4,
142 "Variant4 wants to have a lot of lines\n\nLets see how this works out for it",
143 );
144 assert_display(
145 Happy::Variant5(2),
146 "Variant5 has a parameter 2 and some regular comments",
147 );
148 assert_display(
149 Happy::Variant7(2),
150 "Variant7 has a parameter 2 and uses #[displaydoc]",
151 );
152 assert_display(HappyStruct { thing: "hi" }, "Just a basic struct hi");
153
154 assert_display(
155 HappyStruct2 { thing: "hi2" },
156 "Just a basic struct hi2 and this line should not get ignored",
157 );
158
159 assert_display(
160 HappyMultiLine { thing: "rust" },
161 "Really fancy first line with thing: rust Really cool second line",
162 );
163
164 assert_display(HappyMultilineWithIgnore, "multi line");
165
166 assert_display(
167 MixedBlockAndLineComments::BlockFirst,
168 "hello\nblock\ncomment line comment",
169 );
170 assert_display(
171 MixedBlockAndLineComments::LineFirst,
172 "line comment hello\nblock\ncomment",
173 );
174 assert_display(
175 MixedBlockAndLineComments::DoubleBlock,
176 "block\ncomment block\ncomment2",
177 );
178
179 assert_display(inner_mod::InnerHappy::Variant1, "I really like Variant1");
180 assert_display(
181 inner_mod::InnerHappy::Variant2,
182 "Variant2 is pretty swell 2",
183 );
184 assert_display(
185 inner_mod::InnerHappy::Variant3 { sometimes: "hi" },
186 "Variant3 is okay hi",
187 );
188 assert_display(
189 inner_mod::InnerHappy::Variant4,
190 "Variant4 wants to have a lot of lines\n\nLets see how this works out for it",
191 );
192 assert_display(
193 inner_mod::InnerHappy::Variant5(2),
194 "Variant5 has a parameter 2 and some regular comments",
195 );
196 assert_display(
197 inner_mod::InnerHappy::Variant6,
198 "what happens if we\nput text on the first line?",
199 );
200 assert_display(
201 inner_mod::InnerHappy::Variant7,
202 "what happens if we don\'t use *?",
203 );
204 assert_display(
205 inner_mod::InnerHappy::Variant8,
206 "what about extra new lines?",
207 );
208 }
209
210 #[test]
211 #[cfg(feature = "std")]
212 fn does_it_print_path() {
213 assert_display(
214 Happy::Variant6(PathBuf::from("/var/log/happy")),
215 "The path /var/log/happy",
216 );
217 }