Esempio n. 1
0
func Test_Injector_Invoke(t *testing.T) {
	Convey("Invokes function", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		dep := "some dependency"
		injector.Map(dep)
		dep2 := "another dep"
		injector.MapTo(dep2, (*SpecialString)(nil))
		dep3 := make(chan *SpecialString)
		dep4 := make(chan *SpecialString)
		typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem())
		typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem())
		injector.Set(typRecv, reflect.ValueOf(dep3))
		injector.Set(typSend, reflect.ValueOf(dep4))

		_, err := injector.Invoke(func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString) {
			So(d1, ShouldEqual, dep)
			So(d2, ShouldEqual, dep2)
			So(reflect.TypeOf(d3).Elem(), ShouldEqual, reflect.TypeOf(dep3).Elem())
			So(reflect.TypeOf(d4).Elem(), ShouldEqual, reflect.TypeOf(dep4).Elem())
			So(reflect.TypeOf(d3).ChanDir(), ShouldEqual, reflect.RecvDir)
			So(reflect.TypeOf(d4).ChanDir(), ShouldEqual, reflect.SendDir)
		})
		So(err, ShouldBeNil)

		_, err = injector.Invoke(myFastInvoker(func(string) {}))
		So(err, ShouldBeNil)
	})

	Convey("Invokes function with return value", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		dep := "some dependency"
		injector.Map(dep)
		dep2 := "another dep"
		injector.MapTo(dep2, (*SpecialString)(nil))

		result, err := injector.Invoke(func(d1 string, d2 SpecialString) string {
			So(d1, ShouldEqual, dep)
			So(d2, ShouldEqual, dep2)
			return "Hello world"
		})

		So(result[0].String(), ShouldEqual, "Hello world")
		So(err, ShouldBeNil)
	})
}
Esempio n. 2
0
func Test_Injector_SetParent(t *testing.T) {
	Convey("Set parent of injector", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		injector.MapTo("another dep", (*SpecialString)(nil))

		injector2 := inject.New()
		So(injector, ShouldNotBeNil)

		injector2.SetParent(injector)

		So(injector2.GetVal(inject.InterfaceOf((*SpecialString)(nil))).IsValid(), ShouldBeTrue)
	})
}
Esempio n. 3
0
func benchmarkInjectorInvoke(b *testing.B, isFast, isSimple bool) {
	b.StopTimer()

	injector := inject.New()
	dep := "some dependency"
	injector.Map(dep)
	dep2 := "another dep"
	injector.MapTo(dep2, (*SpecialString)(nil))

	var f1, f2 interface{}
	if isSimple { //func()
		f1 = f1SimpleInjectorInvoke
		f2 = f2SimpleInjectorInvokeHandler(f2SimpleInjectorInvoke)
	} else { //func(p1, p2) ret
		f1 = f1InjectorInvoke
		f2 = f2InjectorInvokeHandler(f2InjectorInvoke)
	}
	injector.Invoke(f1)
	injector.Invoke(f2)

	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if isFast {
			injector.Invoke(f2)
		} else {
			injector.Invoke(f1)
		}
	}
}
Esempio n. 4
0
func Test_Injector_Implementors(t *testing.T) {
	Convey("Check implementors", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		g := &Greeter{"Jeremy"}
		injector.Map(g)

		So(injector.GetVal(inject.InterfaceOf((*fmt.Stringer)(nil))).IsValid(), ShouldBeTrue)
	})
}
Esempio n. 5
0
func Test_Injector_GetVal(t *testing.T) {
	Convey("Map and get type", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		injector.Map("some dependency")

		So(injector.GetVal(reflect.TypeOf("string")).IsValid(), ShouldBeTrue)
		So(injector.GetVal(reflect.TypeOf(11)).IsValid(), ShouldBeFalse)
	})
}
Esempio n. 6
0
func Test_Injector_Apply(t *testing.T) {
	Convey("Apply a type", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		injector.Map("a dep").MapTo("another dep", (*SpecialString)(nil))

		s := TestStruct{}
		So(injector.Apply(&s), ShouldBeNil)

		So(s.Dep1, ShouldEqual, "a dep")
		So(s.Dep2, ShouldEqual, "another dep")
	})
}
Esempio n. 7
0
// NewWithLogger creates a bare bones Macaron instance.
// Use this method if you want to have full control over the middleware that is used.
// You can specify logger output writer with this function.
func NewWithLogger(out io.Writer) *Macaron {
	m := &Macaron{
		Injector: inject.New(),
		action:   func() {},
		Router:   NewRouter(),
		logger:   log.New(out, "[Macaron] ", 0),
	}
	m.Router.m = m
	m.Map(m.logger)
	m.Map(defaultReturnHandler())
	m.NotFound(http.NotFound)
	m.InternalServerError(func(rw http.ResponseWriter, err error) {
		http.Error(rw, err.Error(), 500)
	})
	return m
}
Esempio n. 8
0
func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context {
	c := &Context{
		Injector: inject.New(),
		handlers: m.handlers,
		action:   m.action,
		index:    0,
		Router:   m.Router,
		Req:      Request{req},
		Resp:     NewResponseWriter(rw),
		Data:     make(map[string]interface{}),
	}
	c.SetParent(m)
	c.Map(c)
	c.MapTo(c.Resp, (*http.ResponseWriter)(nil))
	c.Map(req)
	return c
}
Esempio n. 9
0
func Test_Injector_Set(t *testing.T) {
	Convey("Set and get type", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		typ := reflect.TypeOf("string")
		typSend := reflect.ChanOf(reflect.SendDir, typ)
		typRecv := reflect.ChanOf(reflect.RecvDir, typ)

		// instantiating unidirectional channels is not possible using reflect
		// http://golang.org/src/pkg/reflect/value.go?s=60463:60504#L2064
		chanRecv := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0)
		chanSend := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0)

		injector.Set(typSend, chanSend)
		injector.Set(typRecv, chanRecv)

		So(injector.GetVal(typSend).IsValid(), ShouldBeTrue)
		So(injector.GetVal(typRecv).IsValid(), ShouldBeTrue)
		So(injector.GetVal(chanSend.Type()).IsValid(), ShouldBeFalse)
	})
}