Skip to content

Commit

Permalink
Fix storage blocks (FuelLabs#2618)
Browse files Browse the repository at this point in the history
* fix storage blocks

* organize module

* fix nit

Co-authored-by: Joshua Batty <[email protected]>
Co-authored-by: mitchmindtree <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2022
1 parent f7b346e commit 1549bab
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::fmt::Write;
use sway_ast::{keywords::Token, token::Delimiter, ItemStorage, StorageField};
use sway_types::Spanned;

#[cfg(test)]
mod tests;

impl Format for ItemStorage {
fn format(
&self,
Expand Down
166 changes: 166 additions & 0 deletions sway-fmt-v2/src/items/item_storage/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use crate::{Format, Formatter};
use forc_util::{println_green, println_red};
use paste::paste;
use prettydiff::{basic::DiffOp, diff_lines};
use sway_ast::ItemStorage;
use sway_parse::{handler::Handler, *};

fn format_code(input: &str) -> String {
let mut formatter: Formatter = Default::default();
let input_arc = std::sync::Arc::from(input);
let token_stream = lex(&input_arc, 0, input.len(), None).unwrap();
let handler = Handler::default();
let mut parser = Parser::new(&token_stream, &handler);
let expression: ItemStorage = parser.parse().unwrap();

let mut buf = Default::default();
expression.format(&mut buf, &mut formatter).unwrap();

buf
}

macro_rules! fmt_test {
($scope:ident $desired_output:expr, $($name:ident $y:expr),+) => {
fmt_test_inner!($scope $desired_output,
$($name $y)+
,
remove_trailing_whitespace format!("{} \n\n\t ", $desired_output).as_str(),
remove_beginning_whitespace format!(" \n\t{}", $desired_output).as_str(),
identity $desired_output, /* test return is valid */
remove_beginning_and_trailing_whitespace format!(" \n\t {} \n\t ", $desired_output).as_str()
);
};
}

macro_rules! fmt_test_inner {
($scope:ident $desired_output:expr, $($name:ident $y:expr),+) => {
$(
paste! {
#[test]
fn [<$scope _ $name>] () {
let formatted_code = format_code($y);
let changeset = diff_lines(&formatted_code, $desired_output);
let diff = changeset.diff();
let count_of_updates = diff.len();
if count_of_updates != 0 {
println!("FAILED: {count_of_updates} diff items.");
}
for diff in diff {
match diff {
DiffOp::Equal(old) => {
for o in old {
println!("{}", o)
}
}
DiffOp::Insert(new) => {
for n in new {
println_green(&format!("+{}", n));
}
}
DiffOp::Remove(old) => {
for o in old {
println_red(&format!("-{}", o));
}
}
DiffOp::Replace(old, new) => {
for o in old {
println_red(&format!("-{}", o));
}
for n in new {
println_green(&format!("+{}", n));
}
}
}
}
assert_eq!(&formatted_code, $desired_output)
}
}
)+
}
}

fmt_test!( storage_maps
"storage {
map1: StorageMap<u64, bool> = StorageMap {},
map2: StorageMap<u64, u8> = StorageMap {},
map3: StorageMap<u64, u16> = StorageMap {},
map4: StorageMap<u64, u32> = StorageMap {},
map5: StorageMap<u64, u64> = StorageMap {},
map6: StorageMap<u64, (b256, u8, bool)> = StorageMap {},
map7: StorageMap<u64, Struct> = StorageMap {},
map8: StorageMap<u64, Enum> = StorageMap {},
map9: StorageMap<u64, str[33]> = StorageMap {},
map10: StorageMap<u64, [b256; 3]> = StorageMap {},
map11: StorageMap<bool, u64> = StorageMap {},
map12: StorageMap<u8, u64> = StorageMap {},
map13: StorageMap<u16, u64> = StorageMap {},
map14: StorageMap<u32, u64> = StorageMap {},
map15: StorageMap<(b256, u8, bool), u64> = StorageMap {},
map16: StorageMap<Struct, u64> = StorageMap {},
map17: StorageMap<Enum, u64> = StorageMap {},
map18: StorageMap<str[33], u64> = StorageMap {},
map19: StorageMap<[b256; 3], u64> = StorageMap {},
}",
wrong_new_lines
"storage {
map1: StorageMap<u64,
bool> = StorageMap {
},
map2: StorageMap<u64,
u8> = StorageMap {
},
map3: StorageMap<u64,
u16> = StorageMap {
},
map4: StorageMap<u64,
u32> = StorageMap {
},
map5: StorageMap<u64,
u64> = StorageMap {
},
map6: StorageMap<u64,
(b256, u8, bool) > = StorageMap {
},
map7: StorageMap<u64,
Struct> = StorageMap {
},
map8: StorageMap<u64,
Enum> = StorageMap {
},
map9: StorageMap<u64,
str[33]> = StorageMap {
},
map10: StorageMap<u64,
[b256;
3]> = StorageMap {
},
map11: StorageMap<bool,
u64> = StorageMap {
},
map12: StorageMap<u8,
u64> = StorageMap {
},
map13: StorageMap<u16,
u64> = StorageMap {
},
map14: StorageMap<u32,
u64> = StorageMap {
},
map15: StorageMap<(b256,
u8, bool), u64 > = StorageMap {
},
map16: StorageMap<Struct,
u64> = StorageMap {
},
map17: StorageMap<Enum,
u64> = StorageMap {
},
map18: StorageMap<str[33],
u64> = StorageMap {
},
map19: StorageMap<[b256;
3],
u64> = StorageMap {
},
}"
);
1 change: 0 additions & 1 deletion sway-fmt-v2/src/utils/language/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ impl Format for Expr {
Some(body_width),
&formatter.config,
);
debug_expr(buf, field_width, body_width, expr_width, formatter);

format_expr_struct(path, fields, formatted_code, formatter)?;

Expand Down
36 changes: 18 additions & 18 deletions sway-fmt-v2/src/utils/language/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl Format for PathExpr {
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
if let Some((root, double_colon_token)) = &self.root_opt {
if let Some(root) = &root {
if let Some((qualified_path_root, double_colon_token)) = &self.root_opt {
if let Some(root) = &qualified_path_root {
open_angle_bracket(formatted_code)?;
root.clone()
.into_inner()
Expand All @@ -28,9 +28,9 @@ impl Format for PathExpr {
write!(formatted_code, "{}", double_colon_token.ident().as_str())?;
}
self.prefix.format(formatted_code, formatter)?;
for suffix in self.suffix.iter() {
write!(formatted_code, "{}", suffix.0.span().as_str())?;
suffix.1.format(formatted_code, formatter)?;
for (double_colon_token, path_expr_segment) in self.suffix.iter() {
write!(formatted_code, "{}", double_colon_token.span().as_str())?;
path_expr_segment.format(formatted_code, formatter)?;
}

Ok(())
Expand All @@ -50,9 +50,9 @@ impl Format for PathExprSegment {
// name
write!(formatted_code, "{}", self.name.span().as_str())?;
// generics `::<args>`
if let Some(generic_args) = &self.generics_opt {
write!(formatted_code, "{}", generic_args.0.span().as_str())?;
generic_args.1.format(formatted_code, formatter)?;
if let Some((double_colon_token, generic_args)) = &self.generics_opt {
write!(formatted_code, "{}", double_colon_token.span().as_str())?;
generic_args.format(formatted_code, formatter)?;
}

Ok(())
Expand All @@ -66,9 +66,9 @@ impl Format for QualifiedPathRoot {
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
self.ty.format(formatted_code, formatter)?;
if let Some(as_trait) = &self.as_trait {
write!(formatted_code, " {} ", as_trait.0.span().as_str())?;
as_trait.1.format(formatted_code, formatter)?;
if let Some((as_token, path_type)) = &self.as_trait {
write!(formatted_code, " {} ", as_token.span().as_str())?;
path_type.format(formatted_code, formatter)?;
}

Ok(())
Expand All @@ -92,9 +92,9 @@ impl Format for PathType {
write!(formatted_code, "{}", root_opt.1.span().as_str())?;
}
self.prefix.format(formatted_code, formatter)?;
for suffix in self.suffix.iter() {
write!(formatted_code, "{}", suffix.0.span().as_str())?;
suffix.1.format(formatted_code, formatter)?;
for (double_colon_token, path_type_segment) in self.suffix.iter() {
write!(formatted_code, "{}", double_colon_token.span().as_str())?;
path_type_segment.format(formatted_code, formatter)?;
}

Ok(())
Expand All @@ -114,11 +114,11 @@ impl Format for PathTypeSegment {
// name
write!(formatted_code, "{}", self.name.span().as_str())?;
// generics `::<args>`
if let Some(generic_args) = &self.generics_opt {
if let Some(double_colon) = &generic_args.0 {
write!(formatted_code, "{}", double_colon.span().as_str())?;
if let Some((double_colon_opt, generic_args)) = &self.generics_opt {
if let Some(double_colon_token) = &double_colon_opt {
write!(formatted_code, "{}", double_colon_token.span().as_str())?;
}
generic_args.1.format(formatted_code, formatter)?;
generic_args.format(formatted_code, formatter)?;
}

Ok(())
Expand Down
Loading

0 comments on commit 1549bab

Please sign in to comment.