Skip to content

Commit

Permalink
refactor(fmt): dedup fmt_test! macros (FuelLabs#4100)
Browse files Browse the repository at this point in the history
## Description

closes FuelLabs#4098

This PR moves all test related macros into a new crate `test_macros`
(this includes `assert_eq_pretty!` from a previous PR (FuelLabs#3960). The
motivation for this is when working with unit tests for maybe an
expression, I realized that `fmt_test` is being re-implemented
everywhere.

We create 2 new macros `fmt_test_expr` and `fmt_test_item`, which are
now used in the tests, instead of creating a new local `fmt_test!` for
each AST type.

This cuts down quite a bit of LoC and boilerplate code, since for each
AST kind we were re-implementing `fmt_test!` in each `test.rs` file.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Sophie Dankel <[email protected]>
  • Loading branch information
eightfilms and sdankel authored Feb 18, 2023
1 parent 88776b8 commit 44aa2e2
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 490 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ members = [
"test",
]
exclude = [
"examples/*"
"examples/*",
"swayfmt/test_macros"
]

[profile.dev.package.sway-lsp]
Expand Down
1 change: 1 addition & 0 deletions swayfmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ toml = "0.5"
forc-util = { path = "../forc-util" }
paste = "1.0"
prettydiff = "0.6"
test-macros = { path = "test_macros" }
63 changes: 2 additions & 61 deletions swayfmt/src/items/item_enum/tests.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,9 @@
use forc_tracing::{println_green, println_red};
use paste::paste;
use prettydiff::{basic::DiffOp, diff_lines};
use test_macros::fmt_test_item;

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 = crate::parse::parse_format::<sway_ast::ItemEnum>($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!( annotated_enum
fmt_test_item!( annotated_enum
"pub enum Annotated {
#[storage(write)]
foo: (),
Expand Down
71 changes: 6 additions & 65 deletions swayfmt/src/items/item_fn/tests.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,17 @@
use forc_tracing::{println_green, println_red};
use paste::paste;
use prettydiff::{basic::DiffOp, diff_lines};
use test_macros::fmt_test_item;

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 = crate::parse::parse_format::<sway_ast::ItemFn>($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!( long_fn_name "pub fn hello_this_is_a_really_long_fn_name_wow_so_long_ridiculous(\n self,\n foo: Foo,\n bar: Bar,\n baz: Baz,\n) {}",
fmt_test_item!( long_fn_name "pub fn hello_this_is_a_really_long_fn_name_wow_so_long_ridiculous(\n self,\n foo: Foo,\n bar: Bar,\n baz: Baz,\n) {}",
intermediate_whitespace "pub fn hello_this_is_a_really_long_fn_name_wow_so_long_ridiculous ( self , foo : Foo , bar : Bar , baz: Baz) {\n }"
);

fmt_test!( long_fn_args "fn foo(\n mut self,\n this_is_a_really_long_variable: Foo,\n hello_im_really_long: Bar,\n) -> String {}",
fmt_test_item!( long_fn_args "fn foo(\n mut self,\n this_is_a_really_long_variable: Foo,\n hello_im_really_long: Bar,\n) -> String {}",
intermediate_whitespace " fn foo( \n mut self , \n this_is_a_really_long_variable : Foo ,\n hello_im_really_long: Bar , \n ) -> String { \n } "
);

fmt_test!( non_self_fn
fmt_test_item!( non_self_fn
"fn test_function(
helloitsverylong: String,
whatisgoingonthisistoolong: String,
Expand All @@ -94,7 +35,7 @@ fmt_test!( non_self_fn
}"
);

fmt_test!( fn_with_nested_items
fmt_test_item!( fn_with_nested_items
"fn returns_msg_sender(expected_id: ContractId) -> bool {
let result: Result<Identity, AuthError> = msg_sender();
let mut ret = false;
Expand Down Expand Up @@ -128,7 +69,7 @@ fmt_test!( fn_with_nested_items
}"
);

fmt_test!( fn_nested_if_lets
fmt_test_item!( fn_nested_if_lets
"fn has_nested_if_let() {
let result_1 = if let Result::Ok(x) = x { 100 } else { 1 };
let result_2 = if let Result::Err(x) = x { 3 } else { 43 };
Expand Down
65 changes: 3 additions & 62 deletions swayfmt/src/items/item_impl/tests.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,9 @@
use forc_tracing::{println_green, println_red};
use paste::paste;
use prettydiff::{basic::DiffOp, diff_lines};
use test_macros::fmt_test_item;

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 = crate::parse::parse_format::<sway_ast::ItemImpl>($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!( impl_with_nested_items
fmt_test_item!( impl_with_nested_items
"impl AuthTesting for Contract {
fn returns_msg_sender(expected_id: ContractId) -> bool {
let result: Result<Identity, AuthError> = msg_sender();
Expand Down Expand Up @@ -100,7 +41,7 @@ fmt_test!( impl_with_nested_items
}"
);

fmt_test!( normal_with_generics
fmt_test_item!( normal_with_generics
"impl<T> Option<T> {
fn some(value: T) -> Self {
Option::Some::<T>(value)
Expand Down
63 changes: 2 additions & 61 deletions swayfmt/src/items/item_storage/tests.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,9 @@
use forc_tracing::{println_green, println_red};
use paste::paste;
use prettydiff::{basic::DiffOp, diff_lines};
use test_macros::fmt_test_item;

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 = crate::parse::parse_format::<sway_ast::ItemStorage>($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
fmt_test_item!( storage_maps
"storage {
map1: StorageMap<u64, bool> = StorageMap {},
map2: StorageMap<u64, u8> = StorageMap {},
Expand Down
Loading

0 comments on commit 44aa2e2

Please sign in to comment.