Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: common/errors/errors_test.go

Issue 1249933002: Move multierror logic into luci-go. (Closed) Base URL: https://github.com/luci/luci-go@add_mathrand
Patch Set: rename Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package errors
6
7 import (
8 "fmt"
9 "testing"
10
11 . "github.com/smartystreets/goconvey/convey"
12 )
13
14 func TestMark(t *testing.T) {
15 Convey("MakeMarkFn works", t, func() {
16 f := MakeMarkFn("errorsTest")
17
18 type Cat struct{ name string }
19
20 // unfortunately... the line numbers for these tests matter
21 c := &Cat{"dude"}
22 var err error
23 err = f(c)
24
25 So(err.Error(), ShouldEqual, "errorsTest - errors_test.go:23 - & {name:dude}")
26 So(f("hello").Error(), ShouldEqual, "errorsTest - errors_test.go :26 - hello")
27
28 So(err.(*MarkedError).Orig, ShouldEqual, c)
29
30 So(f(nil), ShouldBeNil)
31 })
32 }
33
34 func TestMultiError(t *testing.T) {
35 Convey("MultiError works", t, func() {
36 var me error = MultiError{fmt.Errorf("hello"), fmt.Errorf("bob") }
37
38 So(me.Error(), ShouldEqual, `["hello" "bob"]`)
39
40 Convey("MultiErrorFromErrors with errors works", func() {
41 mec := make(chan error, 5)
42 mec <- fmt.Errorf("what")
43 mec <- nil
44 mec <- MakeMarkFn("multiErr")("one-off")
45 close(mec)
46
47 err := MultiErrorFromErrors(mec)
48 So(err.Error(), ShouldEqual, `["what" "multiErr - errors _test.go:44 - one-off"]`)
49 })
50
51 Convey("MultiErrorFromErrors with nil works", func() {
52 So(MultiErrorFromErrors(nil), ShouldBeNil)
53
54 c := make(chan error)
55 close(c)
56 So(MultiErrorFromErrors(c), ShouldBeNil)
57
58 c = make(chan error, 4)
59 c <- nil
60 c <- nil
61 close(c)
62 So(MultiErrorFromErrors(c), ShouldBeNil)
63 })
64 })
65 }
66
67 func ExampleMakeMarkFn() {
68 // usually this would be in some global area of your library
69 mark := MakeMarkFn("cool_package")
70
71 data := 100 // Data can be anything!
72 err := mark(data)
73 fmt.Printf("got: %q\n", err)
74
75 marked := err.(*MarkedError)
76 fmt.Printf("original: %d", marked.Orig)
77
78 // Output:
79 // got: "cool_package - errors_test.go:72 - 100"
80 // original: 100
81 }
82
83 func ExampleMultiError() {
84 errCh := make(chan error, 10)
85 errCh <- nil // nils are ignored
86 errCh <- fmt.Errorf("what")
87 close(errCh)
88
89 err := MultiErrorFromErrors(errCh)
90 fmt.Printf("got: %s len=%d\n", err, len(err.(MultiError)))
91
92 errCh = make(chan error, 10)
93 errCh <- nil // and if the channel only has nils
94 close(errCh)
95
96 err = MultiErrorFromErrors(errCh) // then it returns nil
97 fmt.Printf("got: %v\n", err)
98
99 // Output:
100 // got: ["what"] len=1
101 // got: <nil>
102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698