Esempio n. 1
0
func getTestMethods(suitePointerType reflect.Type) []reflect.Method {
	var exportedMethods []reflect.Method
	for _, m := range srcutil.GetMethodsInSourceOrder(suitePointerType) {
		if isExported(m.Name) {
			exportedMethods = append(exportedMethods, m)
		}
	}

	return exportedMethods
}
Esempio n. 2
0
// RegisterTestSuite tells ogletest about a test suite containing tests that it
// should run. Any exported method on the type pointed to by the supplied
// prototype value will be treated as test methods, with the exception of the
// methods defined by the following interfaces, which when present are treated
// as described in the documentation for those interfaces:
//
//  *  SetUpTestSuiteInterface
//  *  SetUpInterface
//  *  TearDownInterface
//  *  TearDownTestSuiteInterface
//
// Each test method is invoked on a different receiver, which is initially a
// zero value of the test suite type.
//
// Example:
//
//     // Some value that is needed by the tests but is expensive to compute.
//     var someExpensiveThing uint
//
//     type FooTest struct {
//       // Path to a temporary file used by the tests. Each test gets a
//       // different temporary file.
//       tempFile string
//     }
//     func init() { ogletest.RegisterTestSuite(&FooTest{}) }
//
//     func (t *FooTest) SetUpTestSuite() {
//       someExpensiveThing = ComputeSomeExpensiveThing()
//     }
//
//     func (t *FooTest) SetUp(ti *ogletest.TestInfo) {
//       t.tempFile = CreateTempFile()
//     }
//
//     func (t *FooTest) TearDown() {
//       DeleteTempFile(t.tempFile)
//     }
//
//     func (t *FooTest) FrobinicatorIsSuccessfullyTweaked() {
//       res := DoSomethingWithExpensiveThing(someExpensiveThing, t.tempFile)
//       ExpectThat(res, Equals(true))
//     }
//
func RegisterTestSuite(p interface{}) {
	if p == nil {
		panic("RegisterTestSuite called with nil suite.")
	}

	val := reflect.ValueOf(p)
	typ := val.Type()
	var zeroInstance reflect.Value

	// We will transform to a TestSuite struct.
	suite := TestSuite{}
	suite.Name = typ.Elem().Name()

	zeroInstance = reflect.New(typ.Elem())
	if i, ok := zeroInstance.Interface().(SetUpTestSuiteInterface); ok {
		suite.SetUp = func() { i.SetUpTestSuite() }
	}

	zeroInstance = reflect.New(typ.Elem())
	if i, ok := zeroInstance.Interface().(TearDownTestSuiteInterface); ok {
		suite.TearDown = func() { i.TearDownTestSuite() }
	}

	// Transform a list of test methods for the suite, filtering them to just the
	// ones that we don't need to skip.
	for _, method := range filterMethods(suite.Name, srcutil.GetMethodsInSourceOrder(typ)) {
		var tf TestFunction
		tf.Name = method.Name

		// Create an instance to be operated on by all of the TestFunction's
		// internal functions.
		instance := reflect.New(typ.Elem())

		// Bind the functions to the instance.
		if i, ok := instance.Interface().(SetUpInterface); ok {
			tf.SetUp = func(ti *TestInfo) { i.SetUp(ti) }
		}

		methodCopy := method
		tf.Run = func() { runTestMethod(instance, methodCopy) }

		if i, ok := instance.Interface().(TearDownInterface); ok {
			tf.TearDown = func() { i.TearDown() }
		}

		// Save the TestFunction.
		suite.TestFunctions = append(suite.TestFunctions, tf)
	}

	// Register the suite.
	Register(suite)
}