OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / expr / expr_test.go
1 package expr
2
3 import (
4         "strings"
5         "testing"
6
7         cmn "github.com/tendermint/tmlibs/common"
8 )
9
10 func TestParse(t *testing.T) {
11         testParse(t, `"foobar"`, `"foobar"`)
12         testParse(t, "0x1234", "0x1234")
13         testParse(t, "0xbeef", "0xBEEF")
14         testParse(t, "xbeef", "xBEEF")
15         testParse(t, "12345", "{i 12345}")
16         testParse(t, "u64:12345", "{u64 12345}")
17         testParse(t, "i64:12345", "{i64 12345}")
18         testParse(t, "i64:-12345", "{i64 -12345}")
19         testParse(t, "[1 u64:2]", "[{i 1},{u64 2}]")
20         testParse(t, "[(1 2) (3 4)]", "[({i 1} {i 2}),({i 3} {i 4})]")
21         testParse(t, "0x1234 1 u64:2 [3 4]", "(0x1234 {i 1} {u64 2} [{i 3},{i 4}])")
22         testParse(t, "[(1 <sig:user1>)(2 <sig:user2>)][3 4]",
23                 "([({i 1} <sig:user1>),({i 2} <sig:user2>)] [{i 3},{i 4}])")
24 }
25
26 func testParse(t *testing.T, input string, expected string) {
27         got, err := ParseReader(input, strings.NewReader(input))
28         if err != nil {
29                 t.Error(err.Error())
30                 return
31         }
32         gotStr := cmn.Fmt("%v", got)
33         if gotStr != expected {
34                 t.Error(cmn.Fmt("Expected %v, got %v", expected, gotStr))
35         }
36 }
37
38 func TestBytes(t *testing.T) {
39         testBytes(t, `"foobar"`, `0106666F6F626172`)
40         testBytes(t, "0x1234", "01021234")
41         testBytes(t, "0xbeef", "0102BEEF")
42         testBytes(t, "xbeef", "BEEF")
43         testBytes(t, "12345", "023039")
44         testBytes(t, "u64:12345", "0000000000003039")
45         testBytes(t, "i64:12345", "0000000000003039")
46         testBytes(t, "i64:-12345", "FFFFFFFFFFFFCFC7")
47         testBytes(t, "[1 u64:2]", "010201010000000000000002")
48         testBytes(t, "[(1 2) (3 4)]", "01020101010201030104")
49         testBytes(t, "0x1234 1 u64:2 [3 4]", "0102123401010000000000000002010201030104")
50         testBytes(t, "[(1 <sig:user1>)(2 <sig:user2>)][3 4]",
51                 "0102010100010200010201030104")
52 }
53
54 func testBytes(t *testing.T, input string, expected string) {
55         got, err := ParseReader(input, strings.NewReader(input))
56         if err != nil {
57                 t.Error(err.Error())
58                 return
59         }
60         gotBytes, err := got.(Byteful).Bytes()
61         if err != nil {
62                 t.Error(err.Error())
63                 return
64         }
65         gotHex := cmn.Fmt("%X", gotBytes)
66         if gotHex != expected {
67                 t.Error(cmn.Fmt("Expected %v, got %v", expected, gotHex))
68         }
69 }