This repository was archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
75 lines (68 loc) · 1.67 KB
/
error_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
// Copyright 2018 Romano de Souza. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package coalesce_test
import (
"errors"
"reflect"
"testing"
"github.com/romanodesouza/coalesce"
)
func TestError(t *testing.T) {
tests := []struct {
name string
in []error
want error
}{
{
name: "it should return the first non-nil value",
in: []error{nil, errors.New("err: a"), errors.New("err: b")},
want: errors.New("err: a"),
},
{
name: "it should return nil if a non-nil value has not been found",
in: []error{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.Error(tt.in...)
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v; want %v", got, tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}
func TestErrorSlice(t *testing.T) {
tests := []struct {
name string
in [][]error
want []error
}{
{
name: "it should return the first non-nil value",
in: [][]error{nil, []error{errors.New("err: a")}, []error{errors.New("err: b")}},
want: []error{errors.New("err: a")},
},
{
name: "it should return nil if a non-nil value has not been found",
in: [][]error{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.ErrorSlice(tt.in...)
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v; want %v", got, tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}