...
1 package data
2
3 import (
4 "encoding/json"
5 "testing"
6
7 . "gopkg.in/check.v1"
8 )
9
10
11 func Test(t *testing.T) { TestingT(t) }
12
13 type HexBytesSuite struct{}
14
15 var _ = Suite(&HexBytesSuite{})
16
17 func (HexBytesSuite) TestUnmarshalJSON(c *C) {
18 var data HexBytes
19 err := json.Unmarshal([]byte(`"666f6f"`), &data)
20 c.Assert(err, IsNil)
21 c.Assert(string(data), Equals, "foo")
22 }
23
24 func (HexBytesSuite) TestUnmarshalJSONError(c *C) {
25 var data HexBytes
26
27
28 err := json.Unmarshal([]byte(`"a"`), &data)
29 c.Assert(err, Not(IsNil))
30
31
32 err = json.Unmarshal([]byte(`"zz"`), &data)
33 c.Assert(err, Not(IsNil))
34
35
36 err = json.Unmarshal([]byte("6"), &data)
37 c.Assert(err, Not(IsNil))
38 }
39
40 func (HexBytesSuite) TestMarshalJSON(c *C) {
41 data, err := json.Marshal(HexBytes("foo"))
42 c.Assert(err, IsNil)
43 c.Assert(data, DeepEquals, []byte(`"666f6f"`))
44 }
45
View as plain text