types_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. package rpc
  17. import (
  18. "encoding/json"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common/math"
  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. 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. }