-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_marshal_test.go
57 lines (47 loc) · 1.46 KB
/
json_marshal_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
package errors
import (
"bytes"
"encoding/json"
"testing"
)
func testJSONRoundTrip(err error) error {
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(err)
var e Error
json.NewDecoder(b).Decode(&e)
return &e
}
func TestErrorMarshalJSON(t *testing.T) {
// Single error. No user is set, so we will have a zero-length field inside.
e1 := New("network unreachable", IO, Str("tcp connection dropped"))
input := e1.(*Error)
output, ok := testJSONRoundTrip(e1).(*Error)
if !ok {
t.Errorf("expected pointer err, but failed")
}
if output.kind != input.kind {
t.Errorf("expected kind %d; got %d", input.kind, output.kind)
}
// Note that error will have lost type information, so just check its Error string.
if input.Error() != output.Error() {
t.Errorf("expected Err => %s; got => %s", input, output)
}
}
func TestWrappedMarshalJSON(t *testing.T) {
// Single error. No user is set, so we will have a zero-length field inside.
e1 := New("network unreachable", IO, Str("tcp connection dropped"))
op := Op("TestWrappedMarshalJSON")
e2 := WithOp(e1, op)
input := Unwrap(e2)
output, ok := testJSONRoundTrip(input).(*Error)
if !ok {
t.Errorf("expected pointer err, but failed")
}
if output.kind != input.kind {
t.Errorf("expected kind %d; got %d", input.kind, output.kind)
}
// Note that error will have lost type information, so just check its Error string.
if input.Error() != output.Error() {
t.Errorf("expected Err => %s; got => %s", input, output)
}
}