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