// Validate implements the Constraint interface by checking if the given container does not contain invalid types.
func (c *NoInvalidTypesConstraint) Validate(container *goldi.Container) (err error) {
	for typeID, typeFactory := range container.TypeRegistry {
		if goldi.IsValid(typeFactory) == false {
			return fmt.Errorf("type %q is invalid: %s", typeID, typeFactory.(error))
		}
	}

	return nil
}
Exemple #2
0
// ExampleNewFuncType_ prevents godoc from printing the whole content of this file as example
func ExampleNewFuncType_() {}

var _ = Describe("funcType", func() {
	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewFuncType(SomeFunctionForFuncTypeTest)

		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("goldi.NewFuncType()", func() {
		Context("with invalid argument", func() {
			It("should return an invalid type if the argument is no function", func() {
				Expect(goldi.IsValid(goldi.NewFuncType(42))).To(BeFalse())
			})
		})

		Context("with argument beeing a function", func() {
			It("should create the type", func() {
				typeDef := goldi.NewFuncType(SomeFunctionForFuncTypeTest)
				Expect(typeDef).NotTo(BeNil())
			})
		})
	})

	Describe("Arguments()", func() {
		It("should return an empty list", func() {
			typeDef := goldi.NewFuncType(SomeFunctionForFuncTypeTest)
			Expect(typeDef.Arguments()).NotTo(BeNil())
// ExampleNewStructType_ prevents godoc from printing the whole content of this file as example
func ExampleNewStructType_() {}

var _ = Describe("structType", func() {
	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewStructType(MockType{})
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("goldi.NewStructType()", func() {
		Context("with invalid arguments", func() {
			It("should return an invalid type if the generator is no struct or pointer to a struct", func() {
				Expect(goldi.IsValid(goldi.NewStructType(42))).To(BeFalse())
			})

			It("should return an invalid type if the generator is a pointer to something other than a struct", func() {
				something := "Hello Pointer World!"
				Expect(goldi.IsValid(goldi.NewStructType(&something))).To(BeFalse())
			})
		})

		Context("with first argument beeing a struct", func() {
			It("should create the type", func() {
				typeDef := goldi.NewStructType(MockType{})
				Expect(typeDef).NotTo(BeNil())
			})
		})
	BeforeEach(func() {
		embeddedType = goldi.NewStructType(Foo{})
	})

	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewConfiguredType(embeddedType, "configurator_type", "Configure")
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("NewConfiguredType()", func() {
		Context("with invalid argument", func() {
			It("should return an invalid type if the embedded type is nil", func() {
				typeDef := goldi.NewConfiguredType(nil, "configurator_type", "Configure")
				Expect(goldi.IsValid(typeDef)).To(BeFalse())
			})

			It("should return an invalid type if either the configurator ID or method is empty", func() {
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "", ""))).To(BeFalse())
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "configurator_type", ""))).To(BeFalse())
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "", "configure"))).To(BeFalse())
			})

			It("should return an invalid type if the configurator method is not exported", func() {
				Expect(goldi.IsValid(goldi.NewConfiguredType(embeddedType, "configurator_type", "configure"))).To(BeFalse())
			})
		})

		Context("with valid arguments", func() {
			It("should create the type", func() {
Exemple #5
0
// ExampleNewType_ prevents godoc from printing the whole content of this file as example
func ExampleNewType_() {}

var _ = Describe("type", func() {
	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewType(NewFoo)
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("goldi.NewType()", func() {
		Context("with invalid factory function", func() {
			It("should return an invalid type if the generator is no function", func() {
				Expect(goldi.IsValid(goldi.NewType(42))).To(BeFalse())
			})

			It("should return an invalid type if the generator has no output parameters", func() {
				Expect(goldi.IsValid(goldi.NewType(func() {}))).To(BeFalse())
			})

			It("should return an invalid type if the generator has more than one output parameter", func() {
				Expect(goldi.IsValid(goldi.NewType(func() (*MockType, *MockType) { return nil, nil }))).To(BeFalse())
			})

			It("should return an invalid type if the return parameter is no pointer", func() {
				Expect(goldi.IsValid(goldi.NewType(func() MockType { return MockType{} }))).To(BeFalse())
			})

			It("should not return an invalid type if the return parameter is an interface", func() {
// ExampleNewFuncReferenceType_ prevents godoc from printing the whole content of this file as example
func ExampleNewFuncReferenceType_() {}

var _ = Describe("funcReferenceType", func() {
	It("should implement the TypeFactory interface", func() {
		var factory goldi.TypeFactory
		factory = goldi.NewFuncReferenceType("my_controller", "FancyAction")
		// if this compiles the test passes (next expectation only to make compiler happy)
		Expect(factory).NotTo(BeNil())
	})

	Describe("NewFuncReferenceType()", func() {
		It("should return an invalid type if the method name is not exported", func() {
			t := goldi.NewFuncReferenceType("foo", "doStuff")
			Expect(goldi.IsValid(t)).To(BeFalse())
			Expect(t).To(MatchError(`can not use unexported method "doStuff" as second argument to NewFuncReferenceType`))
		})
	})

	Describe("Arguments()", func() {
		It("should return the referenced service ID", func() {
			typeDef := goldi.NewFuncReferenceType("my_controller", "FancyAction")
			Expect(typeDef.Arguments()).To(Equal([]interface{}{"@my_controller"}))
		})
	})

	Describe("Generate()", func() {
		var (
			container *goldi.Container
			resolver  *goldi.ParameterResolver