github.com/golodash/godash@v1.3.0/functions/wrap_func_test.go (about)

     1  package functions
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/golodash/godash/internal"
     9  )
    10  
    11  type TWrapFunc struct {
    12  	name     string
    13  	inputs   interface{}
    14  	function interface{}
    15  	expected interface{}
    16  }
    17  
    18  var tWrapFuncBenchs = []TWrapFunc{
    19  	{
    20  		name:   "10",
    21  		inputs: []interface{}{},
    22  	},
    23  	{
    24  		name:   "100",
    25  		inputs: []interface{}{},
    26  	},
    27  	{
    28  		name:   "1000",
    29  		inputs: []interface{}{},
    30  	},
    31  	{
    32  		name:   "10000",
    33  		inputs: []interface{}{},
    34  	},
    35  	{
    36  		name:   "100000",
    37  		inputs: []interface{}{},
    38  	},
    39  }
    40  
    41  func init() {
    42  	for j := 0; j < len(tWrapFuncBenchs); j++ {
    43  		length, _ := strconv.Atoi(tWrapFuncBenchs[j].name)
    44  		for i := 0; i < length/10; i++ {
    45  			tWrapFuncBenchs[j].inputs = append(tWrapFuncBenchs[j].inputs.([]interface{}), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    46  		}
    47  	}
    48  }
    49  
    50  func TestWrapFunc(t *testing.T) {
    51  	var tests = []TWrapFunc{
    52  		{
    53  			name:     "nil",
    54  			inputs:   nil,
    55  			function: nil,
    56  			expected: nil,
    57  		},
    58  		{
    59  			name:     "empty",
    60  			inputs:   []interface{}{},
    61  			function: func(inputs ...interface{}) {},
    62  			expected: nil,
    63  		},
    64  		{
    65  			name:     "empty-noInput",
    66  			inputs:   []interface{}{},
    67  			function: func() {},
    68  			expected: true,
    69  		},
    70  		{
    71  			name:     "inputsBigger-withVariadic",
    72  			inputs:   []interface{}{1, 2, 3, 4},
    73  			function: func(first interface{}, seconds ...interface{}) {},
    74  			expected: true,
    75  		},
    76  		{
    77  			name:     "inputsBigger-withoutVariadic",
    78  			inputs:   []interface{}{1, 2, 3, 4},
    79  			function: func(first, seconds interface{}) {},
    80  			expected: nil,
    81  		},
    82  		{
    83  			name:     "inFuncBigger-withVariadic-1",
    84  			inputs:   []interface{}{1, 2},
    85  			function: func(first, seconds interface{}, others ...interface{}) {},
    86  			expected: true,
    87  		},
    88  		{
    89  			name:     "inFuncBigger-withVariadic-2",
    90  			inputs:   []interface{}{1},
    91  			function: func(first, seconds interface{}, others ...interface{}) {},
    92  			expected: nil,
    93  		},
    94  		{
    95  			name:     "inFuncBigger-withoutVariadic",
    96  			inputs:   []interface{}{1},
    97  			function: func(first, seconds interface{}) {},
    98  			expected: nil,
    99  		},
   100  		{
   101  			name:     "sameLength-withVariadic",
   102  			inputs:   []interface{}{1, 2, 3},
   103  			function: func(first, seconds interface{}, others ...interface{}) {},
   104  			expected: true,
   105  		},
   106  		{
   107  			name:     "sameLength-withoutVariadic",
   108  			inputs:   []interface{}{1, 2},
   109  			function: func(first, seconds interface{}) {},
   110  			expected: true,
   111  		},
   112  	}
   113  
   114  	for _, subject := range tests {
   115  		t.Run(subject.name, func(t *testing.T) {
   116  			defer internal.DeferTestCases(t, subject.expected)
   117  			var got func() []interface{} = nil
   118  			if subject.inputs == nil {
   119  				got = WrapFunc(subject.function, nil)
   120  			} else {
   121  				got = WrapFunc(subject.function, subject.inputs.([]interface{})...)
   122  			}
   123  
   124  			if subject.expected != nil && got == nil {
   125  				t.Errorf("got = a function, wanted = %v", subject.expected)
   126  			} else if got != nil {
   127  				defer func() {
   128  					e := recover()
   129  					if e != nil {
   130  						panic("you are fucked")
   131  					}
   132  				}()
   133  				got()
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func benchFunc(inputs ...interface{}) {}
   140  
   141  func BenchmarkWrapFunc(b *testing.B) {
   142  	for j := 0; j < len(tWrapFuncBenchs); j++ {
   143  		b.Run(fmt.Sprintf("slice_size_%s", tWrapFuncBenchs[j].name), func(b *testing.B) {
   144  			inputs := tWrapFuncBenchs[j].inputs.([]interface{})
   145  			for i := 0; i < b.N; i++ {
   146  				WrapFunc(benchFunc, inputs...)
   147  			}
   148  		})
   149  	}
   150  }