Compare commits

..

2 Commits

Author SHA1 Message Date
Charles Iliya Krempeaux 3f808e9749 more tests 2023-09-24 05:30:44 +09:00
Charles Iliya Krempeaux b434dd95d3 .isnothing() 2023-09-24 05:24:56 +09:00
5 changed files with 47 additions and 5 deletions

2
map.go
View File

@ -39,7 +39,7 @@ package opt
// //
// // result2 == opt.Nothing[int]() // // result2 == opt.Nothing[int]()
func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] { func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] {
if !op.something { if op.isnothing() {
return Nothing[T2]() return Nothing[T2]()
} }

View File

@ -107,7 +107,7 @@ func Something[T any](value T) Optional[T] {
// //
// op3 = op3.Filter(fn) // op3 = op3.Filter(fn)
func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] { func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
if !receiver.something { if receiver.isnothing() {
return Nothing[T]() return Nothing[T]()
} }
@ -118,6 +118,10 @@ func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
return receiver return receiver
} }
func (receiver Optional[T]) isnothing() bool {
return !receiver.something
}
// Get returns the value inside of the optional-type if it is holding something. // Get returns the value inside of the optional-type if it is holding something.
// //
// Example usage: // Example usage:
@ -162,7 +166,7 @@ func (receiver Optional[T]) Get() (T, bool) {
// // Output: // // Output:
// // op = opt.Nothing[uint8]() // // op = opt.Nothing[uint8]()
func (receiver Optional[T]) GoString() string { func (receiver Optional[T]) GoString() string {
if !receiver.something { if receiver.isnothing() {
return fmt.Sprintf("opt.Nothing[%T]()", receiver.value) return fmt.Sprintf("opt.Nothing[%T]()", receiver.value)
} }
@ -183,7 +187,7 @@ func (receiver Optional[T]) GoString() string {
// //@TODO // //@TODO
// }) // })
func (receiver Optional[T]) WhenNothing(fn func()) { func (receiver Optional[T]) WhenNothing(fn func()) {
if !receiver.something { if receiver.isnothing() {
fn() fn()
} }
} }

View File

@ -44,3 +44,22 @@ func TestOptional_MarshalJSON_bool(t *testing.T) {
} }
} }
} }
func TestOptional_MarshalJSON_bool_fail(t *testing.T) {
var nothing opt.Optional[bool]
actualBytes, err := nothing.MarshalJSON()
if nil == err {
t.Error("Expected an error but did not actually get one.")
t.Logf("ACTUAL: %q", actualBytes)
t.Logf("ERROR: (%T) %s", err, err)
return
}
if nil != actualBytes {
t.Error("Expected not bytes but actually get some.")
t.Logf("ACTUAL: %q", actualBytes)
t.Logf("ERROR: (%T) %s", err, err)
return
}
}

View File

@ -103,3 +103,22 @@ func TestOptional_MarshalJSON_string(t *testing.T) {
} }
} }
} }
func TestOptional_MarshalJSON_string_fail(t *testing.T) {
var nothing opt.Optional[string]
actualBytes, err := nothing.MarshalJSON()
if nil == err {
t.Error("Expected an error but did not actually get one.")
t.Logf("ACTUAL: %q", actualBytes)
t.Logf("ERROR: (%T) %s", err, err)
return
}
if nil != actualBytes {
t.Error("Expected not bytes but actually get some.")
t.Logf("ACTUAL: %q", actualBytes)
t.Logf("ERROR: (%T) %s", err, err)
return
}
}

View File

@ -36,7 +36,7 @@ package opt
// //
// // result2 == opt.Nothing[byte]() // // result2 == opt.Nothing[byte]()
func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] { func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] {
if !op.something { if op.isnothing() {
return Nothing[T2]() return Nothing[T2]()
} }