opt.Optional[T].IsNothing() opt.Optional[T].IsSomething()

master
Charles Iliya Krempeaux 2024-06-16 06:42:51 -07:00
parent afc1a40c4b
commit f34a58dd58
4 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package opt
func (receiver Optional[T]) IsNothing() bool {
return !(receiver.something)
}

View File

@ -0,0 +1,91 @@
package opt_test
import (
"testing"
"sourcecode.social/reiver/go-opt"
)
func TestOptional_IsNothing_int(t *testing.T) {
tests := []struct{
Optional opt.Optional[int]
Expected bool
}{
{
Optional: opt.Nothing[int](),
Expected: true,
},
{
Optional: opt.Something(-255),
Expected: false,
},
{
Optional: opt.Something(-5),
Expected: false,
},
{
Optional: opt.Something(-4),
Expected: false,
},
{
Optional: opt.Something(-3),
Expected: false,
},
{
Optional: opt.Something(-2),
Expected: false,
},
{
Optional: opt.Something(-1),
Expected: false,
},
{
Optional: opt.Something(0),
Expected: false,
},
{
Optional: opt.Something(1),
Expected: false,
},
{
Optional: opt.Something(2),
Expected: false,
},
{
Optional: opt.Something(3),
Expected: false,
},
{
Optional: opt.Something(4),
Expected: false,
},
{
Optional: opt.Something(5),
Expected: false,
},
{
Optional: opt.Something(255),
Expected: false,
},
}
for testNumber, test := range tests {
actual := test.Optional.IsNothing()
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value of is-nothing it not what was expected.", testNumber)
t.Logf("EXPECTED: %t", expected)
t.Logf("ACTUAL: %t", actual)
t.Logf("OPTIONAL: %#v", test.Optional)
continue
}
}
}

View File

@ -0,0 +1,5 @@
package opt
func (receiver Optional[T]) IsSomething() bool {
return receiver.something
}

View File

@ -0,0 +1,91 @@
package opt_test
import (
"testing"
"sourcecode.social/reiver/go-opt"
)
func TestOptional_IsSomething_int(t *testing.T) {
tests := []struct{
Optional opt.Optional[int]
Expected bool
}{
{
Optional: opt.Nothing[int](),
Expected: false,
},
{
Optional: opt.Something(-255),
Expected: true,
},
{
Optional: opt.Something(-5),
Expected: true,
},
{
Optional: opt.Something(-4),
Expected: true,
},
{
Optional: opt.Something(-3),
Expected: true,
},
{
Optional: opt.Something(-2),
Expected: true,
},
{
Optional: opt.Something(-1),
Expected: true,
},
{
Optional: opt.Something(0),
Expected: true,
},
{
Optional: opt.Something(1),
Expected: true,
},
{
Optional: opt.Something(2),
Expected: true,
},
{
Optional: opt.Something(3),
Expected: true,
},
{
Optional: opt.Something(4),
Expected: true,
},
{
Optional: opt.Something(5),
Expected: true,
},
{
Optional: opt.Something(255),
Expected: true,
},
}
for testNumber, test := range tests {
actual := test.Optional.IsSomething()
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value of is-something it not what was expected.", testNumber)
t.Logf("EXPECTED: %t", expected)
t.Logf("ACTUAL: %t", actual)
t.Logf("OPTIONAL: %#v", test.Optional)
continue
}
}
}