This commit is contained in:
crusader 2018-08-23 15:29:34 +09:00
parent 08db48ce48
commit 61488ba7ad

View File

@ -3,6 +3,8 @@ package annotation
import ( import (
"reflect" "reflect"
"testing" "testing"
di "git.loafle.net/overflow/di-go"
) )
var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil)) var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil))
@ -10,12 +12,15 @@ var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil))
type InjectableAnnotation struct { type InjectableAnnotation struct {
Annotation `@name:"@Injectable"` Annotation `@name:"@Injectable"`
Name string `json:"name" @default:""` Name string `json:"name" @default:""`
InitMethod string `json:"initMethod"`
DestroyMethod string `json:"destroyMethod"`
Scope di.ScopeType `json:"scope"`
} }
var TestServiceType = reflect.TypeOf((*TestService)(nil)) var InjectableServiceType = reflect.TypeOf((*InjectableService)(nil))
type TestService struct { type InjectableService struct {
TypeAnnotation `annotation:"@Injectable('name': 'TestService')"` TypeAnnotation `annotation:"@Injectable('name': 'InjectableService')"`
} }
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
@ -27,13 +32,7 @@ func TestNew(t *testing.T) {
args args args args
want Registry want Registry
}{ }{
{ // TODO: Add test cases.
name: "New of nil",
args: args{
parent: nil,
},
want: New(nil),
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -53,20 +52,7 @@ func TestRegister(t *testing.T) {
args args args args
wantErr bool wantErr bool
}{ }{
{ // TODO: Add test cases.
name: "Register Injectable",
args: args{
t: InjectableAnnotationType,
},
wantErr: false,
},
{
name: "Register duplicate Injectable",
args: args{
t: InjectableAnnotationType,
},
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -81,6 +67,7 @@ func TestAnnotationRegistry_Register(t *testing.T) {
type fields struct { type fields struct {
parent Registry parent Registry
definitions map[string]*Definition definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
} }
type args struct { type args struct {
t reflect.Type t reflect.Type
@ -92,94 +79,28 @@ func TestAnnotationRegistry_Register(t *testing.T) {
wantErr bool wantErr bool
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
}
if err := r.Register(tt.args.t); (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.Register() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGet(t *testing.T) {
Register(InjectableAnnotationType)
ts := TestService{}
tsT := reflect.TypeOf(ts)
f, _ := tsT.FieldByName("TypeAnnotation")
type args struct {
f *reflect.StructField
}
tests := []struct {
name string
args args
want map[reflect.Type]Annotation
wantErr bool
}{
{ {
name: "Get TestService", name: "@Injectable",
fields: fields{
parent: nil,
definitions: make(map[string]*Definition, 0),
typeDefinitions: make(map[reflect.Type]*TypeDefinition, 0),
},
args: args{ args: args{
f: &f, t: InjectableAnnotationType,
},
want: map[reflect.Type]Annotation{
TestServiceType: &InjectableAnnotation{
Name: "TestService",
},
}, },
wantErr: false, wantErr: false,
}, },
} }
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Get(tt.args.f)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_Get(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
}
type args struct {
f *reflect.StructField
}
tests := []struct {
name string
fields fields
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{ r := &AnnotationRegistry{
parent: tt.fields.parent, parent: tt.fields.parent,
definitions: tt.fields.definitions, definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
} }
got, err := r.Get(tt.args.f) if err := r.Register(tt.args.t); (err != nil) != tt.wantErr {
if (err != nil) != tt.wantErr { t.Errorf("AnnotationRegistry.Register() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("AnnotationRegistry.Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.Get() = %v, want %v", got, tt.want)
} }
}) })
} }
@ -206,3 +127,606 @@ func Test_findAnnotatedFields(t *testing.T) {
}) })
} }
} }
func TestAnnotationRegistry_getAnnotation(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
f *reflect.StructField
}
tests := []struct {
name string
fields fields
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.getAnnotation(tt.args.f)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.getAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.getAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_getTypeDefinition(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
{
name: "InjectableService",
fields: fields{
parent: nil,
definitions: make(map[string]*Definition, 0),
typeDefinitions: make(map[reflect.Type]*TypeDefinition, 0),
},
args: args{
t: InjectableServiceType,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
r.Register(InjectableAnnotationType)
got, err := r.getTypeDefinition(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.getTypeDefinition() error = %v, wantErr %v", err, tt.wantErr)
return
}
t.Log(got)
})
}
}
func TestGetTypeAnnotation(t *testing.T) {
type args struct {
t reflect.Type
at reflect.Type
}
tests := []struct {
name string
args args
want Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetTypeAnnotation(tt.args.t, tt.args.at)
if (err != nil) != tt.wantErr {
t.Errorf("GetTypeAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetTypeAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetTypeAnnotation(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
at reflect.Type
}
tests := []struct {
name string
fields fields
args args
want Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetTypeAnnotation(tt.args.t, tt.args.at)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetTypeAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetTypeAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetTypeAnnotations(t *testing.T) {
type args struct {
t reflect.Type
}
tests := []struct {
name string
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetTypeAnnotations(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("GetTypeAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetTypeAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetTypeAnnotations(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
}
tests := []struct {
name string
fields fields
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetTypeAnnotations(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetTypeAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetTypeAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetFieldAnnotation(t *testing.T) {
type args struct {
t reflect.Type
name string
at reflect.Type
}
tests := []struct {
name string
args args
want Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetFieldAnnotation(tt.args.t, tt.args.name, tt.args.at)
if (err != nil) != tt.wantErr {
t.Errorf("GetFieldAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetFieldAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetFieldAnnotation(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
name string
at reflect.Type
}
tests := []struct {
name string
fields fields
args args
want Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetFieldAnnotation(tt.args.t, tt.args.name, tt.args.at)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetFieldAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetFieldAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetFieldAnnotations(t *testing.T) {
type args struct {
t reflect.Type
name string
}
tests := []struct {
name string
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetFieldAnnotations(tt.args.t, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("GetFieldAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetFieldAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetFieldAnnotations(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
name string
}
tests := []struct {
name string
fields fields
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetFieldAnnotations(tt.args.t, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetFieldAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetFieldAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetAllFieldAnnotations(t *testing.T) {
type args struct {
t reflect.Type
}
tests := []struct {
name string
args args
want map[string]map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllFieldAnnotations(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllFieldAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllFieldAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetAllFieldAnnotations(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
}
tests := []struct {
name string
fields fields
args args
want map[string]map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetAllFieldAnnotations(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetAllFieldAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetAllFieldAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetMethodAnnotation(t *testing.T) {
type args struct {
t reflect.Type
name string
at reflect.Type
}
tests := []struct {
name string
args args
want Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetMethodAnnotation(tt.args.t, tt.args.name, tt.args.at)
if (err != nil) != tt.wantErr {
t.Errorf("GetMethodAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetMethodAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetMethodAnnotation(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
name string
at reflect.Type
}
tests := []struct {
name string
fields fields
args args
want Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetMethodAnnotation(tt.args.t, tt.args.name, tt.args.at)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetMethodAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetMethodAnnotation() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetMethodAnnotations(t *testing.T) {
type args struct {
t reflect.Type
name string
}
tests := []struct {
name string
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetMethodAnnotations(tt.args.t, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("GetMethodAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetMethodAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetMethodAnnotations(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
name string
}
tests := []struct {
name string
fields fields
args args
want map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetMethodAnnotations(tt.args.t, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetMethodAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetMethodAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetAllMethodAnnotations(t *testing.T) {
type args struct {
t reflect.Type
}
tests := []struct {
name string
args args
want map[string]map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllMethodAnnotations(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllMethodAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllMethodAnnotations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAnnotationRegistry_GetAllMethodAnnotations(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
}
type args struct {
t reflect.Type
}
tests := []struct {
name string
fields fields
args args
want map[string]map[reflect.Type]Annotation
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &AnnotationRegistry{
parent: tt.fields.parent,
definitions: tt.fields.definitions,
typeDefinitions: tt.fields.typeDefinitions,
}
got, err := r.GetAllMethodAnnotations(tt.args.t)
if (err != nil) != tt.wantErr {
t.Errorf("AnnotationRegistry.GetAllMethodAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AnnotationRegistry.GetAllMethodAnnotations() = %v, want %v", got, tt.want)
}
})
}
}