Skip to content

Commit

Permalink
Sway Reference: Section 9 Annotations: Testing (FuelLabs#4622)
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen authored Jun 3, 2023
1 parent 6a9ba1e commit 6921e71
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/reference/src/code/language/annotations/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,29 @@ abi MyContract {
fn deposit();
// ANCHOR_END: payable
}

// ANCHOR: success_test
#[test]
fn equal() {
assert_eq(1 + 1, 2);
}
// ANCHOR_END: success_test

// ANCHOR: revert_test
#[test(should_revert)]
fn unequal() {
assert_eq(1 + 1, 3);
}
// ANCHOR_END: revert_test

// ANCHOR: revert_code_test
#[test(should_revert = "18446744073709486084")]
fn assert_revert_code() {
assert(1 + 1 == 3);
}

#[test(should_revert = "42")]
fn custom_revert_code() {
revert(42);
}
// ANCHOR_END: revert_code_test
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# Test

Sway provides the `#[test]` attribute which enables unit tests to be written in Sway.

## Success case

The `#[test]` attribute indicates that a test has passed if it did not revert.

```sway
{{#include ../../../../code/language/annotations/src/main.sw:success_test}}
```

## Revert Case

To test a case where code should revert we can use the `#[test(should_revert)]` annotation. If the test reverts then it will be reported as a passing test.

```sway
{{#include ../../../../code/language/annotations/src/main.sw:revert_test}}
```

We may specify a code to specifically test against.

```sway
{{#include ../../../../code/language/annotations/src/main.sw:revert_code_test}}
```

0 comments on commit 6921e71

Please sign in to comment.