github.com/searKing/golang/go@v1.2.117/exp/types/optional_test.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types_test
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  	"testing"
    11  
    12  	"github.com/searKing/golang/go/exp/types"
    13  )
    14  
    15  func ExampleOptional_String() {
    16  	fmt.Printf("%s\n", types.Optional[string]{}.String())
    17  	fmt.Printf("%s\n", types.Optional[int]{}.String())
    18  	fmt.Printf("%s\n", types.Optional[*int]{}.String())
    19  
    20  	fmt.Printf("%s\n", types.Opt("").String())
    21  	fmt.Printf("%s\n", types.Opt(0).String())
    22  	fmt.Printf("%s\n", types.Opt[*int](nil).String())
    23  
    24  	fmt.Printf("%s\n", types.Opt("hello world").String())
    25  	fmt.Printf("%s\n", types.Opt(0xFF).String())
    26  	var p int
    27  	fmt.Printf("%s\n", types.Opt(&p).String()[:2])
    28  
    29  	// Output:
    30  	// null
    31  	// null
    32  	// null
    33  	//
    34  	// 0
    35  	// <nil>
    36  	// hello world
    37  	// 255
    38  	// 0x
    39  }
    40  
    41  func ExampleOptional_Format() {
    42  	fmt.Printf("'%+v'\n", types.Optional[string]{})
    43  	fmt.Printf("'%v'\n", types.Optional[string]{})
    44  	fmt.Printf("'%s'\n", types.Optional[string]{})
    45  	fmt.Printf("'%+v'\n", types.Optional[int]{})
    46  	fmt.Printf("'%v'\n", types.Optional[int]{})
    47  	fmt.Printf("'%s'\n", types.Optional[int]{})
    48  	fmt.Printf("'%+v'\n", types.Optional[*int]{})
    49  	fmt.Printf("'%v'\n", types.Optional[*int]{})
    50  	fmt.Printf("'%s'\n", types.Optional[*int]{})
    51  
    52  	fmt.Printf("'%+v'\n", types.Opt(""))
    53  	fmt.Printf("'%v'\n", types.Opt(""))
    54  	fmt.Printf("'%s'\n", types.Opt(""))
    55  	fmt.Printf("'%+v'\n", types.Opt(0))
    56  	fmt.Printf("'%v'\n", types.Opt(0))
    57  	fmt.Printf("'%s'\n", types.Opt(0))
    58  	fmt.Printf("'%+v'\n", types.Opt[*int](nil))
    59  	fmt.Printf("'%v'\n", types.Opt[*int](nil))
    60  	fmt.Printf("'%s'\n", types.Opt[*int](nil))
    61  
    62  	fmt.Printf("'%+v'\n", types.Opt("hello world"))
    63  	fmt.Printf("'%v'\n", types.Opt("hello world"))
    64  	fmt.Printf("'%s'\n", types.Opt("hello world"))
    65  	fmt.Printf("'%+v'\n", types.Opt(0xFF))
    66  	fmt.Printf("'%v'\n", types.Opt(0xFF))
    67  	fmt.Printf("'%s'\n", types.Opt(0xFF))
    68  	var p int
    69  	fmt.Printf("'%s'\n", fmt.Sprintf("%+v", types.Opt(&p))[:2])
    70  	fmt.Printf("'%s'\n", fmt.Sprintf("%v", types.Opt(&p))[:2])
    71  	fmt.Printf("'%s'\n", fmt.Sprintf("%s", types.Opt(&p))[:11])
    72  
    73  	// Output:
    74  	// 'null string: '
    75  	// 'null'
    76  	// 'null'
    77  	// 'null int: 0'
    78  	// 'null'
    79  	// 'null'
    80  	// 'null *int: <nil>'
    81  	// 'null'
    82  	// 'null'
    83  	// ''
    84  	// ''
    85  	// ''
    86  	// '0'
    87  	// '0'
    88  	// '%!s(int=0)'
    89  	// '<nil>'
    90  	// '<nil>'
    91  	// '%!s(*int=<nil>)'
    92  	// 'hello world'
    93  	// 'hello world'
    94  	// 'hello world'
    95  	// '255'
    96  	// '255'
    97  	// '%!s(int=255)'
    98  	// '0x'
    99  	// '0x'
   100  	// '%!s(*int=0x'
   101  }
   102  
   103  func TestOptional_String(t *testing.T) {
   104  	tests := []struct {
   105  		a    types.Optional[int]
   106  		want string
   107  	}{
   108  		{types.Optional[int]{}, "null"},
   109  		{types.Optional[int]{}, "null"},
   110  		{types.Opt[int](1), "1"},
   111  		{types.Opt[int](0), "0"},
   112  	}
   113  	for i, tt := range tests {
   114  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   115  			{
   116  				got := tt.a.String()
   117  				if got != tt.want {
   118  					t.Errorf("%v.String() got (%v), want (%v)", tt.a, got, tt.want)
   119  				}
   120  			}
   121  		})
   122  	}
   123  
   124  }
   125  
   126  func TestCompareOptional(t *testing.T) {
   127  	tests := []struct {
   128  		a, b types.Optional[int]
   129  		want int
   130  	}{
   131  		{types.Optional[int]{}, types.Optional[int]{}, 0},
   132  		{types.Optional[int]{}, types.Opt[int](1), -1},
   133  		{types.Opt[int](1), types.Optional[int]{}, 1},
   134  		{types.Opt[int](1), types.Opt[int](1), 0},
   135  		{types.Opt[int](0), types.Opt[int](1), -1},
   136  		{types.Opt[int](1), types.Opt[int](0), 1},
   137  	}
   138  	for i, tt := range tests {
   139  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   140  			{
   141  				got := types.CompareOptional(tt.a, tt.b)
   142  				if got != tt.want {
   143  					t.Errorf("CompareOptional(%v, %v) got (%v), want (%v)", tt.a, tt.b, got, tt.want)
   144  				}
   145  			}
   146  		})
   147  	}
   148  }