Skip to content

Commit ca998fe

Browse files
committed
Add Expect to option and result
1 parent 1d546be commit ca998fe

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

option/option.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,13 @@ func (o Option[T]) IsNone() bool {
8686
func (o Option[T]) Value() (T, bool) {
8787
return o.value, o.present
8888
}
89+
90+
// Expect returns the underlying value for a [Some] variant, or panics with the
91+
// provided message for a [None] variant.
92+
func (o Option[T]) Expect(message string) T {
93+
if o.present {
94+
return o.value
95+
}
96+
97+
panic(message)
98+
}

option/option_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ func ExampleOption_Value() {
7272
// true
7373
}
7474

75+
func ExampleOption_Expect() {
76+
fmt.Println(option.Some(4).Expect("oops"))
77+
78+
// Output: 4
79+
}
80+
7581
func TestSomeStringer(t *testing.T) {
7682
assert.Equal(t, fmt.Sprintf("%s", option.Some("foo")), "Some(foo)") //nolint:gosimple
7783
assert.Equal(t, fmt.Sprintf("%s", option.Some(42)), "Some(42)") //nolint:gosimple
@@ -139,3 +145,16 @@ func TestNoneValue(t *testing.T) {
139145
assert.Equal(t, value, 0)
140146
assert.False(t, ok)
141147
}
148+
149+
func TestSomeExpect(t *testing.T) {
150+
assert.Equal(t, option.Some(42).Expect("oops"), 42)
151+
}
152+
153+
func TestNoneExpect(t *testing.T) {
154+
defer func() {
155+
assert.Equal(t, fmt.Sprint(recover()), "oops")
156+
}()
157+
158+
option.None[int]().Expect("oops")
159+
t.Error("did not panic")
160+
}

result/result.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,13 @@ func (r Result[T]) UnwrapErr() error {
9797

9898
return r.err
9999
}
100+
101+
// Expect returns the underlying value of an [Ok] variant, or panics with the
102+
// provided message if called on an [Err] variant.
103+
func (r Result[T]) Expect(message string) T {
104+
if r.err == nil {
105+
return r.value
106+
}
107+
108+
panic(message)
109+
}

result/result_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ func ExampleResult_UnwrapErr() {
7979
// Output: oops
8080
}
8181

82+
func ExampleResult_Expect() {
83+
fmt.Println(result.Ok(4).Expect("oops"))
84+
85+
// Output: 4
86+
}
87+
8288
func TestOkStringer(t *testing.T) {
8389
assert.Equal(t, fmt.Sprintf("%s", result.Ok(42)), "Ok(42)") //nolint:gosimple
8490
}
@@ -160,3 +166,16 @@ func TestOkUnwrapErr(t *testing.T) {
160166
_ = result.Ok(42).UnwrapErr()
161167
t.Error("did not panic")
162168
}
169+
170+
func TestOkExpect(t *testing.T) {
171+
assert.Equal(t, result.Ok(42).Expect("oops"), 42)
172+
}
173+
174+
func TestErrExpect(t *testing.T) {
175+
defer func() {
176+
assert.Equal(t, fmt.Sprintf("%v", recover()), "oops")
177+
}()
178+
179+
_ = result.Err[int](errors.New("oops")).Expect("oops")
180+
t.Error("did not panic")
181+
}

0 commit comments

Comments
 (0)