github.com/coveo/gotemplate@v2.7.7+incompatible/collections/utils_test.go (about)

     1  package collections
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type empty struct{}
     9  
    10  func TestIsEmptyValue(t *testing.T) {
    11  	tests := []struct {
    12  		name string
    13  		arg  interface{}
    14  		want bool
    15  	}{
    16  		{"False", false, true},
    17  		{"True", true, false},
    18  		{"Nil", nil, true},
    19  		{"String", "Hello", false},
    20  		{"Empty string", "", true},
    21  		{"Zero", 0, true},
    22  		{"Uint", uint(10), false},
    23  		{"Floating point zero", 0.0, true},
    24  		{"Floating point", 10.0, false},
    25  		{"Floating point negative", -10.0, false},
    26  		{"Interface to nil", interface{}(nil), true},
    27  		{"Interface to zero", interface{}(0), true},
    28  		{"Interface to empty string", interface{}(""), true},
    29  		{"Interface to string", interface{}("Foo"), false},
    30  		{"Integer", 10, false},
    31  		{"Empty struct", empty{}, false},
    32  		{"Empty list", []string{}, true},
    33  		{"List", []string{"Hello"}, false},
    34  		{"Empty map", map[string]int{}, true},
    35  		{"Map", map[string]int{"Hello": 1}, false},
    36  	}
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			if got := IsEmptyValue(reflect.ValueOf(tt.arg)); got != tt.want {
    40  				t.Errorf("IsEmptyValue() = %v, want %v", got, tt.want)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func TestIsExported(t *testing.T) {
    47  	tests := []struct {
    48  		name string
    49  		id   string
    50  		want bool
    51  	}{
    52  		{"Private", "privateValue", false},
    53  		{"Public", "PublicValue", true},
    54  		{"Zero number", "0", false},
    55  		{"Positive number", "1", false},
    56  		{"Underscore", "_test", false},
    57  		{"Id with space", "ID 1", true},
    58  		{"AllCap", "ALL_CAP_ID", true},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			if got := IsExported(tt.id); got != tt.want {
    63  				t.Errorf("IsExported() = %v, want %v", got, tt.want)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func TestIfUndef(t *testing.T) {
    70  	const def = "default"
    71  	type empty struct{}
    72  	tests := []struct {
    73  		name string
    74  		arg  interface{}
    75  		want interface{}
    76  	}{
    77  		{"False", false, false},
    78  		{"True", true, true},
    79  		{"Nil", nil, def},
    80  		{"String", "Hello", "Hello"},
    81  		{"Empty string", "", ""},
    82  		{"Zero", 0, 0},
    83  		{"Integer", 10, 10},
    84  		{"Empty struct", empty{}, empty{}},
    85  		{"Empty list", []string{}, []string{}},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			if got := IfUndef(def, tt.arg); !reflect.DeepEqual(got, tt.want) {
    90  				t.Errorf("IfUndef() = %v, want %v", got, tt.want)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  func TestIfUndefNoValue(t *testing.T) {
    97  	def := "default"
    98  	t.Run("No Value", func(t *testing.T) {
    99  		if got := IfUndef(def); !reflect.DeepEqual(got, def) {
   100  			t.Errorf("IfUndef() = %v, want %v", got, def)
   101  		}
   102  	})
   103  }
   104  
   105  func TestIfManyValues(t *testing.T) {
   106  	const def = "default"
   107  	list := []interface{}{1, 2, 3}
   108  	t.Run("Many values", func(t *testing.T) {
   109  		if got := IfUndef(def, list...); !reflect.DeepEqual(got, list) {
   110  			t.Errorf("IfUndef() = %v, want %v", got, list)
   111  		}
   112  	})
   113  }
   114  
   115  func TestIIf(t *testing.T) {
   116  	tests := []struct {
   117  		name      string
   118  		testValue interface{}
   119  		want      interface{}
   120  	}{
   121  		{"False", false, 2},
   122  		{"True", true, 1},
   123  		{"Nil", nil, 2},
   124  		{"String", "Hello", 1},
   125  		{"Empty string", "", 2},
   126  		{"Zero", 0, 2},
   127  		{"Integer", 10, 1},
   128  		{"Empty struct", empty{}, 1},
   129  		{"Empty list", []string{}, 2},
   130  		{"List", []string{"Hello"}, 1},
   131  		{"Empty map", map[string]int{}, 2},
   132  		{"Map", map[string]int{"Hello": 1}, 1},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			if got := IIf(tt.testValue, 1, 2); !reflect.DeepEqual(got, tt.want) {
   137  				t.Errorf("IIf() = %v, want %v", got, tt.want)
   138  			}
   139  		})
   140  	}
   141  }