package main import ( "reflect" "testing" ) type TS struct { id int name string } type TestStruct struct { Str string Int int Float64 float64 Slice []complex128 Bool bool Uint uint Ary [2]string Value TS Pointer *TS SliceTestStruct []*TS MapTestStruct map[string][]*TS } func TestValidateEmptyStructValue(t *testing.T) { testcases := []struct { name string typ interface{} wantErr string unexpectedErr bool }{ { name: "valid struct pointer", typ: &TestStruct{}, }, { name: "struct value", typ: &TestStruct{}, wantErr: "must pass a empty struct pointer", }, { name: "int", typ: 10, wantErr: "must pass a empty struct pointer", }, { name: "string", typ: "10", wantErr: "must pass a empty struct pointer", }, { name: "nil", typ: nil, wantErr: "must pass a empty struct pointer", }, { name: "non empty struct (string)", typ: &TestStruct{Str: "Hello"}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (int)", typ: &TestStruct{Int: 10}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (float64)", typ: &TestStruct{Float64: 30.5}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct ([]complex128])", typ: &TestStruct{Slice: []complex128{-23, 1i}}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (bool)", typ: &TestStruct{Bool: true}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (array)", typ: &TestStruct{Ary: [2]string{}}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (struct value)", typ: &TestStruct{Value: TS{id: 1}}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (struct pointer)", typ: &TestStruct{Pointer: &TS{}}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (slice of struct pointer)", typ: &TestStruct{SliceTestStruct: make([]*TS, 1)}, wantErr: "must pass a empty struct pointer, you passed containing values", }, { name: "non empty struct (map of slice of struct pointer)", typ: &TestStruct{MapTestStruct: make(map[string][]*TS, 1)}, wantErr: "must pass a empty struct pointer, you passed containing values", }, } for _, testcase := range testcases { t.Run(testcase.name, func(t *testing.T) { v := reflect.ValueOf(testcase.typ) err := validateEmptyStructValue(v) if err != nil { if err.Error() != testcase.wantErr { t.Fatalf(`expected "%s", but got "%s"`, testcase.wantErr, err) } } }) } } func Test_validateSendChannel(t *testing.T) { testcases := []struct { name string typ interface{} wantErr string }{ { name: "channel", typ: make(chan struct{}), }, { name: "send channel", typ: make(chan<- struct{}), }, { name: "another type", typ: 10, wantErr: `must pass a channel which is send direction (got "int")`, }, { name: "nil", typ: nil, wantErr: `must pass a channel which is send direction (got "nil")`, }, { name: "recv channel", typ: make(<-chan struct{}), wantErr: "must pass a channel which is send direction", }, } for _, testcase := range testcases { t.Run(testcase.name, func(t *testing.T) { v := reflect.ValueOf(testcase.typ) err := validateSendChannel(v) if err != nil { if err.Error() != testcase.wantErr { t.Fatalf(`expected "%s", but got "%s"`, testcase.wantErr, err) } } }) } }