From f34a58dd5853a61028bc8f4837dd30b91a312bc6 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 16 Jun 2024 06:42:51 -0700 Subject: [PATCH] opt.Optional[T].IsNothing() opt.Optional[T].IsSomething() --- optional_isnothing.go | 5 ++ optional_isnothing_int_test.go | 91 ++++++++++++++++++++++++++++++++ optional_issomething.go | 5 ++ optional_issomething_int_test.go | 91 ++++++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 optional_isnothing.go create mode 100644 optional_isnothing_int_test.go create mode 100644 optional_issomething.go create mode 100644 optional_issomething_int_test.go diff --git a/optional_isnothing.go b/optional_isnothing.go new file mode 100644 index 0000000..0183844 --- /dev/null +++ b/optional_isnothing.go @@ -0,0 +1,5 @@ +package opt + +func (receiver Optional[T]) IsNothing() bool { + return !(receiver.something) +} diff --git a/optional_isnothing_int_test.go b/optional_isnothing_int_test.go new file mode 100644 index 0000000..70756d2 --- /dev/null +++ b/optional_isnothing_int_test.go @@ -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 + } + } +} diff --git a/optional_issomething.go b/optional_issomething.go new file mode 100644 index 0000000..db5825f --- /dev/null +++ b/optional_issomething.go @@ -0,0 +1,5 @@ +package opt + +func (receiver Optional[T]) IsSomething() bool { + return receiver.something +} diff --git a/optional_issomething_int_test.go b/optional_issomething_int_test.go new file mode 100644 index 0000000..66a36ec --- /dev/null +++ b/optional_issomething_int_test.go @@ -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 + } + } +}