annotation-go/registry_test.go

209 lines
4.1 KiB
Go
Raw Permalink Normal View History

2018-07-17 10:58:14 +00:00
package annotation
import (
"reflect"
"testing"
)
2018-07-17 11:44:21 +00:00
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)
}
})
}
}
2018-07-17 10:58:14 +00:00
func TestAnnotationRegistry_Register(t *testing.T) {
type fields struct {
parent Registry
definitions map[string]*Definition
}
type args struct {
t reflect.Type
}
tests := []struct {
name string
fields fields
args args
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)
}
})
}
}
2018-07-17 11:44:21 +00:00
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)
}
})
}
}
2018-07-17 10:58:14 +00:00
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,
}
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)
}
})
}
}
func Test_findAnnotatedFields(t *testing.T) {
type args struct {
t reflect.Type
ft reflect.Type
deep bool
}
tests := []struct {
name string
args args
want map[string]*reflect.StructField
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findAnnotatedFields(tt.args.t, tt.args.ft, tt.args.deep); !reflect.DeepEqual(got, tt.want) {
t.Errorf("findAnnotatedFields() = %v, want %v", got, tt.want)
}
})
}
}