| 1 | package gostub |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestStubTime(t *testing.T) { |
| 11 | var timeNow = time.Now |
| 12 | |
| 13 | var fakeTime = time.Date(2015, 7, 1, 0, 0, 0, 0, time.UTC) |
| 14 | StubFunc(&timeNow, fakeTime) |
| 15 | expectVal(t, fakeTime, timeNow()) |
| 16 | } |
| 17 | |
| 18 | func TestReturnErr(t *testing.T) { |
| 19 | var osRemove = os.Remove |
| 20 | |
| 21 | StubFunc(&osRemove, nil) |
| 22 | expectVal(t, nil, osRemove("test")) |
| 23 | |
| 24 | e := errors.New("err") |
| 25 | StubFunc(&osRemove, e) |
| 26 | expectVal(t, e, osRemove("test")) |
| 27 | } |
| 28 | |
| 29 | func TestStubHostname(t *testing.T) { |
| 30 | var osHostname = os.Hostname |
| 31 | |
| 32 | StubFunc(&osHostname, "fakehost", nil) |
| 33 | hostname, err := osHostname() |
| 34 | expectVal(t, "fakehost", hostname) |
| 35 | expectVal(t, nil, err) |
| 36 | |
| 37 | var errNoHost = errors.New("no hostname") |
| 38 | StubFunc(&osHostname, "", errNoHost) |
| 39 | hostname, err = osHostname() |
| 40 | expectVal(t, "", hostname) |
| 41 | expectVal(t, errNoHost, err) |
| 42 | } |
| 43 | |
| 44 | func TestStubReturnFunc(t *testing.T) { |
| 45 | var retFunc = func() func() error { |
| 46 | return func() error { |
| 47 | return errors.New("err") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | var errInception = errors.New("in limbo") |
| 52 | StubFunc(&retFunc, func() error { |
| 53 | return errInception |
| 54 | }) |
| 55 | expectVal(t, errInception, retFunc()()) |
| 56 | } |
| 57 | |
| 58 | func TestStubFuncFail(t *testing.T) { |
| 59 | var osHostname = os.Hostname |
| 60 | var s string |
| 61 | |
| 62 | tests := []struct { |
| 63 | desc string |
| 64 | toStub interface{} |
| 65 | stubVals []interface{} |
| 66 | wantErr string |
| 67 | }{ |
| 68 | { |
| 69 | desc: "toStub is not a function", |
| 70 | toStub: &s, |
| 71 | stubVals: []interface{}{"fakehost", nil}, |
| 72 | wantErr: "to stub must be a pointer to a function", |
| 73 | }, |
| 74 | { |
| 75 | desc: "toStub is not a pointer", |
| 76 | toStub: osHostname, |
| 77 | stubVals: []interface{}{"fakehost", nil}, |
| 78 | wantErr: "to stub must be a pointer to a function", |
| 79 | }, |
| 80 | { |
| 81 | desc: "wrong number of stubVals", |
| 82 | toStub: &osHostname, |
| 83 | stubVals: []interface{}{"fakehost"}, |
| 84 | wantErr: "func type has 2 return values, but only 1 stub values provided", |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | for _, tt := range tests { |
| 89 | func() { |
| 90 | defer expectPanic(t, tt.desc, tt.wantErr) |
| 91 | StubFunc(tt.toStub, tt.stubVals...) |
| 92 | }() |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestMultipleStubFuncs(t *testing.T) { |
| 97 | var f1 = func() int { |
| 98 | return 100 |
| 99 | } |
| 100 | var f2 = func() int { |
| 101 | return 200 |
| 102 | } |
| 103 | var f3 = func() int { |
| 104 | return 300 |
| 105 | } |
| 106 | |
| 107 | stubs := StubFunc(&f1, 1).StubFunc(&f2, 2) |
| 108 | expectVal(t, f1(), 1) |
| 109 | expectVal(t, f2(), 2) |
| 110 | |
| 111 | stubs.StubFunc(&f3, 3) |
| 112 | expectVal(t, f3(), 3) |
| 113 | |
| 114 | stubs.Reset() |
| 115 | expectVal(t, f1(), 100) |
| 116 | expectVal(t, f2(), 200) |
| 117 | expectVal(t, f3(), 300) |
| 118 | } |