-
Notifications
You must be signed in to change notification settings - Fork 45
/
errors_test.go
117 lines (102 loc) · 2.62 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package binding
import (
"fmt"
"testing"
)
func TestErrorsAdd(t *testing.T) {
var actual Errors
expected := Errors{
Error{
FieldNames: []string{"Field1", "Field2"},
Classification: "ErrorClass",
Message: "Some message",
},
}
actual.Add(expected[0].FieldNames, expected[0].Classification, expected[0].Message)
if len(actual) != 1 {
t.Errorf("Expected 1 error, but actually had %d", len(actual))
return
}
expectedStr := fmt.Sprintf("%#v", expected)
actualStr := fmt.Sprintf("%#v", actual)
if actualStr != expectedStr {
t.Errorf("Expected:\n%s\nbut got:\n%s", expectedStr, actualStr)
}
}
func TestErrorsLen(t *testing.T) {
actual := errorsTestSet.Len()
expected := len(errorsTestSet)
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
return
}
}
func TestErrorsHas(t *testing.T) {
if errorsTestSet.Has("ClassA") != true {
t.Errorf("Expected to have error of kind ClassA, but didn't")
}
if errorsTestSet.Has("ClassQ") != false {
t.Errorf("Expected to NOT have error of kind ClassQ, but did")
}
}
func TestErrorGetters(t *testing.T) {
err := Error{
FieldNames: []string{"field1", "field2"},
Classification: "ErrorClass",
Message: "The message",
}
fieldsActual := err.Fields()
if len(fieldsActual) != 2 {
t.Errorf("Expected Fields() to return 2 errors, but got %d", len(fieldsActual))
} else {
if fieldsActual[0] != "field1" || fieldsActual[1] != "field2" {
t.Errorf("Expected Fields() to return the correct fields, but it didn't")
}
}
if err.Kind() != "ErrorClass" {
t.Errorf("Expected the classification to be 'ErrorClass', but got '%s'", err.Kind())
}
if err.Error() != "The message" {
t.Errorf("Expected the message to be 'The message', but got '%s'", err.Error())
}
}
/*
func TestErrorsWithClass(t *testing.T) {
expected := Errors{
errorsTestSet[0],
errorsTestSet[3],
}
actualStr := fmt.Sprintf("%#v", errorsTestSet.WithClass("ClassA"))
expectedStr := fmt.Sprintf("%#v", expected)
if actualStr != expectedStr {
t.Errorf("Expected:\n%s\nbut got:\n%s", expectedStr, actualStr)
}
}
*/
var errorsTestSet = Errors{
Error{
FieldNames: []string{},
Classification: "ClassA",
Message: "Foobar",
},
Error{
FieldNames: []string{},
Classification: "ClassB",
Message: "Foo",
},
Error{
FieldNames: []string{"field1", "field2"},
Classification: "ClassB",
Message: "Foobar",
},
Error{
FieldNames: []string{"field2"},
Classification: "ClassA",
Message: "Foobar",
},
Error{
FieldNames: []string{"field2"},
Classification: "ClassB",
Message: "Foobar",
},
}