From ac10a7a79cb09b51c3171c3a7c22ff114b2b5737 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 5 Nov 2023 01:20:57 -0700 Subject: [PATCH] .GetElse() --- optional.go | 23 +++++ optional_getelse_test.go | 213 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 optional_getelse_test.go diff --git a/optional.go b/optional.go index 1638d4c..53e5d75 100644 --- a/optional.go +++ b/optional.go @@ -141,6 +141,29 @@ func (receiver Optional[T]) Get() (T, bool) { return receiver.value, receiver.something } +// GetElse returns the value inside of the optional-type if it is holding something. +// Else it returns the alternstive value passed as a parameter. +// Example usage: +// +// var op opt.Optional[string] +// +// // ... +// +// value := op.GetElse(alternative) +// +// if found { +// fmt.Println("VALUE:", value) +// } else { +// fmt.Println("nothing") +// } +func (receiver Optional[T]) GetElse(alternative T) T { + if receiver.isnothing() { + return alternative + } + + return receiver.value +} + // GoString makes it that when the fmt.Fprintf(), fmt.Printf(), and fmt.Sprintf() family of functions // renders this type with the %#v verb, that it will be easier to understand. // diff --git a/optional_getelse_test.go b/optional_getelse_test.go new file mode 100644 index 0000000..cc4bf61 --- /dev/null +++ b/optional_getelse_test.go @@ -0,0 +1,213 @@ +package opt_test + +import ( + "sourcecode.social/reiver/go-opt" + + "testing" +) + +func TestOptional_GetElse_string(t *testing.T) { + + tests := []struct{ + Optional opt.Optional[string] + Alternative string + Expected string + }{ + { + Optional: opt.Nothing[string](), + Alternative: "alternative", + Expected: "alternative", + }, + { + Optional: opt.Nothing[string](), + Alternative: "", + Expected: "", + }, + { + Optional: opt.Nothing[string](), + Alternative: "apple", + Expected: "apple", + }, + { + Optional: opt.Nothing[string](), + Alternative: "banana", + Expected: "banana", + }, + { + Optional: opt.Nothing[string](), + Alternative: "cherry", + Expected: "cherry", + }, + { + Optional: opt.Nothing[string](), + Alternative: "ONCE TWICE THRICE FOURCE", + Expected: "ONCE TWICE THRICE FOURCE", + }, + { + Optional: opt.Nothing[string](), + Alternative: "😈", + Expected: "😈", + }, + + + + { + Optional: opt.Something("ONCE TWICE THRICE FOURCE"), + Alternative: "😈", + Expected: "ONCE TWICE THRICE FOURCE", + }, + + + + { + Optional: opt.Something("۰۱۲۳۴۵۶۷۸۹"), + Alternative: "😈", + Expected: "۰۱۲۳۴۵۶۷۸۹", + }, + } + + for testNumber, test := range tests { + + actual := test.Optional.GetElse(test.Alternative) + + { + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED VALUE: %q", expected) + t.Logf("ACTUAL VALUE: %q", actual) + t.Logf("OPTIONAL: %#v", test.Optional) + /////////////////////// CONTINUE + continue + } + } + } +} + +func TestOptional_GetElse_int8(t *testing.T) { + + tests := []struct{ + Optional opt.Optional[int8] + Alternative int8 + Expected int8 + }{ + { + Optional: opt.Nothing[int8](), + Alternative: -2, + Expected: -2, + }, + { + Optional: opt.Nothing[int8](), + Alternative: -1, + Expected: -1, + }, + { + Optional: opt.Nothing[int8](), + Alternative: 0, + Expected: 0, + }, + { + Optional: opt.Nothing[int8](), + Alternative: 1, + Expected: 1, + }, + { + Optional: opt.Nothing[int8](), + Alternative: 2, + Expected: 2, + }, + + + + { + Optional: opt.Something[int8](-127), + Alternative: 42, + Expected: -127, + }, + { + Optional: opt.Something[int8](0), + Alternative: 42, + Expected: 0, + }, + { + Optional: opt.Something[int8](127), + Alternative: 42, + Expected: 127, + }, + } + + for testNumber, test := range tests { + + actual := test.Optional.GetElse(test.Alternative) + + { + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED VALUE: %q", expected) + t.Logf("ACTUAL VALUE: %q", actual) + t.Logf("OPTIONAL: %#v", test.Optional) + /////////////////////// CONTINUE + continue + } + } + } +} + +func TestOptional_GetElse_uint8(t *testing.T) { + + tests := []struct{ + Optional opt.Optional[uint8] + Alternative uint8 + Expected uint8 + }{ + { + Optional: opt.Nothing[uint8](), + Alternative: 0, + Expected: 0, + }, + { + Optional: opt.Nothing[uint8](), + Alternative: 1, + Expected: 1, + }, + { + Optional: opt.Nothing[uint8](), + Alternative: 2, + Expected: 2, + }, + + + + { + Optional: opt.Something[uint8](0), + Alternative: 42, + Expected: 0, + }, + { + Optional: opt.Something[uint8](127), + Alternative: 42, + Expected: 127, + }, + } + + for testNumber, test := range tests { + + actual := test.Optional.GetElse(test.Alternative) + + { + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED VALUE: %q", expected) + t.Logf("ACTUAL VALUE: %q", actual) + t.Logf("OPTIONAL: %#v", test.Optional) + /////////////////////// CONTINUE + continue + } + } + } +}