func (t *GenerateTest) runGoldenTest( caseName 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, "some_pkg", interfaces) AssertEq(nil, err, "Error from GenerateMockSource: %v", err) // Read the golden file. goldenPath := path.Join("test_cases", "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) } }
func (t *GenerateTest) EmptySetOfInterfaces() { err := generate.GenerateMockSource( new(bytes.Buffer), "foo", []reflect.Type{}) ExpectThat(err, Error(HasSubstr("interfaces"))) ExpectThat(err, Error(HasSubstr("non-empty"))) }
func (t *GenerateTest) EmptyPackageName() { err := generate.GenerateMockSource( new(bytes.Buffer), "", []reflect.Type{ reflect.TypeOf((*io.Reader)(nil)).Elem(), }) ExpectThat(err, Error(HasSubstr("Package name"))) ExpectThat(err, Error(HasSubstr("non-empty"))) }
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"))) }