Skip to content

Commit

Permalink
Merge pull request #2 from GpigZ/main
Browse files Browse the repository at this point in the history
change nil to empty
  • Loading branch information
andeya authored Sep 4, 2022
2 parents c1f345c + 73e874a commit 84b1b2f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ func TestResultFlatten(t *testing.T) {
fmt.Printf("errs %v\n", errs)
fmt.Printf("nums %v\n", nums)
}

func TestResultOption(t *testing.T) {
var okStr = "20"
var errStr = "not a number"
var emptyStr string
// Ret encapsulates value and err into result
okResult := gust.Ret(strconv.ParseUint(okStr, 10, 64))
errResult := gust.Ret(strconv.ParseUint(errStr, 10, 64))
emptyResult := gust.Ret(strconv.ParseUint(emptyStr, 10, 64))
assert.Equal(t, uint64(20), okResult.Unwrap())
assert.Equal(t, true, errResult.IsErr())
assert.Equal(t, true, emptyResult.IsErr())
// If an error occurs Ok will set it to empty
// If you encounter the return value is empty but err is also empty, You can use ok to convert result to option
okOption := okResult.Ok()
errOption := errResult.Ok()
emptyOption := emptyResult.Ok()
assert.Equal(t, uint64(20), okOption.Unwrap())
assert.Equal(t, true, errOption.IsNone())
assert.Equal(t, true, emptyOption.IsNone())
}

0 comments on commit 84b1b2f

Please sign in to comment.