| 1 |
// Copyright 2014 Roger Peppe. |
| 2 |
// See LICENCE file for details. |
| 3 |
|
| 4 |
package errgo_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"io/ioutil" |
| 9 |
"runtime" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
qt "github.com/frankban/quicktest" |
| 14 |
|
| 15 |
"gopkg.in/errgo.v1" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
_ errgo.Wrapper = (*errgo.Err)(nil) |
| 20 |
_ errgo.Locationer = (*errgo.Err)(nil) |
| 21 |
_ errgo.Causer = (*errgo.Err)(nil) |
| 22 |
) |
| 23 |
|
| 24 |
func TestNew(t *testing.T) { |
| 25 |
c := qt.New(t) |
| 26 |
err := errgo.New("foo") //err TestNew |
| 27 |
checkErr(c, err, nil, "foo", "[{$TestNew$: foo}]", err) |
| 28 |
} |
| 29 |
|
| 30 |
func TestNewf(t *testing.T) { |
| 31 |
c := qt.New(t) |
| 32 |
err := errgo.Newf("foo %d", 5) //err TestNewf |
| 33 |
checkErr(c, err, nil, "foo 5", "[{$TestNewf$: foo 5}]", err) |
| 34 |
} |
| 35 |
|
| 36 |
var someErr = errgo.New("some error") //err varSomeErr |
| 37 |
|
| 38 |
func annotate1() error { |
| 39 |
err := errgo.Notef(someErr, "annotate1") //err annotate1 |
| 40 |
return err |
| 41 |
} |
| 42 |
|
| 43 |
func annotate2() error { |
| 44 |
err := annotate1() |
| 45 |
err = errgo.Notef(err, "annotate2") //err annotate2 |
| 46 |
return err |
| 47 |
} |
| 48 |
|
| 49 |
func TestNoteUsage(t *testing.T) { |
| 50 |
c := qt.New(t) |
| 51 |
err0 := annotate2() |
| 52 |
err, ok := err0.(errgo.Wrapper) |
| 53 |
c.Assert(ok, qt.Equals, true) |
| 54 |
underlying := err.Underlying() |
| 55 |
checkErr( |
| 56 |
c, err0, underlying, |
| 57 |
"annotate2: annotate1: some error", |
| 58 |
"[{$annotate2$: annotate2} {$annotate1$: annotate1} {$varSomeErr$: some error}]", |
| 59 |
err0) |
| 60 |
} |
| 61 |
|
| 62 |
func TestMask(t *testing.T) { |
| 63 |
c := qt.New(t) |
| 64 |
err0 := errgo.WithCausef(nil, someErr, "foo") //err TestMask#0 |
| 65 |
err := errgo.Mask(err0) //err TestMask#1 |
| 66 |
checkErr(c, err, err0, "foo", "[{$TestMask#1$: } {$TestMask#0$: foo}]", err) |
| 67 |
|
| 68 |
err = errgo.Mask(nil) |
| 69 |
c.Assert(err, qt.IsNil) |
| 70 |
} |
| 71 |
|
| 72 |
func TestNotef(t *testing.T) { |
| 73 |
c := qt.New(t) |
| 74 |
err0 := errgo.WithCausef(nil, someErr, "foo") //err TestNotef#0 |
| 75 |
err := errgo.Notef(err0, "bar") //err TestNotef#1 |
| 76 |
checkErr(c, err, err0, "bar: foo", "[{$TestNotef#1$: bar} {$TestNotef#0$: foo}]", err) |
| 77 |
|
| 78 |
err = errgo.Notef(nil, "bar") //err TestNotef#2 |
| 79 |
checkErr(c, err, nil, "bar", "[{$TestNotef#2$: bar}]", err) |
| 80 |
} |
| 81 |
|
| 82 |
func TestNoteMask(t *testing.T) { |
| 83 |
c := qt.New(t) |
| 84 |
err0 := errgo.WithCausef(nil, someErr, "foo") //err TestNoteMask#0 |
| 85 |
err := errgo.NoteMask(err0, "bar") //err TestNoteMask#1 |
| 86 |
checkErr(c, err, err0, "bar: foo", "[{$TestNoteMask#1$: bar} {$TestNoteMask#0$: foo}]", err) |
| 87 |
|
| 88 |
err = errgo.NoteMask(err0, "bar", errgo.Is(someErr)) //err TestNoteMask#2 |
| 89 |
checkErr(c, err, err0, "bar: foo", "[{$TestNoteMask#2$: bar} {$TestNoteMask#0$: foo}]", someErr) |
| 90 |
|
| 91 |
err = errgo.NoteMask(err0, "") //err TestNoteMask#3 |
| 92 |
checkErr(c, err, err0, "foo", "[{$TestNoteMask#3$: } {$TestNoteMask#0$: foo}]", err) |
| 93 |
} |
| 94 |
|
| 95 |
func TestMaskFunc(t *testing.T) { |
| 96 |
c := qt.New(t) |
| 97 |
err0 := errgo.New("zero") |
| 98 |
err1 := errgo.New("one") |
| 99 |
|
| 100 |
allowVals := func(vals ...error) (r []func(error) bool) { |
| 101 |
for _, val := range vals { |
| 102 |
r = append(r, errgo.Is(val)) |
| 103 |
} |
| 104 |
return |
| 105 |
} |
| 106 |
tests := []struct { |
| 107 |
err error |
| 108 |
allow0 []func(error) bool |
| 109 |
allow1 []func(error) bool |
| 110 |
cause error |
| 111 |
}{{ |
| 112 |
err: err0, |
| 113 |
allow0: allowVals(err0), |
| 114 |
cause: err0, |
| 115 |
}, { |
| 116 |
err: err1, |
| 117 |
allow0: allowVals(err0), |
| 118 |
cause: nil, |
| 119 |
}, { |
| 120 |
err: err0, |
| 121 |
allow1: allowVals(err0), |
| 122 |
cause: err0, |
| 123 |
}, { |
| 124 |
err: err0, |
| 125 |
allow0: allowVals(err1), |
| 126 |
allow1: allowVals(err0), |
| 127 |
cause: err0, |
| 128 |
}, { |
| 129 |
err: err0, |
| 130 |
allow0: allowVals(err0, err1), |
| 131 |
cause: err0, |
| 132 |
}, { |
| 133 |
err: err1, |
| 134 |
allow0: allowVals(err0, err1), |
| 135 |
cause: err1, |
| 136 |
}, { |
| 137 |
err: err0, |
| 138 |
allow1: allowVals(err0, err1), |
| 139 |
cause: err0, |
| 140 |
}, { |
| 141 |
err: err1, |
| 142 |
allow1: allowVals(err0, err1), |
| 143 |
cause: err1, |
| 144 |
}} |
| 145 |
for i, test := range tests { |
| 146 |
c.Logf("test %d", i) |
| 147 |
wrap := errgo.MaskFunc(test.allow0...) |
| 148 |
err := wrap(test.err, test.allow1...) |
| 149 |
cause := errgo.Cause(err) |
| 150 |
wantCause := test.cause |
| 151 |
if wantCause == nil { |
| 152 |
wantCause = err |
| 153 |
} |
| 154 |
c.Check(cause, qt.Equals, wantCause) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
type embed struct { |
| 159 |
*errgo.Err |
| 160 |
} |
| 161 |
|
| 162 |
func TestCause(t *testing.T) { |
| 163 |
c := qt.New(t) |
| 164 |
c.Assert(errgo.Cause(someErr), qt.Equals, someErr) |
| 165 |
|
| 166 |
causeErr := errgo.New("cause error") |
| 167 |
underlyingErr := errgo.New("underlying error") //err TestCause#1 |
| 168 |
err := errgo.WithCausef(underlyingErr, causeErr, "foo %d", 99) //err TestCause#2 |
| 169 |
c.Assert(errgo.Cause(err), qt.Equals, causeErr) |
| 170 |
|
| 171 |
checkErr(c, err, underlyingErr, "foo 99: underlying error", "[{$TestCause#2$: foo 99} {$TestCause#1$: underlying error}]", causeErr) |
| 172 |
|
| 173 |
err = &embed{err.(*errgo.Err)} |
| 174 |
c.Assert(errgo.Cause(err), qt.Equals, causeErr) |
| 175 |
} |
| 176 |
|
| 177 |
func TestWithCausefNoMessage(t *testing.T) { |
| 178 |
c := qt.New(t) |
| 179 |
cause := errgo.New("cause") |
| 180 |
err := errgo.WithCausef(nil, cause, "") |
| 181 |
c.Assert(err, qt.ErrorMatches, "cause") |
| 182 |
c.Assert(errgo.Cause(err), qt.Equals, cause) |
| 183 |
} |
| 184 |
|
| 185 |
func TestWithCausefWithUnderlyingButNoMessage(t *testing.T) { |
| 186 |
c := qt.New(t) |
| 187 |
err := errgo.New("something") |
| 188 |
cause := errgo.New("cause") |
| 189 |
err = errgo.WithCausef(err, cause, "") |
| 190 |
c.Assert(err, qt.ErrorMatches, "something") |
| 191 |
c.Assert(errgo.Cause(err), qt.Equals, cause) |
| 192 |
} |
| 193 |
|
| 194 |
func TestDetails(t *testing.T) { |
| 195 |
c := qt.New(t) |
| 196 |
c.Assert(errgo.Details(nil), qt.Equals, "[]") |
| 197 |
|
| 198 |
otherErr := fmt.Errorf("other") |
| 199 |
checkErr(c, otherErr, nil, "other", "[{other}]", otherErr) |
| 200 |
|
| 201 |
err0 := &embed{errgo.New("foo").(*errgo.Err)} //err TestStack#0 |
| 202 |
checkErr(c, err0, nil, "foo", "[{$TestStack#0$: foo}]", err0) |
| 203 |
|
| 204 |
err1 := &embed{errgo.Notef(err0, "bar").(*errgo.Err)} //err TestStack#1 |
| 205 |
checkErr(c, err1, err0, "bar: foo", "[{$TestStack#1$: bar} {$TestStack#0$: foo}]", err1) |
| 206 |
|
| 207 |
err2 := errgo.Mask(err1) //err TestStack#2 |
| 208 |
checkErr(c, err2, err1, "bar: foo", "[{$TestStack#2$: } {$TestStack#1$: bar} {$TestStack#0$: foo}]", err2) |
| 209 |
} |
| 210 |
|
| 211 |
func TestMatch(t *testing.T) { |
| 212 |
c := qt.New(t) |
| 213 |
type errTest func(error) bool |
| 214 |
allow := func(ss ...string) []func(error) bool { |
| 215 |
fns := make([]func(error) bool, len(ss)) |
| 216 |
for i, s := range ss { |
| 217 |
s := s |
| 218 |
fns[i] = func(err error) bool { |
| 219 |
return err != nil && err.Error() == s |
| 220 |
} |
| 221 |
} |
| 222 |
return fns |
| 223 |
} |
| 224 |
tests := []struct { |
| 225 |
err error |
| 226 |
fns []func(error) bool |
| 227 |
ok bool |
| 228 |
}{{ |
| 229 |
err: errgo.New("foo"), |
| 230 |
fns: allow("foo"), |
| 231 |
ok: true, |
| 232 |
}, { |
| 233 |
err: errgo.New("foo"), |
| 234 |
fns: allow("bar"), |
| 235 |
ok: false, |
| 236 |
}, { |
| 237 |
err: errgo.New("foo"), |
| 238 |
fns: allow("bar", "foo"), |
| 239 |
ok: true, |
| 240 |
}, { |
| 241 |
err: errgo.New("foo"), |
| 242 |
fns: nil, |
| 243 |
ok: false, |
| 244 |
}, { |
| 245 |
err: nil, |
| 246 |
fns: nil, |
| 247 |
ok: false, |
| 248 |
}} |
| 249 |
|
| 250 |
for i, test := range tests { |
| 251 |
c.Logf("test %d", i) |
| 252 |
c.Assert(errgo.Match(test.err, test.fns...), qt.Equals, test.ok) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func checkErr(c *qt.C, err, underlying error, msg string, details string, cause error) { |
| 257 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 258 |
c.Assert(err.Error(), qt.Equals, msg) |
| 259 |
if err, ok := err.(errgo.Wrapper); ok { |
| 260 |
c.Assert(err.Underlying(), qt.Equals, underlying) |
| 261 |
} else { |
| 262 |
c.Assert(underlying, qt.IsNil) |
| 263 |
} |
| 264 |
c.Assert(errgo.Cause(err), qt.Equals, cause) |
| 265 |
wantDetails := replaceLocations(details) |
| 266 |
c.Assert(errgo.Details(err), qt.Equals, wantDetails) |
| 267 |
} |
| 268 |
|
| 269 |
func replaceLocations(s string) string { |
| 270 |
t := "" |
| 271 |
for { |
| 272 |
i := strings.Index(s, "$") |
| 273 |
if i == -1 { |
| 274 |
break |
| 275 |
} |
| 276 |
t += s[0:i] |
| 277 |
s = s[i+1:] |
| 278 |
i = strings.Index(s, "$") |
| 279 |
if i == -1 { |
| 280 |
panic("no second $") |
| 281 |
} |
| 282 |
file, line := location(s[0:i]) |
| 283 |
t += fmt.Sprintf("%s:%d", file, line) |
| 284 |
s = s[i+1:] |
| 285 |
} |
| 286 |
t += s |
| 287 |
return t |
| 288 |
} |
| 289 |
|
| 290 |
func location(tag string) (string, int) { |
| 291 |
line, ok := tagToLine[tag] |
| 292 |
if !ok { |
| 293 |
panic(fmt.Errorf("tag %q not found", tag)) |
| 294 |
} |
| 295 |
return filename, line |
| 296 |
} |
| 297 |
|
| 298 |
var tagToLine = make(map[string]int) |
| 299 |
var filename string |
| 300 |
|
| 301 |
func init() { |
| 302 |
data, err := ioutil.ReadFile("errors_test.go") |
| 303 |
if err != nil { |
| 304 |
panic(err) |
| 305 |
} |
| 306 |
lines := strings.Split(string(data), "\n") |
| 307 |
for i, line := range lines { |
| 308 |
if j := strings.Index(line, "//err "); j >= 0 { |
| 309 |
tagToLine[line[j+len("//err "):]] = i + 1 |
| 310 |
} |
| 311 |
} |
| 312 |
_, filename, _, _ = runtime.Caller(0) |
| 313 |
} |