Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1 | extern crate syn; |
| 2 | use syn::*; |
| 3 | |
| 4 | macro_rules! assert_let { |
| 5 | ($p:pat = $e:expr) => { |
| 6 | assert_let!($p = $e; {}) |
| 7 | }; |
| 8 | |
| 9 | ($p:pat = $e:expr; $body:block) => { |
| 10 | if let $p = $e |
| 11 | $body |
| 12 | else { |
| 13 | panic!("Expected to match {} but got {:?}", stringify!($p), $e) |
| 14 | } |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | #[cfg(feature = "full")] |
| 20 | fn test_catch_expr() { |
| 21 | // Taken from tests/rust/src/test/run-pass/catch-expr.rs |
| 22 | let raw = r#" |
| 23 | struct catch {} |
| 24 | |
| 25 | pub fn main() { |
| 26 | let catch_result = do catch { |
| 27 | let x = 5; |
| 28 | x |
| 29 | }; |
| 30 | assert_eq!(catch_result, 5); |
| 31 | |
| 32 | let mut catch = true; |
| 33 | while catch { catch = false; } |
| 34 | assert_eq!(catch, false); |
| 35 | |
| 36 | catch = if catch { false } else { true }; |
| 37 | assert_eq!(catch, true); |
| 38 | |
| 39 | match catch { |
| 40 | _ => {} |
| 41 | }; |
| 42 | |
| 43 | let catch_err = do catch { |
| 44 | Err(22)?; |
| 45 | Ok(1) |
| 46 | }; |
| 47 | assert_eq!(catch_err, Err(22)); |
| 48 | |
| 49 | let catch_okay: Result<i32, i32> = do catch { |
| 50 | if false { Err(25)?; } |
| 51 | Ok::<(), i32>(())?; |
| 52 | Ok(28) |
| 53 | }; |
| 54 | assert_eq!(catch_okay, Ok(28)); |
| 55 | |
| 56 | let catch_from_loop: Result<i32, i32> = do catch { |
| 57 | for i in 0..10 { |
| 58 | if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; } |
| 59 | } |
| 60 | Ok(22) |
| 61 | }; |
| 62 | assert_eq!(catch_from_loop, Err(5)); |
| 63 | |
| 64 | let cfg_init; |
| 65 | let _res: Result<(), ()> = do catch { |
| 66 | cfg_init = 5; |
| 67 | Ok(()) |
| 68 | }; |
| 69 | assert_eq!(cfg_init, 5); |
| 70 | |
| 71 | let cfg_init_2; |
| 72 | let _res: Result<(), ()> = do catch { |
| 73 | cfg_init_2 = 6; |
| 74 | Err(())?; |
| 75 | Ok(()) |
| 76 | }; |
| 77 | assert_eq!(cfg_init_2, 6); |
| 78 | |
| 79 | let my_string = "test".to_string(); |
| 80 | let res: Result<&str, ()> = do catch { |
| 81 | Ok(&my_string) |
| 82 | }; |
| 83 | assert_eq!(res, Ok("test")); |
| 84 | } |
| 85 | "#; |
| 86 | |
| 87 | let actual = parse_crate(raw).unwrap(); |
| 88 | |
| 89 | assert_eq!(&actual.items[0].ident, "catch"); |
| 90 | |
| 91 | assert_let!(ItemKind::Struct(..) = actual.items[0].node); |
| 92 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 93 | assert_let!(Item { node: ItemKind::Fn(ItemFn { ref block, .. }), .. } = actual.items[1]; { |
| 94 | assert_let!(Stmt::Local(ref local) = block.stmts[0]; { |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 95 | assert_let!(Local { init: Some(ref init_expr), .. } = **local; { |
| 96 | assert_let!(Expr { node: ExprKind::Catch(..), .. } = **init_expr); |
| 97 | }); |
| 98 | }); |
| 99 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 100 | assert_let!(Stmt::Local(ref local) = block.stmts[2]; { |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 101 | assert_let!(Pat::Ident(BindingMode::ByValue(Mutability::Mutable), ref ident, None) = *local.pat; { |
| 102 | assert_eq!(ident, "catch"); |
| 103 | }); |
| 104 | }); |
| 105 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 106 | assert_let!(Stmt::Expr(ref expr) = block.stmts[3]; { |
| 107 | assert_let!(Expr { node: ExprKind::While(ExprWhile { ref cond, .. }), .. } = **expr; { |
| 108 | assert_let!(Expr { node: ExprKind::Path(ExprPath { qself: None, ref path }), .. } = **cond; { |
| 109 | assert_eq!(*path, "catch".into()); |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 110 | }); |
| 111 | }); |
| 112 | }); |
| 113 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 114 | assert_let!(Stmt::Semi(ref expr) = block.stmts[5]; { |
| 115 | assert_let!(Expr { node: ExprKind::Assign(ExprAssign { ref left, ref right }), .. } = **expr; { |
| 116 | assert_let!(Expr { node: ExprKind::Path(ExprPath { qself: None, ref path }), .. } = **left; { |
| 117 | assert_eq!(*path, "catch".into()); |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 118 | }); |
| 119 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 120 | assert_let!(Expr { node: ExprKind::If(ExprIf { ref cond, .. }), .. } = **right; { |
| 121 | assert_let!(Expr { node: ExprKind::Path(ExprPath { qself: None, ref path }), .. } = **cond; { |
| 122 | assert_eq!(*path, "catch".into()); |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 123 | }); |
| 124 | }); |
| 125 | }); |
| 126 | }); |
| 127 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 128 | assert_let!(Stmt::Semi(ref expr) = block.stmts[7]; { |
| 129 | assert_let!(Expr { node: ExprKind::Match(ExprMatch { ref expr, .. }), .. } = **expr; { |
| 130 | assert_let!(Expr { node: ExprKind::Path(ExprPath { qself: None, ref path }), .. } = **expr; { |
| 131 | assert_eq!(*path, "catch".into()); |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 132 | }); |
| 133 | }); |
| 134 | }); |
| 135 | }); |
| 136 | } |