|
| 1 | +use rustpython_ast::{Expr, ExprContext, ExprKind}; |
| 2 | + |
| 3 | +pub fn set_context(expr: Expr, ctx: ExprContext) -> Expr { |
| 4 | + match expr.node { |
| 5 | + ExprKind::Name { id, .. } => Expr { |
| 6 | + node: ExprKind::Name { id, ctx }, |
| 7 | + ..expr |
| 8 | + }, |
| 9 | + ExprKind::Tuple { elts, .. } => Expr { |
| 10 | + node: ExprKind::Tuple { |
| 11 | + elts: elts |
| 12 | + .into_iter() |
| 13 | + .map(|elt| set_context(elt, ctx.clone())) |
| 14 | + .collect(), |
| 15 | + ctx, |
| 16 | + }, |
| 17 | + ..expr |
| 18 | + }, |
| 19 | + ExprKind::List { elts, .. } => Expr { |
| 20 | + node: ExprKind::List { |
| 21 | + elts: elts |
| 22 | + .into_iter() |
| 23 | + .map(|elt| set_context(elt, ctx.clone())) |
| 24 | + .collect(), |
| 25 | + ctx, |
| 26 | + }, |
| 27 | + ..expr |
| 28 | + }, |
| 29 | + ExprKind::Attribute { value, attr, .. } => Expr { |
| 30 | + node: ExprKind::Attribute { value, attr, ctx }, |
| 31 | + ..expr |
| 32 | + }, |
| 33 | + ExprKind::Subscript { value, slice, .. } => Expr { |
| 34 | + node: ExprKind::Subscript { value, slice, ctx }, |
| 35 | + ..expr |
| 36 | + }, |
| 37 | + ExprKind::Starred { value, .. } => Expr { |
| 38 | + node: ExprKind::Starred { |
| 39 | + value: Box::new(set_context(*value, ctx.clone())), |
| 40 | + ctx, |
| 41 | + }, |
| 42 | + ..expr |
| 43 | + }, |
| 44 | + _ => expr, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(test)] |
| 49 | +mod tests { |
| 50 | + use crate::parser::parse_program; |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_assign_name() { |
| 54 | + let source = String::from("x = (1, 2, 3)"); |
| 55 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 56 | + insta::assert_debug_snapshot!(parse_ast); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_assign_tuple() { |
| 61 | + let source = String::from("(x, y) = (1, 2, 3)"); |
| 62 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 63 | + insta::assert_debug_snapshot!(parse_ast); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn test_assign_list() { |
| 68 | + let source = String::from("[x, y] = (1, 2, 3)"); |
| 69 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 70 | + insta::assert_debug_snapshot!(parse_ast); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_assign_attribute() { |
| 75 | + let source = String::from("x.y = (1, 2, 3)"); |
| 76 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 77 | + insta::assert_debug_snapshot!(parse_ast); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_assign_subscript() { |
| 82 | + let source = String::from("x[y] = (1, 2, 3)"); |
| 83 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 84 | + insta::assert_debug_snapshot!(parse_ast); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_assign_starred() { |
| 89 | + let source = String::from("(x, *y) = (1, 2, 3)"); |
| 90 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 91 | + insta::assert_debug_snapshot!(parse_ast); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_assign_for() { |
| 96 | + let source = String::from("for x in (1, 2, 3): pass"); |
| 97 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 98 | + insta::assert_debug_snapshot!(parse_ast); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_assign_list_comp() { |
| 103 | + let source = String::from("x = [y for y in (1, 2, 3)]"); |
| 104 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 105 | + insta::assert_debug_snapshot!(parse_ast); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_assign_set_comp() { |
| 110 | + let source = String::from("x = {y for y in (1, 2, 3)}"); |
| 111 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 112 | + insta::assert_debug_snapshot!(parse_ast); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_assign_with() { |
| 117 | + let source = String::from("with 1 as x: pass"); |
| 118 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 119 | + insta::assert_debug_snapshot!(parse_ast); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_assign_named_expr() { |
| 124 | + let source = String::from("if x:= 1: pass"); |
| 125 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 126 | + insta::assert_debug_snapshot!(parse_ast); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_ann_assign_name() { |
| 131 | + let source = String::from("x: int = 1"); |
| 132 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 133 | + insta::assert_debug_snapshot!(parse_ast); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_aug_assign_name() { |
| 138 | + let source = String::from("x += 1"); |
| 139 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 140 | + insta::assert_debug_snapshot!(parse_ast); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_aug_assign_attribute() { |
| 145 | + let source = String::from("x.y += (1, 2, 3)"); |
| 146 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 147 | + insta::assert_debug_snapshot!(parse_ast); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_aug_assign_subscript() { |
| 152 | + let source = String::from("x[y] += (1, 2, 3)"); |
| 153 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 154 | + insta::assert_debug_snapshot!(parse_ast); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_del_name() { |
| 159 | + let source = String::from("del x"); |
| 160 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 161 | + insta::assert_debug_snapshot!(parse_ast); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn test_del_attribute() { |
| 166 | + let source = String::from("del x.y"); |
| 167 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 168 | + insta::assert_debug_snapshot!(parse_ast); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_del_subscript() { |
| 173 | + let source = String::from("del x[y]"); |
| 174 | + let parse_ast = parse_program(&source, "<test>").unwrap(); |
| 175 | + insta::assert_debug_snapshot!(parse_ast); |
| 176 | + } |
| 177 | +} |
0 commit comments