You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
1.8 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "github.com/gogo/protobuf/proto"
  8. "github.com/stretchr/testify/assert"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. func TestMarshalJSON(t *testing.T) {
  12. b, err := json.Marshal(&ResponseDeliverTx{})
  13. assert.Nil(t, err)
  14. // Do not include empty fields.
  15. assert.False(t, strings.Contains(string(b), "code"))
  16. r1 := ResponseCheckTx{
  17. Code: 1,
  18. Data: []byte("hello"),
  19. GasWanted: 43,
  20. Fee: cmn.KI64Pair{[]byte("pho"), 12},
  21. }
  22. b, err = json.Marshal(&r1)
  23. assert.Nil(t, err)
  24. var r2 ResponseCheckTx
  25. err = json.Unmarshal(b, &r2)
  26. assert.Nil(t, err)
  27. assert.Equal(t, r1, r2)
  28. }
  29. func TestWriteReadMessageSimple(t *testing.T) {
  30. cases := []proto.Message{
  31. &RequestEcho{
  32. Message: "Hello",
  33. },
  34. }
  35. for _, c := range cases {
  36. buf := new(bytes.Buffer)
  37. err := WriteMessage(c, buf)
  38. assert.Nil(t, err)
  39. msg := new(RequestEcho)
  40. err = ReadMessage(buf, msg)
  41. assert.Nil(t, err)
  42. assert.Equal(t, c, msg)
  43. }
  44. }
  45. func TestWriteReadMessage(t *testing.T) {
  46. cases := []proto.Message{
  47. &Header{
  48. NumTxs: 4,
  49. },
  50. // TODO: add the rest
  51. }
  52. for _, c := range cases {
  53. buf := new(bytes.Buffer)
  54. err := WriteMessage(c, buf)
  55. assert.Nil(t, err)
  56. msg := new(Header)
  57. err = ReadMessage(buf, msg)
  58. assert.Nil(t, err)
  59. assert.Equal(t, c, msg)
  60. }
  61. }
  62. func TestWriteReadMessage2(t *testing.T) {
  63. phrase := "hello-world"
  64. cases := []proto.Message{
  65. &ResponseCheckTx{
  66. Data: []byte(phrase),
  67. Log: phrase,
  68. GasWanted: 10,
  69. Tags: []cmn.KVPair{
  70. cmn.KVPair{[]byte("abc"), []byte("def")},
  71. },
  72. // Fee: cmn.KI64Pair{
  73. },
  74. // TODO: add the rest
  75. }
  76. for _, c := range cases {
  77. buf := new(bytes.Buffer)
  78. err := WriteMessage(c, buf)
  79. assert.Nil(t, err)
  80. msg := new(ResponseCheckTx)
  81. err = ReadMessage(buf, msg)
  82. assert.Nil(t, err)
  83. assert.Equal(t, c, msg)
  84. }
  85. }