github.com/searKing/golang/go@v1.2.74/util/function/binary/bi_function_test.go (about)

     1  // Copyright 2020 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 binary_test
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/searKing/golang/go/util/function"
    12  	"github.com/searKing/golang/go/util/function/binary"
    13  )
    14  
    15  type BiFunctionTestInput struct {
    16  	apply  func(t interface{}, u interface{}) interface{}
    17  	afters []func(t interface{}) interface{}
    18  	t      interface{}
    19  	u      interface{}
    20  }
    21  type BiFunctionTest struct {
    22  	input  BiFunctionTestInput
    23  	output interface{}
    24  }
    25  
    26  var biFunctionTests = []BiFunctionTest{
    27  	{
    28  		input: BiFunctionTestInput{
    29  			apply: func(t interface{}, u interface{}) interface{} {
    30  				return t.(int) + u.(int)
    31  			},
    32  			t: 1,
    33  			u: 2,
    34  		},
    35  		output: 3,
    36  	},
    37  	{
    38  		input: BiFunctionTestInput{
    39  			apply: func(t interface{}, u interface{}) interface{} {
    40  				return t.(string) + u.(string)
    41  			},
    42  			afters: []func(t interface{}) interface{}{func(t interface{}) interface{} {
    43  				return strings.ToUpper(t.(string))
    44  			}, func(t interface{}) interface{} {
    45  				return t.(string) + "c"
    46  			}},
    47  			t: "a",
    48  			u: "b",
    49  		},
    50  		output: "ABc",
    51  	},
    52  }
    53  
    54  func TestBiFunction(t *testing.T) {
    55  	for n, test := range biFunctionTests {
    56  		var bi binary.BiFunction = binary.BiFunctionFunc(test.input.apply)
    57  		for _, after := range test.input.afters {
    58  			bi = bi.AndThen(function.FunctionFunc(after))
    59  		}
    60  		got := bi.Apply(test.input.t, test.input.u)
    61  		if got != test.output {
    62  			t.Errorf("#%d: %v: got %v runs; expected %v", n, test.input, got, test.output)
    63  		}
    64  	}
    65  }