Fix bytearray literals parsing and formatting.

Weirdly enough, we got the parsing wrong for byte literals in expressions (but did okay in constants). But got the formatting wrong in constants (yet did okay for formatting expressions). I've factored out the code in both cases to avoid the duplication that led to this in the first place. Plus added test coverage to make sure this doesn't happen in the future.
This commit is contained in:
KtorZ
2023-01-31 17:19:40 +01:00
parent 0e65fecf69
commit a50b51e086
4 changed files with 106 additions and 100 deletions

View File

@@ -256,3 +256,19 @@ fn test_block_expr() {
assert_fmt(src, expected);
}
#[test]
fn test_format_bytearray_literals() {
let src = indoc! {r#"
const foo = #"ff00"
const bar = #[0, 255]
"#};
let expected = indoc! { r#"
const foo = #"ff00"
const bar = #"00ff"
"#};
assert_fmt(src, expected);
}

View File

@@ -1652,22 +1652,50 @@ fn plain_bytearray_literals() {
fn base16_bytearray_literals() {
let code = indoc! {r#"
pub const my_policy_id = #"00aaff"
pub fn foo() {
my_policy_id == #"00aaff"
}
"#};
assert_definitions(
code,
vec![ast::UntypedDefinition::ModuleConstant(ModuleConstant {
doc: None,
location: Span::new((), 0..34),
public: true,
name: "my_policy_id".to_string(),
annotation: None,
value: Box::new(Constant::ByteArray {
location: Span::new((), 25..34),
bytes: vec![0, 170, 255],
vec![
ast::UntypedDefinition::ModuleConstant(ModuleConstant {
doc: None,
location: Span::new((), 0..34),
public: true,
name: "my_policy_id".to_string(),
annotation: None,
value: Box::new(Constant::ByteArray {
location: Span::new((), 25..34),
bytes: vec![0, 170, 255],
}),
tipo: (),
}),
tipo: (),
})],
ast::UntypedDefinition::Fn(Function {
arguments: vec![],
body: expr::UntypedExpr::BinOp {
location: Span::new((), 55..80),
name: ast::BinOp::Eq,
left: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 55..67),
name: "my_policy_id".to_string(),
}),
right: Box::new(expr::UntypedExpr::ByteArray {
location: Span::new((), 71..80),
bytes: vec![0, 170, 255],
}),
},
doc: None,
location: Span::new((), 36..48),
name: "foo".to_string(),
public: true,
return_annotation: None,
return_type: (),
end_position: 81,
}),
],
)
}