forked from asaskevich/govalidator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_test.go
61 lines (54 loc) · 1.99 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
package govalidator
import (
"fmt"
"testing"
)
func TestErrorsToString(t *testing.T) {
t.Parallel()
customErr := &Error{Path: []string{"Custom Error Name"}, Err: fmt.Errorf("stdlib error")}
customErrWithCustomErrorMessage := &Error{Path: []string{"Custom Error Name 2"}, Err: fmt.Errorf("Bad stuff happened"), CustomErrorMessageExists: true}
var tests = []struct {
param1 Errors
expected string
}{
{Errors{}, ""},
{Errors{fmt.Errorf("Error 1")}, "Error 1"},
{Errors{fmt.Errorf("Error 1"), fmt.Errorf("Error 2")}, "Error 1;Error 2"},
{Errors{customErr, fmt.Errorf("Error 2")}, "Custom Error Name: stdlib error;Error 2"},
{Errors{fmt.Errorf("Error 123"), customErrWithCustomErrorMessage}, "Error 123;Bad stuff happened"},
}
for _, test := range tests {
actual := test.param1.Error()
if actual != test.expected {
t.Errorf("Expected Error() to return '%v', got '%v'", test.expected, actual)
}
}
}
func TestPrependPathToErrors(t *testing.T) {
var tests = []struct {
err Errors
expected Errors
}{
{Errors{&Error{Err: fmt.Errorf("Some Error Occured"), Path: []string{"Field"}}}, Errors{&Error{Err: fmt.Errorf("Some Error Occured"), Path: []string{"foo", "bar", "Field"}}}},
}
for _, test := range tests {
prependedErrors := PrependPathToErrors(test.err, "foo", "bar")
if prependedErrors.Error() != test.expected.Error() {
t.Errorf("Expected Error() to return '%v', got '%v'", test.expected.Error(), prependedErrors.Error())
}
}
}
func TestAppendPathToErrors(t *testing.T) {
var tests = []struct {
err Errors
expected Errors
}{
{Errors{&Error{Err: fmt.Errorf("Some Error Occured"), Path: []string{"Field"}}}, Errors{&Error{Err: fmt.Errorf("Some Error Occured"), Path: []string{"Field", "foo", "bar"}}}},
}
for _, test := range tests {
appendedErrors := AppendPathToErrors(test.err, "foo", "bar")
if appendedErrors.Error() != test.expected.Error() {
t.Errorf("Expected Error() to return '%v', got '%v'", test.expected.Error(), appendedErrors.Error())
}
}
}