github.com/coveo/gotemplate@v2.7.7+incompatible/template/math_utilities_test.go (about)

     1  package template
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/coveo/gotemplate/collections"
     8  	"github.com/coveo/gotemplate/collections/implementation"
     9  	"github.com/coveo/gotemplate/json"
    10  )
    11  
    12  type a = []interface{}
    13  type l = implementation.ListTypeName
    14  type j = json.List
    15  
    16  func Test_convertArgs(t *testing.T) {
    17  	// t.Parallel()
    18  	collections.DictionaryHelper = implementation.DictionaryHelper
    19  	collections.ListHelper = implementation.GenericListHelper
    20  	tests := []struct {
    21  		name string
    22  		arg1 interface{}
    23  		args a
    24  		want iList
    25  	}{
    26  		{"Nil", nil, nil, l{}},
    27  		{"Single int", 5, nil, l{5}},
    28  		{"Two int", 2, a{3}, l{2, 3}},
    29  		{"First nil", nil, a{3}, l{3}},
    30  		{"nil+values", nil, a{3, 4, 5}, l{3, 4, 5}},
    31  		{"nil+array", nil, a{a{3, 4, 5}}, l{3, 4, 5}},
    32  		{"array+nil", a{3, 4, 5}, nil, l{3, 4, 5}},
    33  		{"json+empty", j{3, 4, 5}, a{}, j{3, 4, 5}},
    34  		{"nil+json exp", nil, j{3, 4, 5}, l{3, 4, 5}},
    35  		{"nil+json list", nil, a{j{3, 4, 5}}, j{3, 4, 5}},
    36  		{"value+json exp", 2, j{3, 4}, l{2, 3, 4}},
    37  		{"value+json list", 2, a{j{3, 4}}, l{2, j{3, 4}}},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			if got := convertArgs(tt.arg1, tt.args...); !reflect.DeepEqual(got, tt.want) {
    42  				t.Errorf("convertArgs() = %[1]v (%[1]T), want %[2]v (%[2]T)", got, tt.want)
    43  			}
    44  		})
    45  	}
    46  }
    47  
    48  func Test_toListOfFloats(t *testing.T) {
    49  	// t.Parallel()
    50  	collections.DictionaryHelper = implementation.DictionaryHelper
    51  	collections.ListHelper = implementation.GenericListHelper
    52  	tests := []struct {
    53  		name       string
    54  		values     iList
    55  		wantResult iList
    56  		wantErr    bool
    57  	}{
    58  		{"Nil", nil, l{}, false},
    59  		{"Empty", l{}, l{}, false},
    60  		{"Array of int", l{1, 2, 3}, l{float64(1), float64(2), float64(3)}, false},
    61  		{"Array of string", l{"1", "2", "3"}, l{float64(1), float64(2), float64(3)}, false},
    62  		{"Invalid value", l{"1", "bad"}, nil, true},
    63  		{"Json list", l{j{1.2, 2.3}}, j{1.2, 2.3}, false},
    64  		{"Two json list", l{j{1.2, 2.3}, j{3.4}}, nil, true},
    65  	}
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			gotResult, err := toListOfFloats(tt.values)
    69  			if (err != nil) != tt.wantErr {
    70  				t.Errorf("toArrayOfFloats() error = %v, wantErr %v", err, tt.wantErr)
    71  				return
    72  			}
    73  			if !reflect.DeepEqual(gotResult, tt.wantResult) {
    74  				t.Errorf("toArrayOfFloats() = %v, want %v", gotResult, tt.wantResult)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func Test_asFloats(t *testing.T) {
    81  	t.Parallel()
    82  	type a = []interface{}
    83  	type l = implementation.ListTypeName
    84  	type j = json.List
    85  	tests := []struct {
    86  		name       string
    87  		values     iList
    88  		wantResult []float64
    89  		wantErr    bool
    90  	}{
    91  		{"Nil", nil, []float64{}, false},
    92  		{"Empty", l{}, []float64{}, false},
    93  		{"Array of int", l{1, 2, 3}, []float64{1, 2, 3}, false},
    94  		{"Array of string", l{"1", "2", "3"}, []float64{1, 2, 3}, false},
    95  		{"Invalid value", l{"1", "bad"}, nil, true},
    96  		{"Json list", l{j{1.2, 2.3}}, []float64{1.2, 2.3}, false},
    97  		{"Two json list", l{j{1.2, 2.3}, j{3.4}}, nil, true},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			gotResult, err := asFloats(tt.values)
   102  			if (err != nil) != tt.wantErr {
   103  				t.Errorf("toArrayOfFloats() error = %v, wantErr %v", err, tt.wantErr)
   104  				return
   105  			}
   106  			if !reflect.DeepEqual(gotResult, tt.wantResult) {
   107  				t.Errorf("toArrayOfFloats() = %v, want %v", gotResult, tt.wantResult)
   108  			}
   109  		})
   110  	}
   111  }