github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/boilerplate/expr_use_test.go (about)

     1  package boilerplate
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/m4gshm/gollections/expr/use"
     9  	"github.com/m4gshm/gollections/op/delay/sum"
    10  )
    11  
    12  var isEmpty = func(s string) func() bool { return func() bool { return len(s) == 0 } }
    13  
    14  func Test_UseSimple(t *testing.T) {
    15  
    16  	user := User{name: "Bob", surname: "Smith"}
    17  
    18  	fullName := use.If(len(user.surname) == 0, user.name).If(len(user.name) == 0, user.surname).
    19  		ElseGet(func() string { return user.name + " " + user.surname })
    20  
    21  	assert.Equal(t, "Bob Smith", fullName)
    22  
    23  }
    24  
    25  func Test_UseSimpleOld(t *testing.T) {
    26  
    27  	user := User{name: "Bob", surname: "Smith"}
    28  
    29  	fullName := ""
    30  	if len(user.surname) == 0 {
    31  		fullName = user.name
    32  	} else if len(user.name) == 0 {
    33  		fullName = user.surname
    34  	} else {
    35  		fullName = user.name + " " + user.surname
    36  	}
    37  
    38  	assert.Equal(t, "Bob Smith", fullName)
    39  
    40  }
    41  
    42  func Benchmark_UseIfElse(b *testing.B) {
    43  	user := User{name: "Bob", surname: "Smith"}
    44  
    45  	fullName := ""
    46  	for i := 0; i < b.N; i++ {
    47  		fullName = use.
    48  			If(len(user.surname) == 0, user.name).
    49  			If(len(user.name) == 0, user.surname).
    50  			ElseGet(func() string { return user.name + " " + user.surname })
    51  	}
    52  
    53  	assert.Equal(b, "Bob Smith", fullName)
    54  }
    55  
    56  func Benchmark_UseIfElseGetSumOf(b *testing.B) {
    57  	user := User{name: "Bob", surname: "Smith"}
    58  	fullName := ""
    59  	for i := 0; i < b.N; i++ {
    60  		fullName = use.
    61  			If(len(user.surname) == 0, user.name).
    62  			If(len(user.name) == 0, user.surname).
    63  			ElseGet(sum.Of(user.name, " ", user.surname))
    64  	}
    65  
    66  	assert.Equal(b, "Bob Smith", fullName)
    67  }
    68  
    69  func Benchmark_UseOtherElseGet(b *testing.B) {
    70  	user := User{name: "Bob", surname: "Smith"}
    71  	fullName := ""
    72  	for i := 0; i < b.N; i++ {
    73  		fullName = use.
    74  			If(len(user.surname) == 0, user.name).
    75  			Other(isEmpty(user.name), user.Surname).
    76  			ElseGet(sum.Of(user.name, " ", user.surname))
    77  	}
    78  
    79  	assert.Equal(b, "Bob Smith", fullName)
    80  }
    81  
    82  func Benchmark_UseSimpleOld(b *testing.B) {
    83  	user := User{name: "Bob", surname: "Smith"}
    84  	fullName := ""
    85  	for i := 0; i < b.N; i++ {
    86  		if len(user.surname) == 0 {
    87  			fullName = user.name
    88  		} else if len(user.name) == 0 {
    89  			fullName = user.surname
    90  		} else {
    91  			fullName = user.name + " " + user.surname
    92  		}
    93  	}
    94  	assert.Equal(b, "Bob Smith", fullName)
    95  }