This repository has been archived by the owner on Aug 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multierr_test.go
80 lines (58 loc) · 1.74 KB
/
multierr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package emperror
import (
"fmt"
"reflect"
"testing"
)
// guarantee multiError implements Errors and error
var _ Errors = new(multiError)
var _ error = new(multiError)
func TestMultiErrorBuilder_ErrOrNil(t *testing.T) {
builder := NewMultiErrorBuilder()
err := fmt.Errorf("error")
builder.Add(err)
merr := builder.ErrOrNil()
if got, want := merr.(Errors).Errors()[0], err; got != want {
t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
}
}
func TestMultiErrorBuilder_ErrOrNil_NilWhenEmpty(t *testing.T) {
builder := NewMultiErrorBuilder()
if got := builder.ErrOrNil(); got != nil {
t.Errorf("unexpected error, received: %s", got)
}
}
func TestMultiErrorBuilder_ErrOrNil_Single(t *testing.T) {
builder := &MultiErrorBuilder{
SingleWrapMode: ReturnSingle,
}
err := fmt.Errorf("error")
builder.Add(err)
if got, want := builder.ErrOrNil(), err; got != want {
t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
}
}
func TestMultiErrorBuilder_Message(t *testing.T) {
want := "Multiple errors happened during action"
builder := &MultiErrorBuilder{
Message: want,
}
err := fmt.Errorf("error")
builder.Add(err)
if got, want := builder.ErrOrNil().Error(), want; got != want {
t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
}
}
func TestMultiErrorBuilder_MultipleErrors(t *testing.T) {
want := []error{
fmt.Errorf("first"),
fmt.Errorf("second"),
}
builder := NewMultiErrorBuilder()
for _, e := range want {
builder.Add(e)
}
if got := builder.ErrOrNil().(Errors).Errors(); !reflect.DeepEqual(got, want) {
t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
}
}