From 61488ba7adaeaede358cd987f2647a7a18cdd6e0 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 23 Aug 2018 15:29:34 +0900 Subject: [PATCH] ing --- registry_test.go | 736 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 630 insertions(+), 106 deletions(-) diff --git a/registry_test.go b/registry_test.go index 7394885..48fe6bd 100644 --- a/registry_test.go +++ b/registry_test.go @@ -3,19 +3,24 @@ package annotation import ( "reflect" "testing" + + di "git.loafle.net/overflow/di-go" ) var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil)) type InjectableAnnotation struct { - Annotation `@name:"@Injectable"` - Name string `json:"name" @default:""` + Annotation `@name:"@Injectable"` + 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 { - TypeAnnotation `annotation:"@Injectable('name': 'TestService')"` +type InjectableService struct { + TypeAnnotation `annotation:"@Injectable('name': 'InjectableService')"` } func TestNew(t *testing.T) { @@ -27,13 +32,7 @@ func TestNew(t *testing.T) { args args want Registry }{ - { - name: "New of nil", - args: args{ - parent: nil, - }, - want: New(nil), - }, + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -53,20 +52,7 @@ func TestRegister(t *testing.T) { args args wantErr bool }{ - { - name: "Register Injectable", - args: args{ - t: InjectableAnnotationType, - }, - wantErr: false, - }, - { - name: "Register duplicate Injectable", - args: args{ - t: InjectableAnnotationType, - }, - wantErr: true, - }, + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -79,8 +65,9 @@ func TestRegister(t *testing.T) { func TestAnnotationRegistry_Register(t *testing.T) { type fields struct { - parent Registry - definitions map[string]*Definition + parent Registry + definitions map[string]*Definition + typeDefinitions map[reflect.Type]*TypeDefinition } type args struct { t reflect.Type @@ -92,94 +79,28 @@ func TestAnnotationRegistry_Register(t *testing.T) { 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, - } - 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", - args: args{ - f: &f, + name: "@Injectable", + fields: fields{ + parent: nil, + definitions: make(map[string]*Definition, 0), + typeDefinitions: make(map[reflect.Type]*TypeDefinition, 0), }, - want: map[reflect.Type]Annotation{ - TestServiceType: &InjectableAnnotation{ - Name: "TestService", - }, + args: args{ + t: InjectableAnnotationType, }, 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 { t.Run(tt.name, func(t *testing.T) { r := &AnnotationRegistry{ - parent: tt.fields.parent, - definitions: tt.fields.definitions, + parent: tt.fields.parent, + definitions: tt.fields.definitions, + typeDefinitions: tt.fields.typeDefinitions, } - got, err := r.Get(tt.args.f) - if (err != nil) != 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) + if err := r.Register(tt.args.t); (err != nil) != tt.wantErr { + t.Errorf("AnnotationRegistry.Register() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -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) + } + }) + } +}