github.com/enetx/g@v1.0.80/tests/option_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/enetx/g"
     8  )
     9  
    10  func TestOptionUnwrapOr(t *testing.T) {
    11  	fn := func(x int) g.Option[int] {
    12  		if x > 10 {
    13  			return g.Some[int](x)
    14  		}
    15  		return g.None[int]()
    16  	}
    17  
    18  	result := fn(5).UnwrapOr(10)
    19  	expected := 10
    20  
    21  	if result != expected {
    22  		t.Errorf("Expected %d, got %d", expected, result)
    23  	}
    24  
    25  	result = fn(11).UnwrapOr(10)
    26  	expected = 11
    27  
    28  	if result != expected {
    29  		t.Errorf("Expected %d, got %d", expected, result)
    30  	}
    31  }
    32  
    33  // func TestOptionExpect(t *testing.T) {
    34  // 	// Test 1: Expecting value from Some
    35  // 	option1 := g.Some(42)
    36  // 	result1 := option1.Expect("Expected Some, got None")
    37  // 	expected1 := 42
    38  
    39  // 	if result1 != expected1 {
    40  // 		t.Errorf("Test 1: Expected %d, got %d", expected1, result1)
    41  // 	}
    42  
    43  // 	// Test 2: Expecting panic from None
    44  // 	option2 := g.None[int]()
    45  // 	defer func() {
    46  // 		if r := recover(); r == nil {
    47  // 			t.Errorf("Test 2: The code did not panic")
    48  // 		}
    49  // 	}()
    50  // 	option2.Expect("Expected Some, got None")
    51  // }
    52  
    53  func TestOptionThen(t *testing.T) {
    54  	// Test 1: Applying fn to Some
    55  	option1 := g.Some(5)
    56  
    57  	fn1 := func(x int) g.Option[int] {
    58  		return g.Some(x * 2)
    59  	}
    60  
    61  	result1 := option1.Then(fn1)
    62  	expected1 := g.Some(10)
    63  
    64  	if !reflect.DeepEqual(result1, expected1) {
    65  		t.Errorf("Test 1: Expected %v, got %v", expected1, result1)
    66  	}
    67  
    68  	// Test 2: Returning same Option for None
    69  	option2 := g.None[int]()
    70  
    71  	fn2 := func(x int) g.Option[int] {
    72  		return g.Some(x * 2)
    73  	}
    74  
    75  	result2 := option2.Then(fn2)
    76  
    77  	if result2.IsSome() {
    78  		t.Errorf("Test 2: Expected None, got Some")
    79  	}
    80  }
    81  
    82  // func TestOptionUnwrap(t *testing.T) {
    83  // 	// Test 1: Unwrapping Some
    84  // 	option1 := g.Some(42)
    85  // 	result1 := option1.Unwrap()
    86  // 	expected1 := 42
    87  
    88  // 	if result1 != expected1 {
    89  // 		t.Errorf("Test 1: Expected %d, got %d", expected1, result1)
    90  // 	}
    91  
    92  // 	// Test 2: Unwrapping None, should panic
    93  // 	option2 := g.None[int]()
    94  // 	defer func() {
    95  // 		if r := recover(); r == nil {
    96  // 			t.Errorf("Test 2: The code did not panic")
    97  // 		}
    98  // 	}()
    99  
   100  // 	option2.Unwrap()
   101  // }
   102  
   103  func TestOptionMap(t *testing.T) {
   104  	// Test 1: Mapping over Some value
   105  	option1 := g.Some(5)
   106  
   107  	fn1 := func(x int) g.Option[int] {
   108  		return g.Some(x * 2)
   109  	}
   110  
   111  	result1 := g.OptionMap(option1, fn1)
   112  	expected1 := g.Some(10)
   113  
   114  	if !reflect.DeepEqual(result1, expected1) {
   115  		t.Errorf("Test 1: Expected %v, got %v", expected1, result1)
   116  	}
   117  
   118  	// Test 2: Mapping over None value
   119  	option2 := g.None[int]()
   120  
   121  	fn2 := func(x int) g.Option[int] {
   122  		return g.Some(x * 2)
   123  	}
   124  
   125  	result2 := g.OptionMap(option2, fn2)
   126  
   127  	if result2.IsSome() {
   128  		t.Errorf("Test 2: Expected None, got Some")
   129  	}
   130  }