forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
#[test(should_revert)]
attribute to indicate a unit test shou…
…ld revert (FuelLabs#3490) ## Summary This PR adds `#[test(should_revert)]` tests to forc-test, enabling users to write reverting cases in their unit tests. Example: ```rust script; fn main() {} fn my_func() {} #[test(should_revert)] fn my_test_func() { my_func(); revert(0) } ``` ## Implementation Details Since FuelLabs#3509 is merged, this PR only adds the required section to `forc-test` so that it can detect if the given entry point's corresponding function declarations has `#[test(should_revert)]` attribute. If that is the case the passing condition is selected as `TestPassCondition::ShouldRevert` otherwise it defaults to `TestPassCondition::ShouldNotRevert`. I also added a safety check for erroneous declarations such as `#[test(foo)]`. Since we just have `should revert` tests right now, I am checking if there is an argument provided with the test attirbute. If there is none, test is considered to be `TestPassCondition::ShouldNotRevert`, tl-dr; default is non reverting tests. If there is an argument and it is `should_revert`, test is considered to be `TestPassCondition::ShouldRevert` and finally if there is an argument but it is not `should_revert` an error is produced. A simple should revert test with `revert(0)` added to unit tests as well.
- Loading branch information
1 parent
80aa366
commit 716b34f
Showing
8 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
13 changes: 13 additions & 0 deletions
13
test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-98FF531C5C738A40' | ||
|
||
[[package]] | ||
name = 'should_revert' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-98FF531C5C738A40' | ||
dependencies = ['core'] |
8 changes: 8 additions & 0 deletions
8
test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "lib.sw" | ||
license = "Apache-2.0" | ||
name = "should_revert" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/src/lib.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
library should_revert; | ||
|
||
#[test(should_revert)] | ||
fn should_revert_test() { | ||
assert(0 == 1) | ||
} |