Debian Codegraph @

debian/missing-sources/glib-macros/tests/clone.rs

raw
1 // Take a look at the license at the top of the repository in the LICENSE file.
2
3 use std::rc::Rc;
4
5 use glib::clone;
6
7 #[test]
8 fn clone() {
9 let _ = clone!(move || {});
10 let fut = clone!(async move {});
11 drop(fut);
12
13 let x = 1;
14 let _ = clone!(move || {
15 println!("foo {x}");
16 1
17 });
18
19 let x = 1;
20 let y = String::from("123");
21 let v = Rc::new(1);
22 let _ = clone!(
23 #[strong]
24 v,
25 move || {
26 println!("foo {x} {y} {v}");
27 1
28 }
29 );
30
31 let v = Rc::new(1);
32 let _ = clone!(
33 #[strong(rename_to = y)]
34 v,
35 move || {
36 println!("foo {y}");
37 1
38 }
39 );
40
41 let v = Rc::new(1);
42 let _ = clone!(
43 #[strong(rename_to = y)]
44 Rc::strong_count(&v),
45 move || {
46 println!("foo {y}");
47 1
48 }
49 );
50
51 let v = Rc::new(1);
52 let _ = clone!(
53 #[strong]
54 v,
55 move |a: i32, b: &str| {
56 println!("foo {a} {b} {v}");
57 1
58 }
59 );
60
61 let x = 1;
62 let y = String::from("123");
63 let v = Rc::new(1);
64 let fut = clone!(
65 #[strong]
66 v,
67 async move {
68 println!("foo {x} {y} {v}");
69 1
70 }
71 );
72 drop(fut);
73
74 let v = Rc::new(1);
75 let _ = clone!(
76 #[weak]
77 v,
78 move || {
79 println!("foo {v}");
80 }
81 );
82
83 let v = Rc::new(1);
84 let _ = clone!(
85 #[weak]
86 v,
87 #[upgrade_or_else]
88 || None::<i32>,
89 move || {
90 println!("foo {v}");
91 Some(1)
92 }
93 );
94
95 let v = Rc::new(1);
96 let _ = clone!(
97 #[weak]
98 v,
99 #[upgrade_or]
100 None::<i32>,
101 move || {
102 println!("foo {v}");
103 Some(1)
104 }
105 );
106
107 let v = Rc::new(1);
108 let _ = clone!(
109 #[weak]
110 v,
111 #[upgrade_or_default]
112 move || {
113 println!("foo {v}");
114 Some(1)
115 }
116 );
117
118 let v = Rc::new(1);
119 let w = Rc::new(2);
120 let x = Rc::new(3);
121 let _ = clone!(
122 #[weak]
123 v,
124 #[weak]
125 w,
126 #[upgrade_or_else]
127 || {
128 let x: Rc<i32> = x;
129 Some(*x)
130 },
131 move || {
132 println!("foo {v} {w}");
133 Some(1)
134 }
135 );
136
137 let v = Rc::new(1);
138 let _ = clone!(
139 #[weak]
140 v,
141 #[upgrade_or_panic]
142 move || {
143 println!("foo {v}");
144 Some(1)
145 }
146 );
147
148 let v = Rc::new(1);
149 let _ = clone!(
150 #[weak_allow_none]
151 v,
152 move || {
153 println!("foo {}", v.unwrap());
154 Some(1)
155 }
156 );
157
158 let v = "123";
159 let _ = clone!(
160 #[to_owned]
161 v,
162 move || {
163 println!("foo {v}");
164 1
165 }
166 );
167 }
168
169 const TESTS: &[(&str, &str)] = &[
170 ("clone!()", "expected a closure or async block"),
171 (
172 "clone!(#[weak] a, #[weak] b, |x| {})",
173 r#"error: closures need to capture variables by move. Please add the `move` keyword
174 --> test_1.rs:1:88
175 |
176 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[weak] a, #[weak] b, |x| {}); }
177 | ^^^^^^"#,
178 ),
179 (
180 "clone!(#[strong] self, move |x| {})",
181 r#"error: capture attribute for `self` requires usage of the `rename_to` attribute property
182 --> test_2.rs:1:66
183 |
184 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong] self, move |x| {}); }
185 | ^^^^^^^^^"#,
186 ),
187 (
188 "clone!(#[strong] self.v, move |x| {})",
189 r#"error: capture attribute for an expression requires usage of the `rename_to` attribute property
190 --> test_3.rs:1:66
191 |
192 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong] self.v, move |x| {}); }
193 | ^^^^^^^^^"#,
194 ),
195 (
196 "clone!(#[strong(rename_to = x, rename_to = y)] self.v, move || {})",
197 r#"error: multiple `rename_to` properties are not allowed
198 --> test_4.rs:1:90
199 |
200 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong(rename_to = x, rename_to = y)] self.v, move || {}); }
201 | ^^^^^^^^^^^^^"#,
202 ),
203 (
204 "clone!(#[strong(stronk)] self.v, move || {})",
205 r#"error: unsupported capture attribute property `stronk`: only `rename_to` is supported
206 --> test_5.rs:1:75
207 |
208 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong(stronk)] self.v, move || {}); }
209 | ^^^^^^"#,
210 ),
211 (
212 "clone!(#[strong(rename_to = \"a\")] self.v, move || {})",
213 r#"error: expected identifier
214 --> test_6.rs:1:87
215 |
216 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong(rename_to = "a")] self.v, move || {}); }
217 | ^^^"#,
218 ),
219 (
220 "clone!(#[weak] v, #[upgrade_or_else] false, move || {})",
221 r#"error: expected `|`
222 --> test_7.rs:1:96
223 |
224 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[weak] v, #[upgrade_or_else] false, move || {}); }
225 | ^^^^^"#,
226 ),
227 (
228 "clone!(#[weak] v, #[upgrade_or(abort)] move || {})",
229 r#"error: unexpected token in attribute
230 --> test_8.rs:1:89
231 |
232 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[weak] v, #[upgrade_or(abort)] move || {}); }
233 | ^"#,
234 ),
235 (
236 "clone!(#[yolo] v, move || {})",
237 r#"error: unsupported attribute `yolo`: only `strong`, `weak`, `weak_allow_none`, `to_owned`, `upgrade_or`, `upgrade_or_else`, `upgrade_or_default` and `upgrade_or_panic` are supported
238 --> test_9.rs:1:66
239 |
240 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[yolo] v, move || {}); }
241 | ^^^^^^^"#,
242 ),
243 (
244 "clone!(#[watch] v, move || {})",
245 r#"error: watch variable captures are not supported
246 --> test_10.rs:1:66
247 |
248 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[watch] v, move || {}); }
249 | ^^^^^^^^"#,
250 ),
251 (
252 "clone!(#[strong]#[strong] v, move || {})",
253 r#"error: variable capture attributes must be followed by an identifier
254 --> test_11.rs:1:75
255 |
256 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong]#[strong] v, move || {}); }
257 | ^^^^^^^^^"#,
258 ),
259 (
260 "clone!(v, move || {})",
261 r#"error: only closures and async blocks are supported
262 --> test_12.rs:1:66
263 |
264 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(v, move || {}); }
265 | ^"#,
266 ),
267 (
268 "clone!(#[upgrade_or_else] || lol, #[strong] v, move || {println!(\"foo\");})",
269 r#"error: upgrade failure attribute must not be followed by any other attributes. Found 1 more attribute
270 --> test_13.rs:1:93
271 |
272 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[upgrade_or_else] || lol, #[strong] v, move || {println!("foo");}); }
273 | ^^^^^^^^^"#,
274 ),
275 (
276 "clone!(#[upgrade_or_else] |x| lol, #[strong] v, move || {println!(\"foo\");})",
277 r#"error: `upgrade_or_else` closure must not have any parameters
278 --> test_14.rs:1:85
279 |
280 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[upgrade_or_else] |x| lol, #[strong] v, move || {println!("foo");}); }
281 | ^^^^^^^"#,
282 ),
283 (
284 "clone!(#[upgrade_or_else] async || lol, #[strong] v, move || {println!(\"foo\");})",
285 r#"error: `upgrade_or_else` closure needs to be a non-async closure
286 --> test_15.rs:1:85
287 |
288 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[upgrade_or_else] async || lol, #[strong] v, move || {println!("foo");}...
289 | ^^^^^^^^^^^^"#,
290 ),
291 (
292 "clone!(#[upgrade_or_panic] #[strong] v, move || {println!(\"foo\");})",
293 r#"error: upgrade failure attribute must not be followed by any other attributes. Found 1 more attribute
294 --> test_16.rs:1:86
295 |
296 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[upgrade_or_panic] #[strong] v, move || {println!("foo");}); }
297 | ^^^^^^^^^"#,
298 ),
299 (
300 "clone!(#[strong] v, #[upgrade_or_panic] move || {println!(\"foo\");})",
301 r#"error: upgrade failure attribute can only be used together with weak variable captures
302 --> test_17.rs:1:79
303 |
304 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong] v, #[upgrade_or_panic] move || {println!("foo");}); }
305 | ^"#,
306 ),
307 // The async part!
308 (
309 "clone!(#[strong] v, async {println!(\"foo\");})",
310 r#"error: async blocks need to capture variables by move. Please add the `move` keyword
311 --> test_18.rs:1:79
312 |
313 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong] v, async {println!("foo");}); }
314 | ^^^^^^^^^^^^^^^^^^^^^^^^"#,
315 ),
316 (
317 "clone!(#[strong] v, {println!(\"foo\");})",
318 r#"error: only closures and async blocks are supported
319 --> test_19.rs:1:79
320 |
321 1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(#[strong] v, {println!("foo");}); }
322 | ^^^^^^^^^^^^^^^^^^"#,
323 ),
324 ];
325
326 #[test]
327 fn clone_failures() {
328 let t = trybuild2::TestCases::new();
329
330 for (index, (expr, err)) in TESTS.iter().enumerate() {
331 let prefix = "fn main() { use glib::clone; let v = std::rc::Rc::new(1); ";
332 let suffix = "; }";
333 let output = format!("{prefix}{expr}{suffix}");
334
335 t.compile_fail_inline_check_sub(&format!("test_{index}"), &output, err);
336 }
337 }
338
339 const NO_WARNING: &[&str] = &[
340 "let _ = clone!(#[weak] v, #[upgrade_or] (), move || println!(\"{}\", v))",
341 "let _ = clone!(#[weak] v, #[upgrade_or_else] || (), move || println!(\"{}\", v))",
342 "let _ = clone!(#[weak] v, #[upgrade_or_else] || (()), move || println!(\"{}\", v))",
343 "let _ = clone!(#[weak] v, #[upgrade_or_else] || ( () ), move || println!(\"{}\", v))",
344 "let _ = clone!(#[weak] v, #[upgrade_or_else] || ( ), move || println!(\"{}\", v))",
345 ];
346
347 // Ensures that no warning are emitted if the return value is a unit tuple.
348 #[test]
349 fn clone_unit_tuple_return() {
350 let t = trybuild2::TestCases::new();
351
352 for (index, expr) in NO_WARNING.iter().enumerate() {
353 let prefix = "fn main() { use glib::clone; let v = std::rc::Rc::new(1); ";
354 let suffix = "; }";
355 let output = format!("{prefix}{expr}{suffix}");
356
357 t.pass_inline(&format!("test_{index}"), &output);
358 }
359 }