OSDN Git Service

Wallet store test (#312)
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / data / json_test.go
1 package data_test
2
3 import (
4         "encoding/json"
5         "testing"
6
7         "github.com/stretchr/testify/assert"
8         "github.com/stretchr/testify/require"
9         data "github.com/tendermint/go-wire/data"
10 )
11
12 func TestSimpleJSON(t *testing.T) {
13         assert, require := assert.New(t), require.New(t)
14
15         cases := []struct {
16                 foo Fooer
17         }{
18                 {foo: Bar{Name: "Fly"}},
19                 {foo: Baz{Name: "For Bar"}},
20         }
21
22         for _, tc := range cases {
23                 assert.NotEmpty(tc.foo.Foo())
24                 wrap := FooerS{tc.foo}
25                 parsed := FooerS{}
26                 d, err := json.Marshal(wrap)
27                 require.Nil(err, "%+v", err)
28                 err = json.Unmarshal(d, &parsed)
29                 require.Nil(err, "%+v", err)
30                 assert.Equal(tc.foo.Foo(), parsed.Foo())
31         }
32 }
33
34 func TestNestedJSON(t *testing.T) {
35         assert, require := assert.New(t), require.New(t)
36
37         cases := []struct {
38                 expected string
39                 foo      Fooer
40         }{
41                 {"Bar Fly", Bar{Name: "Fly"}},
42                 {"Foz Baz", Baz{Name: "For Bar"}},
43                 {"My: Bar None", Nested{"My", FooerS{Bar{"None"}}}},
44         }
45
46         for _, tc := range cases {
47                 assert.Equal(tc.expected, tc.foo.Foo())
48                 wrap := FooerS{tc.foo}
49                 parsed := FooerS{}
50                 // also works with indentation
51                 d, err := data.ToJSON(wrap)
52                 require.Nil(err, "%+v", err)
53                 err = json.Unmarshal(d, &parsed)
54                 require.Nil(err, "%+v", err)
55                 assert.Equal(tc.expected, parsed.Foo())
56         }
57 }