...

Source file src/github.com/codegangsta/inject/inject_test.go

Documentation: github.com/codegangsta/inject

     1  package inject_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/codegangsta/inject"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  type SpecialString interface {
    11  }
    12  
    13  type TestStruct struct {
    14  	Dep1 string        `inject:"t" json:"-"`
    15  	Dep2 SpecialString `inject`
    16  	Dep3 string
    17  }
    18  
    19  type Greeter struct {
    20  	Name string
    21  }
    22  
    23  func (g *Greeter) String() string {
    24  	return "Hello, My name is" + g.Name
    25  }
    26  
    27  /* Test Helpers */
    28  func expect(t *testing.T, a interface{}, b interface{}) {
    29  	if a != b {
    30  		t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
    31  	}
    32  }
    33  
    34  func refute(t *testing.T, a interface{}, b interface{}) {
    35  	if a == b {
    36  		t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
    37  	}
    38  }
    39  
    40  func Test_InjectorInvoke(t *testing.T) {
    41  	injector := inject.New()
    42  	expect(t, injector == nil, false)
    43  
    44  	dep := "some dependency"
    45  	injector.Map(dep)
    46  	dep2 := "another dep"
    47  	injector.MapTo(dep2, (*SpecialString)(nil))
    48  	dep3 := make(chan *SpecialString)
    49  	dep4 := make(chan *SpecialString)
    50  	typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem())
    51  	typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem())
    52  	injector.Set(typRecv, reflect.ValueOf(dep3))
    53  	injector.Set(typSend, reflect.ValueOf(dep4))
    54  
    55  	_, err := injector.Invoke(func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString) {
    56  		expect(t, d1, dep)
    57  		expect(t, d2, dep2)
    58  		expect(t, reflect.TypeOf(d3).Elem(), reflect.TypeOf(dep3).Elem())
    59  		expect(t, reflect.TypeOf(d4).Elem(), reflect.TypeOf(dep4).Elem())
    60  		expect(t, reflect.TypeOf(d3).ChanDir(), reflect.RecvDir)
    61  		expect(t, reflect.TypeOf(d4).ChanDir(), reflect.SendDir)
    62  	})
    63  
    64  	expect(t, err, nil)
    65  }
    66  
    67  func Test_InjectorInvokeReturnValues(t *testing.T) {
    68  	injector := inject.New()
    69  	expect(t, injector == nil, false)
    70  
    71  	dep := "some dependency"
    72  	injector.Map(dep)
    73  	dep2 := "another dep"
    74  	injector.MapTo(dep2, (*SpecialString)(nil))
    75  
    76  	result, err := injector.Invoke(func(d1 string, d2 SpecialString) string {
    77  		expect(t, d1, dep)
    78  		expect(t, d2, dep2)
    79  		return "Hello world"
    80  	})
    81  
    82  	expect(t, result[0].String(), "Hello world")
    83  	expect(t, err, nil)
    84  }
    85  
    86  func Test_InjectorApply(t *testing.T) {
    87  	injector := inject.New()
    88  
    89  	injector.Map("a dep").MapTo("another dep", (*SpecialString)(nil))
    90  
    91  	s := TestStruct{}
    92  	err := injector.Apply(&s)
    93  	expect(t, err, nil)
    94  
    95  	expect(t, s.Dep1, "a dep")
    96  	expect(t, s.Dep2, "another dep")
    97  	expect(t, s.Dep3, "")
    98  }
    99  
   100  func Test_InterfaceOf(t *testing.T) {
   101  	iType := inject.InterfaceOf((*SpecialString)(nil))
   102  	expect(t, iType.Kind(), reflect.Interface)
   103  
   104  	iType = inject.InterfaceOf((**SpecialString)(nil))
   105  	expect(t, iType.Kind(), reflect.Interface)
   106  
   107  	// Expecting nil
   108  	defer func() {
   109  		rec := recover()
   110  		refute(t, rec, nil)
   111  	}()
   112  	iType = inject.InterfaceOf((*testing.T)(nil))
   113  }
   114  
   115  func Test_InjectorSet(t *testing.T) {
   116  	injector := inject.New()
   117  	typ := reflect.TypeOf("string")
   118  	typSend := reflect.ChanOf(reflect.SendDir, typ)
   119  	typRecv := reflect.ChanOf(reflect.RecvDir, typ)
   120  
   121  	// instantiating unidirectional channels is not possible using reflect
   122  	// http://golang.org/src/pkg/reflect/value.go?s=60463:60504#L2064
   123  	chanRecv := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0)
   124  	chanSend := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0)
   125  
   126  	injector.Set(typSend, chanSend)
   127  	injector.Set(typRecv, chanRecv)
   128  
   129  	expect(t, injector.Get(typSend).IsValid(), true)
   130  	expect(t, injector.Get(typRecv).IsValid(), true)
   131  	expect(t, injector.Get(chanSend.Type()).IsValid(), false)
   132  }
   133  
   134  func Test_InjectorGet(t *testing.T) {
   135  	injector := inject.New()
   136  
   137  	injector.Map("some dependency")
   138  
   139  	expect(t, injector.Get(reflect.TypeOf("string")).IsValid(), true)
   140  	expect(t, injector.Get(reflect.TypeOf(11)).IsValid(), false)
   141  }
   142  
   143  func Test_InjectorSetParent(t *testing.T) {
   144  	injector := inject.New()
   145  	injector.MapTo("another dep", (*SpecialString)(nil))
   146  
   147  	injector2 := inject.New()
   148  	injector2.SetParent(injector)
   149  
   150  	expect(t, injector2.Get(inject.InterfaceOf((*SpecialString)(nil))).IsValid(), true)
   151  }
   152  
   153  func TestInjectImplementors(t *testing.T) {
   154  	injector := inject.New()
   155  	g := &Greeter{"Jeremy"}
   156  	injector.Map(g)
   157  
   158  	expect(t, injector.Get(inject.InterfaceOf((*fmt.Stringer)(nil))).IsValid(), true)
   159  }
   160  

View as plain text