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.
Enhance docs and add a few methods for
option.sw
(FuelLabs#3322)
A lot of this is copied from https://doc.rust-lang.org/src/core/option.rs.html#1864 Now that we have `forc-doc`, we want to spend some time enhancing all of our standard library docstrings. There are a few more methods we can implement. I attempted `transpose()` but faced an [issue](FuelLabs#3321). There are all the Boolean methods such as `and` and `or` as well. - [x] Write tests for `unwrap_or` and `ok_or`. - [x] Enhance tests for `Option` in general Co-authored-by: JC <[email protected]> Co-authored-by: Emily Herbert <[email protected]> Co-authored-by: Toby Hutton <[email protected]>
- Loading branch information
1 parent
f581909
commit a7fa7d9
Showing
5 changed files
with
429 additions
and
42 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
84 changes: 84 additions & 0 deletions
84
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option/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,84 @@ | ||
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) | ||
} | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// Error | ||
///////////////////////////////////////////////////////////////////////////// | ||
pub enum Error { | ||
BoolError: bool, | ||
U8Error: u8, | ||
U16Error: u16, | ||
U32Error: u32, | ||
U64Error: u64, | ||
StructError: MyStruct, | ||
EnumError: MyEnum, | ||
TupleError: (u64, u64), | ||
ArrayError: [u64; 3], | ||
StringError: str[4], | ||
} | ||
|
||
impl Eq for Error { | ||
fn eq(self, other: Self) -> bool { | ||
match (self, other) { | ||
(Error::BoolError(val1), Error::BoolError(val2)) => val1 == val2, | ||
(Error::U8Error(val1), Error::U8Error(val2)) => val1 == val2, | ||
(Error::U16Error(val1), Error::U16Error(val2)) => val1 == val2, | ||
(Error::U32Error(val1), Error::U32Error(val2)) => val1 == val2, | ||
(Error::U64Error(val1), Error::U64Error(val2)) => val1 == val2, | ||
(Error::StructError(val1), Error::StructError(val2)) => val1 == val2, | ||
(Error::EnumError(val1), Error::EnumError(val2)) => val1 == val2, | ||
(Error::TupleError(val1), Error::TupleError(val2)) => val1 == val2, | ||
(Error::StringError(val1), Error::StringError(val2)) => sha256(val1) == sha256(val2), | ||
_ => false, | ||
} | ||
} | ||
} |
44 changes: 14 additions & 30 deletions
44
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option/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::option::*; | ||
use std::revert::revert; | ||
dep data_structures; | ||
dep tests; | ||
|
||
use tests::*; | ||
|
||
fn main() -> bool { | ||
test_some(); | ||
test_none(); | ||
test_unwrap_some(); | ||
test_bool(); | ||
test_u8(); | ||
test_u16(); | ||
test_u32(); | ||
test_u64(); | ||
test_struct(); | ||
test_enum(); | ||
test_tuple(); | ||
test_array(); | ||
test_string(); | ||
|
||
true | ||
} | ||
|
||
fn test_some() { | ||
let o = Option::Some(42u64); | ||
|
||
if (!o.is_some() || o.is_none()) { | ||
revert(0); | ||
} | ||
} | ||
|
||
fn test_none() { | ||
let o = Option::None::<()>(); | ||
|
||
if (o.is_some() || !o.is_none()) { | ||
revert(0); | ||
} | ||
} | ||
|
||
fn test_unwrap_some() { | ||
let o = Option::Some(42u64); | ||
|
||
let u = o.unwrap(); | ||
if (u != 42) { | ||
revert(0); | ||
} | ||
} |
Oops, something went wrong.