123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 |
- package generators
- import (
- "reflect"
- "testing"
- "go-common/app/tool/gengo/types"
- )
- func Test_isRootedUnder(t *testing.T) {
- testCases := []struct {
- path string
- roots []string
- expect bool
- }{
- {
- path: "/foo/bar",
- roots: nil,
- expect: false,
- },
- {
- path: "/foo/bar",
- roots: []string{},
- expect: false,
- },
- {
- path: "/foo/bar",
- roots: []string{
- "/bad",
- },
- expect: false,
- },
- {
- path: "/foo/bar",
- roots: []string{
- "/foo",
- },
- expect: true,
- },
- {
- path: "/foo/bar",
- roots: []string{
- "/bad",
- "/foo",
- },
- expect: true,
- },
- {
- path: "/foo/bar/qux/zorb",
- roots: []string{
- "/foo/bar/qux",
- },
- expect: true,
- },
- {
- path: "/foo/bar",
- roots: []string{
- "/foo/bar",
- },
- expect: true,
- },
- {
- path: "/foo/barn",
- roots: []string{
- "/foo/bar",
- },
- expect: false,
- },
- {
- path: "/foo/bar",
- roots: []string{
- "/foo/barn",
- },
- expect: false,
- },
- {
- path: "/foo/bar",
- roots: []string{
- "",
- },
- expect: true,
- },
- }
- for i, tc := range testCases {
- r := isRootedUnder(tc.path, tc.roots)
- if r != tc.expect {
- t.Errorf("case[%d]: expected %t, got %t for %q in %q", i, tc.expect, r, tc.path, tc.roots)
- }
- }
- }
- func Test_deepCopyMethod(t *testing.T) {
- testCases := []struct {
- typ types.Type
- expect bool
- error bool
- }{
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- // No DeepCopy method.
- Methods: map[string]*types.Type{},
- },
- expect: false,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // No DeepCopy method.
- "method": {
- Name: types.Name{Package: "pkgname", Name: "func()"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: false,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (no result).
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func()"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (wrong result).
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func() int"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{
- {
- Name: types.Name{Name: "int"},
- Kind: types.Builtin,
- },
- },
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature with pointer receiver, but non-pointer result.
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{
- {
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- },
- },
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature with non-pointer receiver, but pointer result.
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- Parameters: []*types.Type{},
- Results: []*types.Type{
- {
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- },
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Correct signature with non-pointer receiver.
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- Parameters: []*types.Type{},
- Results: []*types.Type{
- {
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- },
- },
- },
- },
- },
- },
- expect: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Correct signature with pointer receiver.
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{
- {
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- },
- },
- },
- },
- },
- expect: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (has params).
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func(int) pkgname.typename"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{
- {
- Name: types.Name{Name: "int"},
- Kind: types.Builtin,
- },
- },
- Results: []*types.Type{
- {
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- },
- },
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (extra results).
- "DeepCopy": {
- Name: types.Name{Package: "pkgname", Name: "func() (pkgname.typename, int)"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{
- {
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- },
- {
- Name: types.Name{Name: "int"},
- Kind: types.Builtin,
- },
- },
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- }
- for i, tc := range testCases {
- r, err := deepCopyMethod(&tc.typ)
- if tc.error && err == nil {
- t.Errorf("case[%d]: expected an error, got none", i)
- } else if !tc.error && err != nil {
- t.Errorf("case[%d]: expected no error, got: %v", i, err)
- } else if !tc.error && (r != nil) != tc.expect {
- t.Errorf("case[%d]: expected result %v, got: %v", i, tc.expect, r)
- }
- }
- }
- func Test_deepCopyIntoMethod(t *testing.T) {
- testCases := []struct {
- typ types.Type
- expect bool
- error bool
- }{
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- // No DeepCopyInto method.
- Methods: map[string]*types.Type{},
- },
- expect: false,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // No DeepCopyInto method.
- "method": {
- Name: types.Name{Package: "pkgname", Name: "func()"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: false,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (no parameter).
- "DeepCopyInto": {
- Name: types.Name{Package: "pkgname", Name: "func()"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{},
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (unexpected result).
- "DeepCopyInto": {
- Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename) int"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{
- {
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- },
- Results: []*types.Type{
- {
- Name: types.Name{Name: "int"},
- Kind: types.Builtin,
- },
- },
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (non-pointer parameter, pointer receiver).
- "DeepCopyInto": {
- Name: types.Name{Package: "pkgname", Name: "func(pkgname.typename)"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{
- {Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Wrong signature (non-pointer parameter, non-pointer receiver).
- "DeepCopyInto": {
- Name: types.Name{Package: "pkgname", Name: "func(pkgname.typename)"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- Parameters: []*types.Type{
- {Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: false,
- error: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Correct signature with non-pointer receiver.
- "DeepCopyInto": {
- Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename)"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- Parameters: []*types.Type{
- {
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- },
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: true,
- },
- {
- typ: types.Type{
- Name: types.Name{Package: "pkgname", Name: "typename"},
- Kind: types.Builtin,
- Methods: map[string]*types.Type{
- // Correct signature with pointer receiver.
- "DeepCopyInto": {
- Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename)"},
- Kind: types.Func,
- Signature: &types.Signature{
- Receiver: &types.Type{
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- Parameters: []*types.Type{
- {
- Kind: types.Pointer,
- Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
- },
- },
- Results: []*types.Type{},
- },
- },
- },
- },
- expect: true,
- },
- }
- for i, tc := range testCases {
- r, err := deepCopyIntoMethod(&tc.typ)
- if tc.error && err == nil {
- t.Errorf("case[%d]: expected an error, got none", i)
- } else if !tc.error && err != nil {
- t.Errorf("case[%d]: expected no error, got: %v", i, err)
- } else if !tc.error && (r != nil) != tc.expect {
- t.Errorf("case[%d]: expected result %v, got: %v", i, tc.expect, r)
- }
- }
- }
- func Test_extractTagParams(t *testing.T) {
- testCases := []struct {
- comments []string
- expect *tagValue
- }{
- {
- comments: []string{
- "Human comment",
- },
- expect: nil,
- },
- {
- comments: []string{
- "Human comment",
- "+bili:deepcopy-gen",
- },
- expect: &tagValue{
- value: "",
- register: false,
- },
- },
- {
- comments: []string{
- "Human comment",
- "+bili:deepcopy-gen=package",
- },
- expect: &tagValue{
- value: "package",
- register: false,
- },
- },
- {
- comments: []string{
- "Human comment",
- "+bili:deepcopy-gen=package,register",
- },
- expect: &tagValue{
- value: "package",
- register: true,
- },
- },
- {
- comments: []string{
- "Human comment",
- "+bili:deepcopy-gen=package,register=true",
- },
- expect: &tagValue{
- value: "package",
- register: true,
- },
- },
- {
- comments: []string{
- "Human comment",
- "+bili:deepcopy-gen=package,register=false",
- },
- expect: &tagValue{
- value: "package",
- register: false,
- },
- },
- }
- for i, tc := range testCases {
- r := extractTag(tc.comments)
- if r == nil && tc.expect != nil {
- t.Errorf("case[%d]: expected non-nil", i)
- }
- if r != nil && tc.expect == nil {
- t.Errorf("case[%d]: expected nil, got %v", i, *r)
- }
- if r != nil && *r != *tc.expect {
- t.Errorf("case[%d]: expected %v, got %v", i, *tc.expect, *r)
- }
- }
- }
- func Test_extractInterfacesTag(t *testing.T) {
- testCases := []struct {
- comments []string
- expect []string
- }{
- {
- comments: []string{},
- expect: nil,
- },
- {
- comments: []string{
- "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
- },
- expect: []string{
- "k8s.io/kubernetes/runtime.Object",
- },
- },
- {
- comments: []string{
- "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
- "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.List",
- },
- expect: []string{
- "k8s.io/kubernetes/runtime.Object",
- "k8s.io/kubernetes/runtime.List",
- },
- },
- {
- comments: []string{
- "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
- "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
- },
- expect: []string{
- "k8s.io/kubernetes/runtime.Object",
- "k8s.io/kubernetes/runtime.Object",
- },
- },
- }
- for i, tc := range testCases {
- r := extractInterfacesTag(tc.comments)
- if r == nil && tc.expect != nil {
- t.Errorf("case[%d]: expected non-nil", i)
- }
- if r != nil && tc.expect == nil {
- t.Errorf("case[%d]: expected nil, got %v", i, r)
- }
- if r != nil && !reflect.DeepEqual(r, tc.expect) {
- t.Errorf("case[%d]: expected %v, got %v", i, tc.expect, r)
- }
- }
- }
|