This commit is contained in:
crusader 2018-07-17 20:44:21 +09:00
parent b37874d849
commit cfbf5e8933
2 changed files with 116 additions and 7 deletions

View File

@ -156,10 +156,3 @@ LOOP:
} }
return fields return fields
} }
var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil))
type InjectableAnnotation struct {
Annotation `@name:"@Component"`
Name string `json:"name" @default:"test"`
}

View File

@ -5,6 +5,78 @@ import (
"testing" "testing"
) )
var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil))
type InjectableAnnotation struct {
Annotation `@name:"@Injectable"`
Name string `json:"name" @default:""`
}
var TestServiceType = reflect.TypeOf((*TestService)(nil))
type TestService struct {
TypeAnnotation `annotation:"@Injectable('name': 'TestService')"`
}
func TestNew(t *testing.T) {
type args struct {
parent Registry
}
tests := []struct {
name string
args args
want Registry
}{
{
name: "New of nil",
args: args{
parent: nil,
},
want: New(nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.parent); !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
func TestRegister(t *testing.T) {
type args struct {
t reflect.Type
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Register Injectable",
args: args{
t: InjectableAnnotationType,
},
wantErr: false,
},
{
name: "Register duplicate Injectable",
args: args{
t: InjectableAnnotationType,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Register(tt.args.t); (err != nil) != tt.wantErr {
t.Errorf("Register() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAnnotationRegistry_Register(t *testing.T) { func TestAnnotationRegistry_Register(t *testing.T) {
type fields struct { type fields struct {
parent Registry parent Registry
@ -34,6 +106,50 @@ func TestAnnotationRegistry_Register(t *testing.T) {
} }
} }
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",
args: args{
f: &f,
},
want: map[reflect.Type]Annotation{
TestServiceType: &InjectableAnnotation{
Name: "TestService",
},
},
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) { func TestAnnotationRegistry_Get(t *testing.T) {
type fields struct { type fields struct {
parent Registry parent Registry