Beispiel #1
0
func (t *GenerateTest) runGoldenTest(
	caseName string,
	outputPkgPath string,
	nilPtrs ...interface{}) {
	// Make a slice of interface types to give to GenerateMockSource.
	interfaces := make([]reflect.Type, len(nilPtrs))
	for i, ptr := range nilPtrs {
		interfaces[i] = reflect.TypeOf(ptr).Elem()
	}

	// Create the mock source.
	buf := new(bytes.Buffer)
	err := generate.GenerateMockSource(buf, outputPkgPath, interfaces)
	AssertEq(nil, err, "Error from GenerateMockSource: %v", err)

	// Read the golden file.
	goldenPath := path.Join("testdata", "golden."+caseName+".go")
	goldenData := readFileOrDie(goldenPath)

	// Compare the two.
	identical := (buf.String() == string(goldenData))
	ExpectTrue(identical, "Output doesn't match for case '%s'.", caseName)

	// Write out a new golden file if requested.
	if !identical && *dumpNew {
		writeContentsToFileOrDie(buf.Bytes(), goldenPath)
	}
}
Beispiel #2
0
func (t *GenerateTest) EmptySetOfInterfaces() {
	err := generate.GenerateMockSource(
		new(bytes.Buffer),
		"foo",
		[]reflect.Type{})

	ExpectThat(err, Error(HasSubstr("interfaces")))
	ExpectThat(err, Error(HasSubstr("non-empty")))
}
Beispiel #3
0
func (t *GenerateTest) EmptyOutputPackagePath() {
	err := generate.GenerateMockSource(
		new(bytes.Buffer),
		"",
		[]reflect.Type{
			reflect.TypeOf((*io.Reader)(nil)).Elem(),
		})

	ExpectThat(err, Error(HasSubstr("Package path")))
	ExpectThat(err, Error(HasSubstr("non-empty")))
}
Beispiel #4
0
func (t *GenerateTest) NonInterfaceType() {
	err := generate.GenerateMockSource(
		new(bytes.Buffer),
		"foo",
		[]reflect.Type{
			reflect.TypeOf((*io.Reader)(nil)).Elem(),
			reflect.TypeOf(17),
			reflect.TypeOf((*io.Writer)(nil)).Elem(),
		})

	ExpectThat(err, Error(HasSubstr("Invalid type")))
}