github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/rpc/types_test.go (about) 1 package rpc 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/quickchainproject/quickchain/common/math" 8 ) 9 10 func TestBlockNumberJSONUnmarshal(t *testing.T) { 11 tests := []struct { 12 input string 13 mustFail bool 14 expected BlockNumber 15 }{ 16 0: {`"0x"`, true, BlockNumber(0)}, 17 1: {`"0x0"`, false, BlockNumber(0)}, 18 2: {`"0X1"`, false, BlockNumber(1)}, 19 3: {`"0x00"`, true, BlockNumber(0)}, 20 4: {`"0x01"`, true, BlockNumber(0)}, 21 5: {`"0x1"`, false, BlockNumber(1)}, 22 6: {`"0x12"`, false, BlockNumber(18)}, 23 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 24 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, 25 9: {"0", true, BlockNumber(0)}, 26 10: {`"ff"`, true, BlockNumber(0)}, 27 11: {`"pending"`, false, PendingBlockNumber}, 28 12: {`"latest"`, false, LatestBlockNumber}, 29 13: {`"earliest"`, false, EarliestBlockNumber}, 30 14: {`someString`, true, BlockNumber(0)}, 31 15: {`""`, true, BlockNumber(0)}, 32 16: {``, true, BlockNumber(0)}, 33 } 34 35 for i, test := range tests { 36 var num BlockNumber 37 err := json.Unmarshal([]byte(test.input), &num) 38 if test.mustFail && err == nil { 39 t.Errorf("Test %d should fail", i) 40 continue 41 } 42 if !test.mustFail && err != nil { 43 t.Errorf("Test %d should pass but got err: %v", i, err) 44 continue 45 } 46 if num != test.expected { 47 t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num) 48 } 49 } 50 }