...
1 package emperror
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 type errorCollection interface {
9 Errors() []error
10 }
11
12 func TestMultiErrorBuilder_ErrOrNil(t *testing.T) {
13 builder := NewMultiErrorBuilder()
14
15 err := fmt.Errorf("error")
16
17 builder.Add(err)
18
19 merr := builder.ErrOrNil()
20
21 if got, want := merr.(errorCollection).Errors()[0], err; got != want {
22 t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
23 }
24 }
25
26 func TestMultiErrorBuilder_ErrOrNil_NilWhenEmpty(t *testing.T) {
27 builder := NewMultiErrorBuilder()
28
29 if got := builder.ErrOrNil(); got != nil {
30 t.Errorf("unexpected error, received: %s", got)
31 }
32 }
33
34 func TestMultiErrorBuilder_ErrOrNil_Single(t *testing.T) {
35 builder := &MultiErrorBuilder{
36 SingleWrapMode: ReturnSingle,
37 }
38
39 err := fmt.Errorf("error")
40
41 builder.Add(err)
42
43 if got, want := builder.ErrOrNil(), err; got != want {
44 t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
45 }
46 }
47
48 func TestMultiErrorBuilder_Message(t *testing.T) {
49 want := "Multiple errors happened during action"
50
51 builder := &MultiErrorBuilder{
52 Message: want,
53 }
54
55 err := fmt.Errorf("error")
56
57 builder.Add(err)
58
59 if got, want := builder.ErrOrNil().Error(), want; got != want {
60 t.Errorf("error does not match the expected one\nactual: %s\nexpected: %s", got, want)
61 }
62 }
63
View as plain text