| 1 |
// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 |
|
| 3 |
const TESTS: &[(&str, &str)] = &[ |
| 4 |
("closure!()", "expected a closure"), |
| 5 |
( |
| 6 |
"closure!(#[weak] a, #[weak] b, |x| {})", |
| 7 |
r#"error: closures need to capture variables by move. Please add the `move` keyword |
| 8 |
--> test_1.rs:1:92 |
| 9 |
| |
| 10 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[weak] a, #[weak] b, |x| {}); } |
| 11 |
| ^^^^^^"#, |
| 12 |
), |
| 13 |
( |
| 14 |
"closure!(#[strong] self, move |x| {})", |
| 15 |
r#"error: capture attribute for `self` requires usage of the `rename_to` attribute property |
| 16 |
--> test_2.rs:1:70 |
| 17 |
| |
| 18 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong] self, move |x| {}); } |
| 19 |
| ^^^^^^^^^"#, |
| 20 |
), |
| 21 |
( |
| 22 |
"closure!(#[strong] self.v, move |x| {})", |
| 23 |
r#"error: capture attribute for an expression requires usage of the `rename_to` attribute property |
| 24 |
--> test_3.rs:1:70 |
| 25 |
| |
| 26 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong] self.v, move |x| {}); } |
| 27 |
| ^^^^^^^^^"#, |
| 28 |
), |
| 29 |
( |
| 30 |
"closure!(#[strong(rename_to = x, rename_to = y)] self.v, move || {})", |
| 31 |
r#"error: multiple `rename_to` properties are not allowed |
| 32 |
--> test_4.rs:1:94 |
| 33 |
| |
| 34 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong(rename_to = x, rename_to = y)] self.v, move || {}); } |
| 35 |
| ^^^^^^^^^^^^^"#, |
| 36 |
), |
| 37 |
( |
| 38 |
"closure!(#[strong(stronk)] self.v, move || {})", |
| 39 |
r#"error: unsupported capture attribute property `stronk`: only `rename_to` is supported |
| 40 |
--> test_5.rs:1:79 |
| 41 |
| |
| 42 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong(stronk)] self.v, move || {}); } |
| 43 |
| ^^^^^^"#, |
| 44 |
), |
| 45 |
( |
| 46 |
"closure!(#[strong(rename_to = \"a\")] self.v, move || {})", |
| 47 |
r#"error: expected identifier |
| 48 |
--> test_6.rs:1:91 |
| 49 |
| |
| 50 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong(rename_to = "a")] self.v, move || {}); } |
| 51 |
| ^^^"#, |
| 52 |
), |
| 53 |
( |
| 54 |
"closure!(#[weak] v, #[upgrade_or_else] false, move || {})", |
| 55 |
r#"error: expected `|` |
| 56 |
--> test_7.rs:1:100 |
| 57 |
| |
| 58 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[weak] v, #[upgrade_or_else] false, move || {}); } |
| 59 |
| ^^^^^"#, |
| 60 |
), |
| 61 |
( |
| 62 |
"closure!(#[weak] v, #[upgrade_or(abort)] move || {})", |
| 63 |
r#"error: unexpected token in attribute |
| 64 |
--> test_8.rs:1:93 |
| 65 |
| |
| 66 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[weak] v, #[upgrade_or(abort)] move || {}); } |
| 67 |
| ^"#, |
| 68 |
), |
| 69 |
( |
| 70 |
"closure!(#[yolo] v, move || {})", |
| 71 |
r#"error: unsupported attribute `yolo`: only `watch`, `strong`, `weak`, `weak_allow_none`, `to_owned`, `upgrade_or`, `upgrade_or_else`, `upgrade_or_default` and `upgrade_or_panic` are supported |
| 72 |
--> test_9.rs:1:70 |
| 73 |
| |
| 74 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[yolo] v, move || {}); } |
| 75 |
| ^^^^^^^"#, |
| 76 |
), |
| 77 |
( |
| 78 |
"closure!(#[watch] v, #[watch] v, move || {})", |
| 79 |
r#"error: only one `watch` capture is allowed per closure |
| 80 |
--> test_10.rs:1:82 |
| 81 |
| |
| 82 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[watch] v, #[watch] v, move || {}); } |
| 83 |
| ^^^^^^^^"#, |
| 84 |
), |
| 85 |
( |
| 86 |
"closure!(#[strong]#[strong] v, move || {})", |
| 87 |
r#"error: variable capture attributes must be followed by an identifier |
| 88 |
--> test_11.rs:1:79 |
| 89 |
| |
| 90 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong]#[strong] v, move || {}); } |
| 91 |
| ^^^^^^^^^"#, |
| 92 |
), |
| 93 |
( |
| 94 |
"closure!(v, move || {})", |
| 95 |
r#"error: expected `|` |
| 96 |
--> test_12.rs:1:70 |
| 97 |
| |
| 98 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(v, move || {}); } |
| 99 |
| ^"#, |
| 100 |
), |
| 101 |
( |
| 102 |
"closure!(#[upgrade_or_else] || lol, #[strong] v, move || {println!(\"foo\");})", |
| 103 |
r#"error: upgrade failure attribute must not be followed by any other attributes. Found 1 more attribute |
| 104 |
--> test_13.rs:1:97 |
| 105 |
| |
| 106 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[upgrade_or_else] || lol, #[strong] v, move || {println!("foo");}); } |
| 107 |
| ^^^^^^^^^"#, |
| 108 |
), |
| 109 |
( |
| 110 |
"closure!(#[upgrade_or_else] |x| lol, #[strong] v, move || {println!(\"foo\");})", |
| 111 |
r#"error: `upgrade_or_else` closure must not have any parameters |
| 112 |
--> test_14.rs:1:89 |
| 113 |
| |
| 114 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[upgrade_or_else] |x| lol, #[strong] v, move || {println!("foo");}); } |
| 115 |
| ^^^^^^^"#, |
| 116 |
), |
| 117 |
( |
| 118 |
"closure!(#[upgrade_or_else] async || lol, #[strong] v, move || {println!(\"foo\");})", |
| 119 |
r#"error: `upgrade_or_else` closure needs to be a non-async closure |
| 120 |
--> test_15.rs:1:89 |
| 121 |
| |
| 122 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[upgrade_or_else] async || lol, #[strong] v, move || {println!("foo... |
| 123 |
| ^^^^^^^^^^^^"#, |
| 124 |
), |
| 125 |
( |
| 126 |
"closure!(#[upgrade_or_panic] #[strong] v, move || {println!(\"foo\");})", |
| 127 |
r#"error: upgrade failure attribute must not be followed by any other attributes. Found 1 more attribute |
| 128 |
--> test_16.rs:1:90 |
| 129 |
| |
| 130 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[upgrade_or_panic] #[strong] v, move || {println!("foo");}); } |
| 131 |
| ^^^^^^^^^"#, |
| 132 |
), |
| 133 |
( |
| 134 |
"closure!(#[strong] v, async {println!(\"foo\");})", |
| 135 |
r#"error: expected `|` |
| 136 |
--> test_17.rs:1:89 |
| 137 |
| |
| 138 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong] v, async {println!("foo");}); } |
| 139 |
| ^"#, |
| 140 |
), |
| 141 |
( |
| 142 |
"closure!(#[strong] v, {println!(\"foo\");})", |
| 143 |
r#"error: expected `|` |
| 144 |
--> test_18.rs:1:83 |
| 145 |
| |
| 146 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong] v, {println!("foo");}); } |
| 147 |
| ^"#, |
| 148 |
), |
| 149 |
( |
| 150 |
"closure!(#[strong] v, #[upgrade_or_panic] move || {println!(\"foo\");})", |
| 151 |
r#"error: upgrade failure attribute can only be used together with weak variable captures |
| 152 |
--> test_19.rs:1:83 |
| 153 |
| |
| 154 |
1 | fn main() { use glib::closure; let v = std::rc::Rc::new(1); closure!(#[strong] v, #[upgrade_or_panic] move || {println!("foo");}); } |
| 155 |
| ^"#, |
| 156 |
), |
| 157 |
]; |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn closure_failures() { |
| 161 |
let t = trybuild2::TestCases::new(); |
| 162 |
|
| 163 |
for (index, (expr, err)) in TESTS.iter().enumerate() { |
| 164 |
let prefix = "fn main() { use glib::closure; let v = std::rc::Rc::new(1); "; |
| 165 |
let suffix = "; }"; |
| 166 |
let output = format!("{prefix}{expr}{suffix}"); |
| 167 |
|
| 168 |
t.compile_fail_inline_check_sub(&format!("test_{index}"), &output, err); |
| 169 |
} |
| 170 |
} |