|
| 1 | +use nera_fmt::{config::FormatConfig, format_source}; |
| 2 | +use nera_syntax::{ |
| 3 | + ast::{BinOp, Expr, UnaryOp}, |
| 4 | + parser::Parser, |
| 5 | +}; |
| 6 | + |
| 7 | +// Newly accepted unary plus must not become list append or lose the grouping |
| 8 | +// of a weaker-binding operand when a program is formatted. |
| 9 | +#[test] |
| 10 | +fn formatted_unary_plus_keeps_the_expression_tree() { |
| 11 | + let source = "-module(unary_plus). |
| 12 | + f(X) -> +(+X). |
| 13 | + g(X) -> +(X + 2) * 2. |
| 14 | + h(X) -> id((+X)), m:f((+X)), F((+X)), {(+X)}, ?IDENTITY((+X)), maybe (+X) end."; |
| 15 | + let formatted = format_source(source, &FormatConfig::default()).unwrap(); |
| 16 | + let result = Parser::new(&formatted).parse_module(); |
| 17 | + assert!(result.errors.is_empty(), "{:?}", result.errors); |
| 18 | + assert!(matches!(&result.module.functions[0].clauses[0].body[0], |
| 19 | + Expr::UnaryOp(UnaryOp::Pos, operand, _) |
| 20 | + if matches!(operand.as_ref(), Expr::UnaryOp(UnaryOp::Pos, inner, _) |
| 21 | + if matches!(inner.as_ref(), Expr::Variable(..))))); |
| 22 | + let Expr::BinOp(BinOp::Mul, positive, _, _) = &result.module.functions[1].clauses[0].body[0] |
| 23 | + else { |
| 24 | + panic!("lost outer multiplication: {formatted}"); |
| 25 | + }; |
| 26 | + assert!( |
| 27 | + matches!(positive.as_ref(), Expr::UnaryOp(UnaryOp::Pos, operand, _) |
| 28 | + if matches!(operand.as_ref(), Expr::BinOp(BinOp::Add, ..))) |
| 29 | + ); |
| 30 | + let body = &result.module.functions[2].clauses[0].body; |
| 31 | + for call in &body[..3] { |
| 32 | + assert!(matches!(call, Expr::Call(_, args, _) |
| 33 | + if matches!(args.as_slice(), [Expr::UnaryOp(UnaryOp::Pos, _, _)]))); |
| 34 | + } |
| 35 | + assert!(matches!(&body[3], Expr::Tuple(elements, _) |
| 36 | + if matches!(elements.as_slice(), [Expr::UnaryOp(UnaryOp::Pos, _, _)]))); |
| 37 | + assert!(matches!(&body[4], Expr::MacroCall { args: Some(args), .. } |
| 38 | + if matches!(args.as_slice(), [Expr::UnaryOp(UnaryOp::Pos, _, _)]))); |
| 39 | + assert!(matches!(&body[5], Expr::Maybe { .. })); |
| 40 | + assert_eq!( |
| 41 | + formatted, |
| 42 | + format_source(&formatted, &FormatConfig::default()).unwrap() |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +// Numeric normalization must keep the whole consumed spelling, including the |
| 47 | +// closing parenthesis, in expressions and expression-derived match patterns. |
| 48 | +#[test] |
| 49 | +fn formatted_parenthesized_positive_literals_keep_complete_spans() { |
| 50 | + for literal in ["1", "9223372036854775808", "1.5"] { |
| 51 | + let source = format!( |
| 52 | + "-module(unary_plus). f() -> +({literal}). |
| 53 | + g() -> +({literal}) = id({literal}). |
| 54 | + h() -> <<+({literal})/float>>." |
| 55 | + ); |
| 56 | + let formatted = format_source(&source, &FormatConfig::default()).unwrap(); |
| 57 | + let result = Parser::new(&formatted).parse_module(); |
| 58 | + assert!(result.errors.is_empty(), "{:?}: {formatted}", result.errors); |
| 59 | + assert!(matches!( |
| 60 | + &result.module.functions[0].clauses[0].body[0], |
| 61 | + Expr::Integer(..) | Expr::BigInteger(..) | Expr::Float(..) |
| 62 | + )); |
| 63 | + assert!(matches!( |
| 64 | + &result.module.functions[1].clauses[0].body[0], |
| 65 | + Expr::Match(..) |
| 66 | + )); |
| 67 | + assert_eq!( |
| 68 | + formatted, |
| 69 | + format_source(&formatted, &FormatConfig::default()).unwrap() |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Construction and pattern segments share size formatting; both must preserve |
| 75 | +// the parentheses required around a signed size, including comprehension sites. |
| 76 | +#[test] |
| 77 | +fn formatted_positive_binary_sizes_keep_required_grouping() { |
| 78 | + let source = "-module(unary_plus). |
| 79 | + f() -> <<1:(+8)>>. |
| 80 | + g(<<X:(+8)>>) -> X. |
| 81 | + h() -> << <<X:(+8)>> || <<X:(+8)>> <= <<1, 2>> >>."; |
| 82 | + let formatted = format_source(source, &FormatConfig::default()).unwrap(); |
| 83 | + let result = Parser::new(&formatted).parse_module(); |
| 84 | + assert!(result.errors.is_empty(), "{:?}: {formatted}", result.errors); |
| 85 | + assert_eq!(result.module.functions.len(), 3); |
| 86 | + assert_eq!( |
| 87 | + formatted, |
| 88 | + format_source(&formatted, &FormatConfig::default()).unwrap() |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +// A folded number may retain compound signs in its spelling. Such spellings |
| 93 | +// are expressions, not bare binary values, even though the AST is a literal. |
| 94 | +#[test] |
| 95 | +fn formatted_compound_signed_binary_values_keep_grouping() { |
| 96 | + let source = "-module(unary_plus). f() -> <<(+-0.0)/float, (+-1):8, (+ +1):8>>."; |
| 97 | + let formatted = format_source(source, &FormatConfig::default()).unwrap(); |
| 98 | + let result = Parser::new(&formatted).parse_module(); |
| 99 | + assert!(result.errors.is_empty(), "{:?}: {formatted}", result.errors); |
| 100 | + let Expr::Binary(segments, _) = &result.module.functions[0].clauses[0].body[0] else { |
| 101 | + panic!("lost binary construction: {formatted}"); |
| 102 | + }; |
| 103 | + assert_eq!(segments.len(), 3); |
| 104 | + assert!(matches!(&segments[0].value, Expr::Float(value, _) if value.is_sign_negative())); |
| 105 | + assert!(matches!(&segments[1].value, Expr::Integer(-1, _))); |
| 106 | + assert!(matches!(&segments[2].value, Expr::Integer(1, _))); |
| 107 | + assert_eq!( |
| 108 | + formatted, |
| 109 | + format_source(&formatted, &FormatConfig::default()).unwrap() |
| 110 | + ); |
| 111 | +} |
0 commit comments