diff --git a/registry.go b/registry.go index 6abbf0e..24b6a3c 100644 --- a/registry.go +++ b/registry.go @@ -156,10 +156,3 @@ LOOP: } return fields } - -var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil)) - -type InjectableAnnotation struct { - Annotation `@name:"@Component"` - Name string `json:"name" @default:"test"` -} diff --git a/registry_test.go b/registry_test.go index 78efd0b..7394885 100644 --- a/registry_test.go +++ b/registry_test.go @@ -5,6 +5,78 @@ import ( "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) { type fields struct { 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) { type fields struct { parent Registry