github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/types_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package rpc 13 14 import ( 15 "encoding/json" 16 "testing" 17 18 "github.com/Sberex/go-sberex/common/math" 19 ) 20 21 func TestBlockNumberJSONUnmarshal(t *testing.T) { 22 tests := []struct { 23 input string 24 mustFail bool 25 expected BlockNumber 26 }{ 27 0: {`"0x"`, true, BlockNumber(0)}, 28 1: {`"0x0"`, false, BlockNumber(0)}, 29 2: {`"0X1"`, false, BlockNumber(1)}, 30 3: {`"0x00"`, true, BlockNumber(0)}, 31 4: {`"0x01"`, true, BlockNumber(0)}, 32 5: {`"0x1"`, false, BlockNumber(1)}, 33 6: {`"0x12"`, false, BlockNumber(18)}, 34 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 35 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, 36 9: {"0", true, BlockNumber(0)}, 37 10: {`"ff"`, true, BlockNumber(0)}, 38 11: {`"pending"`, false, PendingBlockNumber}, 39 12: {`"latest"`, false, LatestBlockNumber}, 40 13: {`"earliest"`, false, EarliestBlockNumber}, 41 14: {`someString`, true, BlockNumber(0)}, 42 15: {`""`, true, BlockNumber(0)}, 43 16: {``, true, BlockNumber(0)}, 44 } 45 46 for i, test := range tests { 47 var num BlockNumber 48 err := json.Unmarshal([]byte(test.input), &num) 49 if test.mustFail && err == nil { 50 t.Errorf("Test %d should fail", i) 51 continue 52 } 53 if !test.mustFail && err != nil { 54 t.Errorf("Test %d should pass but got err: %v", i, err) 55 continue 56 } 57 if num != test.expected { 58 t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num) 59 } 60 } 61 }