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.

104 lines
1.9 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. Tags: []cmn.KVPair{
  21. {[]byte("pho"), []byte("bo")},
  22. },
  23. }
  24. b, err = json.Marshal(&r1)
  25. assert.Nil(t, err)
  26. var r2 ResponseCheckTx
  27. err = json.Unmarshal(b, &r2)
  28. assert.Nil(t, err)
  29. assert.Equal(t, r1, r2)
  30. }
  31. func TestWriteReadMessageSimple(t *testing.T) {
  32. cases := []proto.Message{
  33. &RequestEcho{
  34. Message: "Hello",
  35. },
  36. }
  37. for _, c := range cases {
  38. buf := new(bytes.Buffer)
  39. err := WriteMessage(c, buf)
  40. assert.Nil(t, err)
  41. msg := new(RequestEcho)
  42. err = ReadMessage(buf, msg)
  43. assert.Nil(t, err)
  44. assert.Equal(t, c, msg)
  45. }
  46. }
  47. func TestWriteReadMessage(t *testing.T) {
  48. cases := []proto.Message{
  49. &Header{
  50. NumTxs: 4,
  51. },
  52. // TODO: add the rest
  53. }
  54. for _, c := range cases {
  55. buf := new(bytes.Buffer)
  56. err := WriteMessage(c, buf)
  57. assert.Nil(t, err)
  58. msg := new(Header)
  59. err = ReadMessage(buf, msg)
  60. assert.Nil(t, err)
  61. assert.Equal(t, c, msg)
  62. }
  63. }
  64. func TestWriteReadMessage2(t *testing.T) {
  65. phrase := "hello-world"
  66. cases := []proto.Message{
  67. &ResponseCheckTx{
  68. Data: []byte(phrase),
  69. Log: phrase,
  70. GasWanted: 10,
  71. Tags: []cmn.KVPair{
  72. cmn.KVPair{[]byte("abc"), []byte("def")},
  73. },
  74. // Fee: cmn.KI64Pair{
  75. },
  76. // TODO: add the rest
  77. }
  78. for _, c := range cases {
  79. buf := new(bytes.Buffer)
  80. err := WriteMessage(c, buf)
  81. assert.Nil(t, err)
  82. msg := new(ResponseCheckTx)
  83. err = ReadMessage(buf, msg)
  84. assert.Nil(t, err)
  85. assert.Equal(t, c, msg)
  86. }
  87. }