OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "testing" | 9 "testing" |
10 | 10 |
11 . "github.com/smartystreets/goconvey/convey" | 11 . "github.com/smartystreets/goconvey/convey" |
12 ) | 12 ) |
13 | 13 |
14 func TestMark(t *testing.T) { | 14 func TestMark(t *testing.T) { |
15 Convey("MakeMarkFn works", t, func() { | 15 Convey("MakeMarkFn works", t, func() { |
16 f := MakeMarkFn("errorsTest") | 16 f := MakeMarkFn("errorsTest") |
17 | 17 |
18 type Cat struct{ name string } | 18 type Cat struct{ name string } |
19 | 19 |
20 // unfortunately... the line numbers for these tests matter | 20 // unfortunately... the line numbers for these tests matter |
21 c := &Cat{"dude"} | 21 c := &Cat{"dude"} |
22 var err error | 22 var err error |
23 err = f(c) | 23 err = f(c) |
24 | 24 |
25 » » So(err.Error(), ShouldEqual, "errorsTest - errors_test.go:23 - &
{name:dude}") | 25 » » So(err.Error(), ShouldEqual, "errorsTest - markederror_test.go:2
3 - &{name:dude}") |
26 » » So(f("hello").Error(), ShouldEqual, "errorsTest - errors_test.go
:26 - hello") | 26 » » So(f("hello").Error(), ShouldEqual, "errorsTest - markederror_te
st.go:26 - hello") |
27 | 27 |
28 So(err.(*MarkedError).Orig, ShouldEqual, c) | 28 So(err.(*MarkedError).Orig, ShouldEqual, c) |
29 | 29 |
30 So(f(nil), ShouldBeNil) | 30 So(f(nil), ShouldBeNil) |
31 }) | 31 }) |
32 } | 32 } |
33 | 33 |
34 func TestMultiError(t *testing.T) { | 34 func TestMultiError(t *testing.T) { |
35 Convey("MultiError works", t, func() { | 35 Convey("MultiError works", t, func() { |
36 var me error = MultiError{fmt.Errorf("hello"), fmt.Errorf("bob")
} | 36 var me error = MultiError{fmt.Errorf("hello"), fmt.Errorf("bob")
} |
37 | 37 |
38 » » So(me.Error(), ShouldEqual, `["hello" "bob"]`) | 38 » » So(me.Error(), ShouldEqual, `hello (and 1 other error)`) |
39 | 39 |
40 Convey("MultiErrorFromErrors with errors works", func() { | 40 Convey("MultiErrorFromErrors with errors works", func() { |
41 mec := make(chan error, 5) | 41 mec := make(chan error, 5) |
| 42 mec <- MakeMarkFn("multiErr")("one-off") |
| 43 mec <- nil |
42 mec <- fmt.Errorf("what") | 44 mec <- fmt.Errorf("what") |
43 mec <- nil | |
44 mec <- MakeMarkFn("multiErr")("one-off") | |
45 close(mec) | 45 close(mec) |
46 | 46 |
47 err := MultiErrorFromErrors(mec) | 47 err := MultiErrorFromErrors(mec) |
48 » » » So(err.Error(), ShouldEqual, `["what" "multiErr - errors
_test.go:44 - one-off"]`) | 48 » » » So(err.Error(), ShouldEqual, `multiErr - markederror_tes
t.go:42 - one-off (and 1 other error)`) |
49 }) | 49 }) |
50 | 50 |
51 Convey("MultiErrorFromErrors with nil works", func() { | 51 Convey("MultiErrorFromErrors with nil works", func() { |
52 So(MultiErrorFromErrors(nil), ShouldBeNil) | 52 So(MultiErrorFromErrors(nil), ShouldBeNil) |
53 | 53 |
54 c := make(chan error) | 54 c := make(chan error) |
55 close(c) | 55 close(c) |
56 So(MultiErrorFromErrors(c), ShouldBeNil) | 56 So(MultiErrorFromErrors(c), ShouldBeNil) |
57 | 57 |
58 c = make(chan error, 4) | 58 c = make(chan error, 4) |
(...skipping 10 matching lines...) Expand all Loading... |
69 mark := MakeMarkFn("cool_package") | 69 mark := MakeMarkFn("cool_package") |
70 | 70 |
71 data := 100 // Data can be anything! | 71 data := 100 // Data can be anything! |
72 err := mark(data) | 72 err := mark(data) |
73 fmt.Printf("got: %q\n", err) | 73 fmt.Printf("got: %q\n", err) |
74 | 74 |
75 marked := err.(*MarkedError) | 75 marked := err.(*MarkedError) |
76 fmt.Printf("original: %d", marked.Orig) | 76 fmt.Printf("original: %d", marked.Orig) |
77 | 77 |
78 // Output: | 78 // Output: |
79 » // got: "cool_package - errors_test.go:72 - 100" | 79 » // got: "cool_package - markederror_test.go:72 - 100" |
80 // original: 100 | 80 // original: 100 |
81 } | 81 } |
82 | 82 |
83 func ExampleMultiError() { | 83 func ExampleMultiError() { |
84 errCh := make(chan error, 10) | 84 errCh := make(chan error, 10) |
85 errCh <- nil // nils are ignored | 85 errCh <- nil // nils are ignored |
86 errCh <- fmt.Errorf("what") | 86 errCh <- fmt.Errorf("what") |
87 close(errCh) | 87 close(errCh) |
88 | 88 |
89 err := MultiErrorFromErrors(errCh) | 89 err := MultiErrorFromErrors(errCh) |
90 fmt.Printf("got: %s len=%d\n", err, len(err.(MultiError))) | 90 fmt.Printf("got: %s len=%d\n", err, len(err.(MultiError))) |
91 | 91 |
92 errCh = make(chan error, 10) | 92 errCh = make(chan error, 10) |
93 errCh <- nil // and if the channel only has nils | 93 errCh <- nil // and if the channel only has nils |
94 close(errCh) | 94 close(errCh) |
95 | 95 |
96 err = MultiErrorFromErrors(errCh) // then it returns nil | 96 err = MultiErrorFromErrors(errCh) // then it returns nil |
97 fmt.Printf("got: %v\n", err) | 97 fmt.Printf("got: %v\n", err) |
98 | 98 |
99 // Output: | 99 // Output: |
100 » // got: ["what"] len=1 | 100 » // got: what len=1 |
101 // got: <nil> | 101 // got: <nil> |
102 } | 102 } |
OLD | NEW |