func (t *ReturnTest) NoReturnValues() { sig := reflect.TypeOf(func() {}) var a oglemock.Action var err error var vals []interface{} // No values. a = oglemock.Return() err = a.SetSignature(sig) AssertEq(nil, err) vals = a.Invoke([]interface{}{}) ExpectThat(vals, ElementsAre()) // One value. a = oglemock.Return(17) err = a.SetSignature(sig) ExpectThat(err, Error(HasSubstr("given 1 val"))) ExpectThat(err, Error(HasSubstr("expected 0"))) // Two values. a = oglemock.Return(17, 19) err = a.SetSignature(sig) ExpectThat(err, Error(HasSubstr("given 2 vals"))) ExpectThat(err, Error(HasSubstr("expected 0"))) }
func (t *IntegrationTest) ExpectedCalls() { // Expectations t.controller.ExpectCall(t.reader, "Read", "", 112)(nil). WillOnce(oglemock.Return(17, nil)). WillOnce(oglemock.Return(19, nil)) t.controller.ExpectCall(t.reader, "Read", "", 112)(Not(Equals(nil))). WillOnce(oglemock.Return(23, errors.New("taco"))) // Calls var n int var err error n, err = t.reader.Read(nil) ExpectEq(17, n) ExpectEq(nil, err) n, err = t.reader.Read([]byte{}) ExpectEq(23, n) ExpectThat(err, Error(Equals("taco"))) n, err = t.reader.Read(nil) ExpectEq(19, n) ExpectEq(nil, err) // Errors AssertEq(0, len(t.reporter.errors), "%v", t.reporter.errors) AssertEq(0, len(t.reporter.fatalErrors), "%v", t.reporter.fatalErrors) }
func (t *ReturnTest) MultipleReturnValues() { sig := reflect.TypeOf(func() (int, string) { return 0, "" }) var a oglemock.Action var err error var vals []interface{} // No values. a = oglemock.Return() err = a.SetSignature(sig) ExpectThat(err, Error(HasSubstr("given 0 vals"))) ExpectThat(err, Error(HasSubstr("expected 2"))) // One value. a = oglemock.Return(17) err = a.SetSignature(sig) ExpectThat(err, Error(HasSubstr("given 1 val"))) ExpectThat(err, Error(HasSubstr("expected 2"))) // Two values. a = oglemock.Return(17, "taco") err = a.SetSignature(sig) AssertEq(nil, err) vals = a.Invoke([]interface{}{}) ExpectThat(vals, ElementsAre(IdenticalTo(int(17)), "taco")) }
func (t *IntegrationTest) WrongTypeForReturn() { t.controller.ExpectCall(t.reader, "Read", "foo.go", 112)(nil). WillOnce(oglemock.Return(0, errors.New(""))). WillOnce(oglemock.Return("taco", errors.New(""))) // Errors AssertEq(0, len(t.reporter.errors), "%v", t.reporter.errors) AssertEq(1, len(t.reporter.fatalErrors), "%v", t.reporter.fatalErrors) r := t.reporter.fatalErrors[0] ExpectEq("foo.go", r.fileName) ExpectEq(112, r.lineNumber) ExpectThat(r.err, Error(HasSubstr("Return"))) ExpectThat(r.err, Error(HasSubstr("arg 0"))) ExpectThat(r.err, Error(HasSubstr("int"))) ExpectThat(r.err, Error(HasSubstr("string"))) }
func (t *DoAllTest) SingleAction() { f := func(a int) string { return "" } a0 := oglemock.Return("taco") action := oglemock.DoAll(a0) AssertEq(nil, action.SetSignature(reflect.TypeOf(f))) rets := action.Invoke([]interface{}{17}) ExpectThat(rets, ElementsAre("taco")) }
func (t *DoAllTest) LastActionDoesntLikeSignature() { f := func(a int, b string) {} a0 := oglemock.Invoke(f) a1 := oglemock.Invoke(f) a2 := oglemock.Return(17) err := oglemock.DoAll(a0, a1, a2).SetSignature(reflect.TypeOf(f)) ExpectThat(err, Error(HasSubstr("Action 2"))) ExpectThat(err, Error(HasSubstr("1 vals; expected 0"))) }
func (t *DoAllTest) MultipleActions() { f := func(a int) string { return "" } var saved int a0 := oglemock.SaveArg(0, &saved) a1 := oglemock.Return("taco") action := oglemock.DoAll(a0, a1) AssertEq(nil, action.SetSignature(reflect.TypeOf(f))) rets := action.Invoke([]interface{}{17}) ExpectEq(17, saved) ExpectThat(rets, ElementsAre("taco")) }
func (t *ReturnTest) runTestCases(signature reflect.Type, cases []returnTestCase) { for i, c := range cases { a := oglemock.Return(c.suppliedVal) // SetSignature err := a.SetSignature(signature) if c.expectedSetSignatureErrorSubstring == "" { ExpectEq(nil, err, "Test case %d: %v", i, c) if err != nil { continue } } else { ExpectThat(err, Error(HasSubstr(c.expectedSetSignatureErrorSubstring)), "Test case %d: %v", i, c) continue } // Invoke res := a.Invoke([]interface{}{}) AssertThat(res, ElementsAre(Any())) ExpectThat(res[0], IdenticalTo(c.expectedVal), "Test case %d: %v", i, c) } }
func (t *ReturnTest) SetSignatureNotCalled() { a := oglemock.Return() f := func() { a.Invoke([]interface{}{}) } ExpectThat(f, Panics(MatchesRegexp("first call SetSignature"))) }