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

     1  package template
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func Test_average(t *testing.T) {
     9  
    10  	tests := []struct {
    11  		name    string
    12  		arg1    interface{}
    13  		args    a
    14  		want    interface{}
    15  		wantErr bool
    16  	}{
    17  		{"Nil", nil, nil, nil, true},
    18  		{"First nil", nil, l{1, 2}, nil, true},
    19  		{"Single", 1, nil, int64(1), false},
    20  		{"Two values", 1, l{2}, 1.5, false},
    21  		{"With nil", 1, l{2, nil}, nil, true},
    22  	}
    23  	for _, tt := range tests {
    24  		t.Run(tt.name, func(t *testing.T) {
    25  			got, err := average(tt.arg1, tt.args...)
    26  			if (err != nil) != tt.wantErr {
    27  				t.Errorf("average() error = %v, wantErr %v", err, tt.wantErr)
    28  				return
    29  			}
    30  			if !reflect.DeepEqual(got, tt.want) {
    31  				t.Errorf("average() = %[1]v (%[1]T), want %[2]v (%[2]T)", got, tt.want)
    32  			}
    33  		})
    34  	}
    35  }
    36  
    37  func Test_min(t *testing.T) {
    38  	tests := []struct {
    39  		name   string
    40  		values l
    41  		want   interface{}
    42  	}{
    43  		{"Nil", nil, nil},
    44  		{"Zero", l{0}, int64(0)},
    45  		{"Single float", l{1.1}, 1.1},
    46  		{"Array of floats", l{1.1, 2.2, 3.3, 4}, 1.1},
    47  		{"Mixed array", l{1.1, 2.2, 3.3, "hello"}, "1.1"},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			if got := min(tt.values...); !reflect.DeepEqual(got, tt.want) {
    52  				t.Errorf("min() = %[1]v (%[1]T), want %[2]v (%[2]T)", got, tt.want)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func Test_max(t *testing.T) {
    59  	tests := []struct {
    60  		name   string
    61  		values l
    62  		want   interface{}
    63  	}{
    64  		{"Nil", nil, nil},
    65  		{"Zero", l{0}, int64(0)},
    66  		{"Single float", l{1.1}, 1.1},
    67  		{"Array of floats", l{1.1, 2.2, 3.3, 4}, int64(4)},
    68  		{"Mixed array", l{1.1, 2.2, 3.3, "hello"}, "hello"},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			if got := max(tt.values...); !reflect.DeepEqual(got, tt.want) {
    73  				t.Errorf("max() = %[1]v (%[1]T), want %[2]v (%[2]T)", got, tt.want)
    74  			}
    75  		})
    76  	}
    77  }