From 4652a6058d5b957782ef70ef8b0f671fe1d8b747 Mon Sep 17 00:00:00 2001 From: kayagokalp Date: Wed, 10 May 2023 01:45:12 +0300 Subject: [PATCH 1/4] feat: enable specifying should revert code --- forc-pkg/src/pkg.rs | 29 ++++++++++++++++++++++++----- forc-test/src/lib.rs | 7 ++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/forc-pkg/src/pkg.rs b/forc-pkg/src/pkg.rs index 2451a4ce192..0b3651597ec 100644 --- a/forc-pkg/src/pkg.rs +++ b/forc-pkg/src/pkg.rs @@ -148,7 +148,7 @@ pub enum PkgEntryKind { /// The possible conditions for a test result to be considered "passing". #[derive(Debug, Clone)] pub enum TestPassCondition { - ShouldRevert, + ShouldRevert(Option), ShouldNotRevert, } @@ -1949,18 +1949,37 @@ impl PkgTestEntry { let span = decl_ref.span(); let test_function_decl = decl_engine.get_function(&decl_ref); - let test_args: HashSet = test_function_decl + const FAILING_TEST_KEYWORD: &str = "should_revert"; + + let test_args: HashMap> = test_function_decl .attributes .get(&AttributeKind::Test) .expect("test declaration is missing test attribute") .iter() - .flat_map(|attr| attr.args.iter().map(|arg| arg.name.to_string())) + .flat_map(|attr| attr.args.iter()) + .map(|arg| { + ( + arg.name.to_string(), + arg.value + .as_ref() + .map(|val| val.span().as_str().to_string()), + ) + }) .collect(); + println!("{test_args:?}"); + let pass_condition = if test_args.is_empty() { anyhow::Ok(TestPassCondition::ShouldNotRevert) - } else if test_args.get("should_revert").is_some() { - anyhow::Ok(TestPassCondition::ShouldRevert) + } else if let Some(args) = test_args.get(FAILING_TEST_KEYWORD) { + let expected_revert_code = args + .as_ref() + .map(|arg| { + let arg_str = arg.replace('"', ""); + arg_str.parse::() + }) + .transpose()?; + anyhow::Ok(TestPassCondition::ShouldRevert(expected_revert_code)) } else { let test_name = &test_function_decl.name; bail!("Invalid test argument(s) for test: {test_name}.") diff --git a/forc-test/src/lib.rs b/forc-test/src/lib.rs index 9b3eebda93c..21d46f8df73 100644 --- a/forc-test/src/lib.rs +++ b/forc-test/src/lib.rs @@ -491,9 +491,10 @@ impl TestResult { /// Whether or not the test passed. pub fn passed(&self) -> bool { match &self.condition { - TestPassCondition::ShouldRevert => { - matches!(self.state, vm::state::ProgramState::Revert(_)) - } + TestPassCondition::ShouldRevert(revert_code) => match revert_code { + Some(revert_code) => self.state == vm::state::ProgramState::Revert(*revert_code), + None => matches!(self.state, vm::state::ProgramState::Revert(_)), + }, TestPassCondition::ShouldNotRevert => { !matches!(self.state, vm::state::ProgramState::Revert(_)) } From 086d0cf725066baf8ef9ed38c51d024ee473bd3b Mon Sep 17 00:00:00 2001 From: kayagokalp Date: Wed, 10 May 2023 01:56:51 +0300 Subject: [PATCH 2/4] docs added for specified revert code --- docs/book/src/testing/unit-testing.md | 9 +++++++++ forc-pkg/src/pkg.rs | 2 -- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/book/src/testing/unit-testing.md b/docs/book/src/testing/unit-testing.md index ecbe7ed02ba..7ee63888836 100644 --- a/docs/book/src/testing/unit-testing.md +++ b/docs/book/src/testing/unit-testing.md @@ -63,6 +63,15 @@ fn test_meaning_of_life() { Tests with `#[test(should_revert)]` considered to be passing if they are reverting. +It is also possible to specify a specify revert code like the following example. + +```sway +#[test(should_revert = "18446744073709486084")] +fn test_meaning_of_life() { + assert(6 * 6 == 42); +} +``` + ## Calling Contracts Unit tests can call contract functions an example for such calls can be seen below. diff --git a/forc-pkg/src/pkg.rs b/forc-pkg/src/pkg.rs index 0b3651597ec..f7dd9bc53e5 100644 --- a/forc-pkg/src/pkg.rs +++ b/forc-pkg/src/pkg.rs @@ -1967,8 +1967,6 @@ impl PkgTestEntry { }) .collect(); - println!("{test_args:?}"); - let pass_condition = if test_args.is_empty() { anyhow::Ok(TestPassCondition::ShouldNotRevert) } else if let Some(args) = test_args.get(FAILING_TEST_KEYWORD) { From caf50b8e8002a6bc9cbc592b09c878be2013478c Mon Sep 17 00:00:00 2001 From: kayagokalp Date: Wed, 10 May 2023 01:59:07 +0300 Subject: [PATCH 3/4] fix docs --- docs/book/src/testing/unit-testing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/book/src/testing/unit-testing.md b/docs/book/src/testing/unit-testing.md index 7ee63888836..c94930e084c 100644 --- a/docs/book/src/testing/unit-testing.md +++ b/docs/book/src/testing/unit-testing.md @@ -61,9 +61,7 @@ fn test_meaning_of_life() { } ``` -Tests with `#[test(should_revert)]` considered to be passing if they are reverting. - -It is also possible to specify a specify revert code like the following example. +It is also possible to specify an expected revert code, like the following example. ```sway #[test(should_revert = "18446744073709486084")] @@ -72,6 +70,8 @@ fn test_meaning_of_life() { } ``` +Tests with `#[test(should_revert)]` considered to be passing if they are reverting. + ## Calling Contracts Unit tests can call contract functions an example for such calls can be seen below. From b9be2fceb0ac979a0e4203b5067b9f236a5254ef Mon Sep 17 00:00:00 2001 From: kayagokalp Date: Wed, 10 May 2023 02:45:35 +0300 Subject: [PATCH 4/4] add expected revert code to the test --- .../should_pass/unit_tests/should_revert/src/lib.sw | 5 +++++ .../should_pass/unit_tests/should_revert/test.toml | 1 + 2 files changed, 6 insertions(+) create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/test.toml diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/src/lib.sw b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/src/lib.sw index 2c0b59959a0..280e62a02fb 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/src/lib.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/src/lib.sw @@ -4,3 +4,8 @@ library; fn should_revert_test() { assert(0 == 1) } + +#[test(should_revert = "18446744073709486084")] +fn should_revert_test_with_exact_code() { + assert(0 == 1) +} diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/test.toml b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/test.toml new file mode 100644 index 00000000000..0f3f6d7e866 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/should_revert/test.toml @@ -0,0 +1 @@ +category = "unit_tests_pass"