-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherr_json_test.go
More file actions
52 lines (50 loc) · 1.36 KB
/
Copy patherr_json_test.go
File metadata and controls
52 lines (50 loc) · 1.36 KB
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
package errors
import (
"reflect"
"testing"
)
func TestErrorJSON(t *testing.T) {
var toJSON JSONFunc = func(e *Error) interface{} {
return map[string]interface{}{"op": e.Op, "kind": e.Kind}
}
cases := []struct {
name string
e *Error
want interface{}
}{
{name: "Empty", e: &Error{}, want: map[string]interface{}{"code": "", "error": "unknown error", "msg": ""}},
{
name: "WithFields",
e: E(InvalidInput, WithUserMsg("Deal with it!")).(*Error),
want: map[string]interface{}{
"code": "INVALID_INPUT",
"error": "invalid input",
"msg": "Deal with it!",
},
},
{
name: "WithToJSON",
e: E(WithOp("Get"), NotFound, WithToJSON(toJSON)).(*Error),
want: map[string]interface{}{"op": "Get", "kind": NotFound},
},
{
name: "WithNestedToJSON",
e: E(WithOp("Get"), NotFound, WithToJSON(toJSON), WithErr(E(
WithToJSON(func(*Error) interface{} { return 420 }),
))).(*Error),
want: map[string]interface{}{"op": "Get", "kind": NotFound},
},
{
name: "WithOnlyNestedToJSON",
e: E(WithOp("Get"), NotFound, WithErr(E(WithToJSON(toJSON)))).(*Error),
want: map[string]interface{}{"op": "Get", "kind": NotFound},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := tc.e.JSON(); !reflect.DeepEqual(got, tc.want) {
t.Errorf("Error.JSON()=%v; want %v", got, tc.want)
}
})
}
}