Testing
The Sway Applications repository generally follows the current file structure when testing using the Rust SDK.
tests/
├── functions/
| └── 1 file per ABI function
├── utils/
| ├── interface.rs
| ├── setup.rs
| └── mod.rs
└── harness.rs
functions
The functions directory contains 1 file per function declared in the abi and all test cases (not utility/helper functions) for that function are contained within that module.
There are two possibilities with any function call and they are that either the call succeeds or it reverts. For this reason each file contains two modules:
successrevert
All of the tests where the function does not revert should be contained inside the success module while the reverting calls (panics) should be contained inside the revert module.
There are two additional alternatives to consider:
- The
abimay be split into multipleabiblocks- In this case each
abimay be its own directory containing its functions - This changes the structure from having all functions in 1 directory to having categorized functions in their relevant directories
- In this case each
- In some cases it may be reasonable to further separate the function into its own directory which contains two files
successrevert
utils
The utils directory contains utility functions and abstractions which allow the tests in the functions directory to remain small, clean and "DRY" (do not repeat yourself).
This can be achieved by separating content into files, internally creating modules in mod.rs or a mixture of both.
The interface.rs file contains abi wrappers which are functions that are generally limited to calling the contract with the relevant arguments. In the case where there are multiple abi blocks (as mentioned in functions) the interface.rs file would be changed to follow a similar structure.
The setup.rs file contains code that generates the contracts/scripts/predicates and sets up the environment for the tests in the functions directory.
harness.rs
The harness file is the entry point for the tests, and thus it contains the functions and utils modules. This is what is executed when cargo test is run.