Skip to content

Commit

Permalink
printer: Disambiguate ternary in comprehension condition
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Sep 8, 2020
1 parent 685e0d0 commit 9710b82
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/visitors/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ fn format_subscript(sub: &Subscript) -> String {

fn format_comp(comp: &ComprehensionChunk) -> String {
match *comp {
ComprehensionChunk::If { ref cond } => format!("if {}", format_expr(cond)),
ComprehensionChunk::If { ref cond } => format!("if ({})", format_expr(cond)),
ComprehensionChunk::For { async, ref item, ref iterator } => format!("{}for {} in {}",
if async { "async " } else { "" },
comma_join(item.iter().map(format_expr)),
Expand Down Expand Up @@ -671,3 +671,33 @@ fn format_import(imp: &Import) -> String {
}
s
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ternary_in_comp_if() {
let e = Expression::ListComp(
Box::new(SetItem::Unique(Expression::Name("a".to_string()))),
vec![
ComprehensionChunk::For {
async: false,
item: vec![Expression::Name("a".to_string())],
iterator: Expression::Name("L".to_string()),
},
ComprehensionChunk::If {
cond: Expression::Ternary(
Box::new(Expression::Call(
Box::new(Expression::Name("f".to_string())),
vec![Argument::Positional(Expression::Name("a".to_string()))],
)),
Box::new(Expression::Name("a".to_string())),
Box::new(Expression::None),
),
},
],
);
assert_eq!(&format_expr(&e), "[a for a in L if ((f(a)) if (a) else (None))]");
}
}

0 comments on commit 9710b82

Please sign in to comment.