-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors_test.go
71 lines (62 loc) · 1.56 KB
/
errors_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
package goval_test
import (
"errors"
"github.com/pkg-id/goval"
"testing"
)
func TestRuleError(t *testing.T) {
err := goval.NewRuleError(goval.NumberMin, 3)
exp := `{"code":3001,"args":[3]}`
got := err.Error()
if got != exp {
t.Errorf("expect string Error: %q; got %q", exp, got)
}
}
func TestTextError(t *testing.T) {
err := goval.TextError("my-error")
exp := "my-error"
got := err.Error()
if got != exp {
t.Errorf("expect string Error: %q; got %q", exp, got)
}
}
func TestKeyError(t *testing.T) {
t.Run("Error: using marshal able error", func(t *testing.T) {
err := goval.KeyError{
Key: "my-key",
Err: goval.TextError("my-error"),
}
str := err.Error()
exp := `{"key":"my-key","err":"my-error"}`
if str != exp {
t.Errorf("expect string Error: %q; got %q", exp, str)
}
})
t.Run("Error: using an error that not implement json marshaller", func(t *testing.T) {
err := goval.KeyError{
Key: "my-key",
Err: errors.New("my-error"), // it should be converted by the json.Marshal of KeyError.
}
str := err.Error()
exp := `{"key":"my-key","err":"my-error"}`
if str != exp {
t.Errorf("expect string Error: %q; got %q", exp, str)
}
})
}
func TestErrors(t *testing.T) {
t.Run("Error: using var", func(t *testing.T) {
var errs goval.Errors
str := errs.Error()
if str != "null" {
t.Errorf("expect string Error: %q; got %q", "null", str)
}
})
t.Run("Error: using make", func(t *testing.T) {
errs := make(goval.Errors, 0)
str := errs.Error()
if str != "[]" {
t.Errorf("expect string Error: %q; got %q", "[]", str)
}
})
}