-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoinerror_test.go
60 lines (53 loc) · 1.46 KB
/
joinerror_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
package dotconfig
import (
"errors"
"strings"
"testing"
)
func TestErrorsReturnsErr(t *testing.T) {
// Make sure calling Errors on an error just returns that in collection
err := errors.New("test error")
errs := Errors(err)
if len(errs) != 1 {
t.Fatalf("Expected 1 error. Got %v", len(errs))
}
}
func TestErrorsReturnsNil(t *testing.T) {
// Make sure calling Errors on an error just returns that in collection
errs := Errors(nil)
if errs != nil {
t.Fatal("Expected nil slice")
}
}
type empty struct{}
type required struct {
MyInt int `env:"MY_INT,required"`
}
type doubleRequired struct {
MyInt int `env:"MY_INT,required"`
MySecond int `env:"MY_SECOND, required"`
}
func TestErrorsStringer(t *testing.T) {
// Make sure calling Errors on an error just returns that in collection
_, err := FromReader[empty](strings.NewReader(""))
if err != nil {
t.Fatal("Expected nil slice")
}
// Single error should return in common error format
_, err = FromReader[required](strings.NewReader(""))
want := "key not present in ENV: MY_INT"
if err.Error() != want {
t.Fatalf("Expected %v. Got %v.", want, err.Error())
}
_, err = FromReader[doubleRequired](strings.NewReader(""))
want = `multiple errors:
- key not present in ENV: MY_INT
- key not present in ENV: MY_SECOND`
if err.Error() != want {
t.Fatalf("Expected %v. Got %v.", want, err.Error())
}
errs := joinError{}
if errs.Error() != "" {
t.Fatalf("Expected empty string. Got: %v", errs.Error())
}
}