github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/rpc/types_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:46</date> 10 //</624342665785249792> 11 12 13 package rpc 14 15 import ( 16 "encoding/json" 17 "testing" 18 19 "github.com/ethereum/go-ethereum/common/math" 20 ) 21 22 func TestBlockNumberJSONUnmarshal(t *testing.T) { 23 tests := []struct { 24 input string 25 mustFail bool 26 expected BlockNumber 27 }{ 28 0: {`"0x"`, true, BlockNumber(0)}, 29 1: {`"0x0"`, false, BlockNumber(0)}, 30 2: {`"0X1"`, false, BlockNumber(1)}, 31 3: {`"0x00"`, true, BlockNumber(0)}, 32 4: {`"0x01"`, true, BlockNumber(0)}, 33 5: {`"0x1"`, false, BlockNumber(1)}, 34 6: {`"0x12"`, false, BlockNumber(18)}, 35 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 36 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, 37 9: {"0", true, BlockNumber(0)}, 38 10: {`"ff"`, true, BlockNumber(0)}, 39 11: {`"pending"`, false, PendingBlockNumber}, 40 12: {`"latest"`, false, LatestBlockNumber}, 41 13: {`"earliest"`, false, EarliestBlockNumber}, 42 14: {`someString`, true, BlockNumber(0)}, 43 15: {`""`, true, BlockNumber(0)}, 44 16: {``, true, BlockNumber(0)}, 45 } 46 47 for i, test := range tests { 48 var num BlockNumber 49 err := json.Unmarshal([]byte(test.input), &num) 50 if test.mustFail && err == nil { 51 t.Errorf("Test %d should fail", i) 52 continue 53 } 54 if !test.mustFail && err != nil { 55 t.Errorf("Test %d should pass but got err: %v", i, err) 56 continue 57 } 58 if num != test.expected { 59 t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num) 60 } 61 } 62 } 63