forked from go-ozzo/ozzo-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
70 lines (59 loc) · 1.38 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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package validation
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewInternalError(t *testing.T) {
err := NewInternalError(errors.New("abc"))
if assert.NotNil(t, err.InternalError()) {
assert.Equal(t, "abc", err.InternalError().Error())
}
}
func TestErrors_Error(t *testing.T) {
errs := Errors{
"B": errors.New("B1"),
"C": errors.New("C1"),
"A": errors.New("A1"),
}
assert.Equal(t, "A: A1; B: B1; C: C1.", errs.Error())
errs = Errors{
"B": errors.New("B1"),
}
assert.Equal(t, "B: B1.", errs.Error())
errs = Errors{}
assert.Equal(t, "", errs.Error())
}
func TestErrors_MarshalMessage(t *testing.T) {
errs := Errors{
"A": errors.New("A1"),
"B": Errors{
"2": errors.New("B1"),
},
}
errsJSON, err := errs.MarshalJSON()
assert.Nil(t, err)
assert.Equal(t, "{\"A\":\"A1\",\"B\":{\"2\":\"B1\"}}", string(errsJSON))
}
func TestErrors_Filter(t *testing.T) {
errs := Errors{
"B": errors.New("B1"),
"C": nil,
"A": errors.New("A1"),
}
err := errs.Filter()
assert.Equal(t, 2, len(errs))
if assert.NotNil(t, err) {
assert.Equal(t, "A: A1; B: B1.", err.Error())
}
errs = Errors{}
assert.Nil(t, errs.Filter())
errs = Errors{
"B": nil,
"C": nil,
}
assert.Nil(t, errs.Filter())
}