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.
Enhanced `Result` documentation and add `unwrap_ok` method.
- Loading branch information
1 parent
495ad00
commit de27066
Showing
4 changed files
with
320 additions
and
30 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
51 changes: 51 additions & 0 deletions
51
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/data_structures.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,51 @@ | ||
library data_structures; | ||
|
||
use core::ops::*; | ||
use std::hash::sha256; | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// Data Structures Used in in the Tests | ||
///////////////////////////////////////////////////////////////////////////// | ||
pub struct MyStruct { | ||
x: u64, | ||
y: u64, | ||
} | ||
|
||
pub enum MyEnum { | ||
X: u64, | ||
Y: u64, | ||
} | ||
|
||
impl Eq for MyStruct { | ||
fn eq(self, other: Self) -> bool { | ||
self.x == other.x && self.y == other.y | ||
} | ||
} | ||
|
||
impl Eq for MyEnum { | ||
fn eq(self, other: Self) -> bool { | ||
match (self, other) { | ||
(MyEnum::X(val1), MyEnum::X(val2)) => val1 == val2, | ||
(MyEnum::Y(val1), MyEnum::Y(val2)) => val1 == val2, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl Eq for (u64, u64) { | ||
fn eq(self, other: Self) -> bool { | ||
self.0 == other.0 && self.1 == other.1 | ||
} | ||
} | ||
|
||
impl Eq for [u64; 3] { | ||
fn eq(self, other: Self) -> bool { | ||
self[0] == other[0] && self[1] == other[1] && self[2] == other[2] | ||
} | ||
} | ||
|
||
impl Eq for str[4] { | ||
fn eq(self, other: Self) -> bool { | ||
sha256(self) == sha256(other) | ||
} | ||
} |
44 changes: 14 additions & 30 deletions
44
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/main.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 |
---|---|---|
@@ -1,37 +1,21 @@ | ||
script; | ||
|
||
use std::result::Result; | ||
use std::revert::revert; | ||
dep data_structures; | ||
dep tests; | ||
|
||
use tests::*; | ||
|
||
fn main() -> bool { | ||
test_ok(); | ||
test_err(); | ||
test_unwrap_ok(); | ||
test_bool(); | ||
test_u8(); | ||
test_u16(); | ||
test_u32(); | ||
test_u64(); | ||
test_struct(); | ||
test_enum(); | ||
test_tuple(); | ||
test_array(); | ||
test_string(); | ||
|
||
true | ||
} | ||
|
||
fn test_ok() { | ||
let r = Result::Ok::<u64, ()>(42u64); | ||
|
||
if (!r.is_ok() || r.is_err()) { | ||
revert(0); | ||
} | ||
} | ||
|
||
fn test_err() { | ||
let r = Result::Err::<(), ()>(()); | ||
|
||
if (r.is_ok() || !r.is_err()) { | ||
revert(0); | ||
} | ||
} | ||
|
||
fn test_unwrap_ok() { | ||
let r = Result::Ok::<u64, ()>(42); | ||
|
||
let u = r.unwrap(); | ||
if (u != 42) { | ||
revert(0); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/tests.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,129 @@ | ||
library tests; | ||
|
||
dep data_structures; | ||
|
||
use data_structures::*; | ||
use core::ops::*; | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// Generic Tests | ||
///////////////////////////////////////////////////////////////////////////// | ||
fn test_is_ok<T>(val: T) { | ||
assert(Result::Ok::<T, T>(val).is_ok()); | ||
assert(!Result::Err::<T, T>(val).is_ok()); | ||
} | ||
|
||
fn test_is_err<T>(val: T) { | ||
assert(!Result::Ok::<T, T>(val).is_err()); | ||
assert(Result::Err::<T, T>(val).is_err()); | ||
} | ||
|
||
fn test_unwrap<T>(val: T) | ||
where | ||
T: Eq | ||
{ | ||
assert(Result::Ok::<T, T>(val).unwrap() == val); | ||
} | ||
|
||
// TODO: Combine following two functions when the following issue is resolved: | ||
// https://github.com/FuelLabs/sway/issues/3623 | ||
fn test_unwrap_or_ok<T>(val: T, default: T) | ||
where | ||
T: Eq | ||
{ | ||
assert(Result::Ok::<T, T>(val).unwrap_or(default) == val); | ||
} | ||
fn test_unwrap_or_err<T>(val: T, default: T) | ||
where | ||
T: Eq | ||
{ | ||
assert(Result::Err::<T, T>(val).unwrap_or(default) == default); | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// Tests for Various Types | ||
///////////////////////////////////////////////////////////////////////////// | ||
pub fn test_bool() { | ||
test_is_ok(true); | ||
test_is_err(true); | ||
test_unwrap(true); | ||
test_unwrap_or_ok(true, false); | ||
test_unwrap_or_err(true, false); | ||
} | ||
|
||
pub fn test_u8() { | ||
test_is_ok(42_u8); | ||
test_is_err(42_u8); | ||
test_unwrap(42_u8); | ||
test_unwrap_or_ok(42_u8, 69_u8); | ||
test_unwrap_or_err(42_u8, 69_u8); | ||
} | ||
|
||
pub fn test_u16() { | ||
test_is_ok(42_u16); | ||
test_is_err(42_u16); | ||
test_unwrap(42_u16); | ||
test_unwrap_or_ok(42_u64, 69_u64); | ||
test_unwrap_or_err(42_u16, 69_u16); | ||
} | ||
|
||
pub fn test_u32() { | ||
test_is_ok(42_u32); | ||
test_is_err(42_u32); | ||
test_unwrap(42_u32); | ||
test_unwrap_or_ok(42_u64, 69_u64); | ||
test_unwrap_or_err(42_u32, 69_u32); | ||
} | ||
|
||
pub fn test_u64() { | ||
test_is_ok(42_u64); | ||
test_is_err(42_u64); | ||
test_unwrap(42_u64); | ||
test_unwrap_or_ok(42_u64, 69_u64); | ||
test_unwrap_or_err(42_u64, 69_u64); | ||
} | ||
|
||
pub fn test_struct() { | ||
let s = MyStruct { x: 42, y: 43 }; | ||
test_is_ok(s); | ||
test_is_err(s); | ||
test_unwrap(s); | ||
test_unwrap_or_ok(s, MyStruct { x: 69, y: 69 }); | ||
test_unwrap_or_err(s, MyStruct { x: 69, y: 69 }); | ||
} | ||
|
||
pub fn test_enum() { | ||
let e = MyEnum::Y(42); | ||
test_is_ok(e); | ||
test_is_err(e); | ||
test_unwrap(e); | ||
test_unwrap_or_ok(e, MyEnum::X(69)); | ||
test_unwrap_or_err(e, MyEnum::X(69)); | ||
} | ||
|
||
pub fn test_tuple() { | ||
let t = (42, 43); | ||
test_is_ok(t); | ||
test_is_err(t); | ||
test_unwrap(t); | ||
test_unwrap_or_ok(t, (69, 70)); | ||
test_unwrap_or_err(t, (69, 70)); | ||
} | ||
|
||
pub fn test_array() { | ||
let a = [42, 43, 44]; | ||
test_is_ok(a); | ||
test_is_err(a); | ||
test_unwrap(a); | ||
test_unwrap_or_ok(a, [69, 70, 71]); | ||
test_unwrap_or_err(a, [69, 70, 71]); | ||
} | ||
|
||
pub fn test_string() { | ||
let s = "fuel"; | ||
test_is_ok(s); | ||
test_is_err(s); | ||
test_unwrap(s); | ||
test_unwrap_or_ok(s, "sway"); | ||
test_unwrap_or_err(s, "sway"); | ||
} |