github.com/podhmo/reflect-shape@v0.4.3/is_zero_test.go (about)

     1  package reflectshape_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	reflectshape "github.com/podhmo/reflect-shape"
     8  )
     9  
    10  func TestIsZeroRecursive(t *testing.T) {
    11  	type S struct {
    12  		Name string
    13  		Age  int
    14  	}
    15  
    16  	type W struct {
    17  		Name string
    18  		S    S
    19  	}
    20  
    21  	tests := []struct {
    22  		name string
    23  		v    interface{}
    24  		want bool
    25  	}{
    26  		{"zero-int", 0, true},
    27  		{"int", 1, false},
    28  		{"zero-string", "", true},
    29  		{"string", "x", false},
    30  		// {"nil", nil, true}, // panic
    31  		{"nil-slice", func() []int { return nil }(), true},
    32  		{"empty-slice", func() []int { return []int{} }(), false},
    33  		{"nil-map", func() map[int]int { return nil }(), true},
    34  		{"empty-slice", func() map[int]int { return map[int]int{} }(), false},
    35  		// struct
    36  		{"zero-struct", S{}, true},
    37  		{"zero-struct-pointer", &S{}, true},
    38  		{"not-zero-struct1", S{Name: "foo"}, false},
    39  		{"not-zero-struct2", S{Age: 20}, false},
    40  		// recursive
    41  		{"zero-rec-struct", W{}, true},
    42  		{"zero-rec-struct2", W{S: S{}}, true},
    43  		{"not-zero-rec-struct", W{S: S{Age: 20}}, false},
    44  	}
    45  	for _, tt := range tests {
    46  		rt := reflect.TypeOf(tt.v)
    47  		rv := reflect.ValueOf(tt.v)
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			if got := reflectshape.IsZeroRecursive(rt, rv); got != tt.want {
    50  				t.Errorf("IsZeroRecursive() = %v, want %v", got, tt.want)
    51  			}
    52  		})
    53  	}
    54  }