|
| 1 | +package opt |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestSome(t *testing.T) { |
| 8 | + res := Some("val") |
| 9 | + if res.hasValue != true { |
| 10 | + t.Error("Some should return a struct with hasValue set to true. Received:", res.hasValue) |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +func TestNone(t *testing.T) { |
| 15 | + res := None[string]() |
| 16 | + if res.hasValue != false { |
| 17 | + t.Error("None should return a struct with hasValue set to false. Received:", res.hasValue) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestIsSome_Some(t *testing.T) { |
| 22 | + res := IsSome(Some("value")) |
| 23 | + if res != true { |
| 24 | + t.Error("IsSome should return true. Received:", res) |
| 25 | + } |
| 26 | +} |
| 27 | +func TestIsSome_None(t *testing.T) { |
| 28 | + res := IsSome(None[string]()) |
| 29 | + if res != false { |
| 30 | + t.Error("IsSome should return false. Received:", res) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestIsNone_Some(t *testing.T) { |
| 35 | + res := IsNone(None[string]()) |
| 36 | + if res != true { |
| 37 | + t.Error("IsNone should return true. Received:", res) |
| 38 | + } |
| 39 | +} |
| 40 | +func TestIsNone_None(t *testing.T) { |
| 41 | + res := IsNone(Some("value")) |
| 42 | + if res != false { |
| 43 | + t.Error("IsNone should return false. Received:", res) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestGetOrElse_Some(t *testing.T) { |
| 48 | + res := GetOrElse(func() string { return "fail" })(Some("val")) |
| 49 | + if res != "val" { |
| 50 | + t.Error("GetOrElse should return the Some value. Received:", res) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestGetOrElse_None(t *testing.T) { |
| 55 | + res := GetOrElse(func() string { return "elseValue" })(None[string]()) |
| 56 | + if res != "elseValue" { |
| 57 | + t.Error("GetOrElse should return the onNone() value. Received:", res) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestMatch_onSome(t *testing.T) { |
| 62 | + res := Match(func() string { return "onNone" }, func(x string) string { return x + x })(Some("val")) |
| 63 | + if res != "valval" { |
| 64 | + t.Error("Match should return the onSome() value. Received:", res) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestMatch_onNone(t *testing.T) { |
| 69 | + res := Match(func() string { return "onNone" }, func(x string) string { return x + x })(None[string]()) |
| 70 | + if res != "onNone" { |
| 71 | + t.Error("Match should return the onNone() return value. Received:", res) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestMap_Some(t *testing.T) { |
| 76 | + res := Map(func(x string) string { return x + x })(Some("val")) |
| 77 | + if res.value != "valval" { |
| 78 | + t.Error("Map should return the result of the callback function. Received:", res.value) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestMap_None(t *testing.T) { |
| 83 | + res := Map(func(x string) string { return x + x })(None[string]()) |
| 84 | + if res.hasValue != false { |
| 85 | + t.Error("Map should return a None value. Received:", res.value) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestChain_Some(t *testing.T) { |
| 90 | + res := Chain(func(x string) Option[string] { return Some(x + x) })(Some("val")) |
| 91 | + if res.hasValue != true { |
| 92 | + t.Error("Chain should return a Some of string. Received:", res.value) |
| 93 | + } |
| 94 | +} |
| 95 | +func TestChain_None(t *testing.T) { |
| 96 | + res := Chain(func(x string) Option[string] { return Some(x + x) })(None[string]()) |
| 97 | + if res.hasValue != false { |
| 98 | + t.Error("Chain should return a None value. Received:", res.value) |
| 99 | + } |
| 100 | +} |
0 commit comments