Skip to content

Commit

Permalink
refactor: item_configurable test to use test_macros (FuelLabs#4365)
Browse files Browse the repository at this point in the history
## Description

Just a small cleanup - Code deletion is delicious

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] 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.
  • Loading branch information
eightfilms authored Mar 30, 2023
1 parent da546b7 commit dfd0734
Showing 1 changed file with 2 additions and 61 deletions.
63 changes: 2 additions & 61 deletions swayfmt/src/items/item_configurable/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::ItemConfigurable>($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!( configurables
fmt_test_item!( configurables
"configurable {
C0: bool = true,
C1: u64 = 42,
Expand Down

0 comments on commit dfd0734

Please sign in to comment.