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: multi contract calls in unit tests for contracts (FuelLabs#4156)
## Description closes FuelLabs#3571. closes FuelLabs#4162. This PR adds the ability of calling multiple contracts from sway unit tests if they are added as `[contract-dependencies]`. This is limited with contracts currently but I will be having a follow-up which builds upon this to introduce this support to scripts as well. As these contracts are already declared under `[contract-dependencies]` their contract ids are injected into their namespace by `forc-pkg`. A bug related to this step is fixed in FuelLabs#4159. <img width="787" alt="image" src="https://user-images.githubusercontent.com/20915464/224345002-92dc2bcb-823d-4971-9041-31111cf85e77.png"> ### Follow-ups - FuelLabs#4161 - ~FuelLabs#4162~ ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] 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). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Kaya Gokalp <[email protected]>
- Loading branch information
1 parent
9599549
commit f4cffba
Showing
27 changed files
with
442 additions
and
82 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[[package]] | ||
name = 'callee' | ||
source = 'member' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'caller' | ||
source = 'member' | ||
dependencies = ['std'] | ||
contract-dependencies = ['callee'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-9B44250BDFED688D' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-9B44250BDFED688D' | ||
dependencies = ['core'] |
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,3 @@ | ||
[workspace] | ||
|
||
members = ["caller", "callee"] |
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 |
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 = "main.sw" | ||
license = "Apache-2.0" | ||
name = "callee" | ||
|
||
[dependencies] | ||
std = { path = "../../../sway-lib-std/" } |
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,11 @@ | ||
contract; | ||
|
||
abi CalleeContract { | ||
fn test_true() -> bool; | ||
} | ||
|
||
impl CalleeContract for Contract { | ||
fn test_true() -> bool { | ||
true | ||
} | ||
} |
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 |
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 @@ | ||
# ANCHOR: multi_contract_call_toml | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "caller" | ||
|
||
[dependencies] | ||
std = { path = "../../../sway-lib-std/" } | ||
|
||
[contract-dependencies] | ||
callee = { path = "../callee" } | ||
# ANCHOR: multi_contract_call_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,28 @@ | ||
// ANCHOR: multi_contract_calls | ||
contract; | ||
|
||
abi CallerContract { | ||
fn test_false() -> bool; | ||
} | ||
|
||
impl CallerContract for Contract { | ||
fn test_false() -> bool { | ||
false | ||
} | ||
} | ||
|
||
abi CalleeContract { | ||
fn test_true() -> bool; | ||
} | ||
|
||
#[test] | ||
fn test_multi_contract_calls() { | ||
let caller = abi(CallerContract, CONTRACT_ID); | ||
let callee = abi(CalleeContract, callee::CONTRACT_ID); | ||
|
||
let should_be_false = caller.test_false(); | ||
let should_be_true = callee.test_true(); | ||
assert(!should_be_false); | ||
assert(should_be_true); | ||
} | ||
// ANCHOR: multi_contract_calls |
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
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
Oops, something went wrong.