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.

132 lines
3.9 KiB

  1. package test
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. var debug = false
  9. func TestParseBlockchainTxs(t *testing.T) {
  10. s := `
  11. // deposits
  12. Deposit(1) A: 10
  13. Deposit(2) A: 20
  14. Deposit(1) B: 5
  15. CreateAccountDeposit(1) C: 5
  16. CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
  17. // L2 transactions
  18. Transfer(1) A-B: 6 (1)
  19. Transfer(1) B-D: 3 (1)
  20. // set new batch
  21. > batch
  22. DepositTransfer(1) A-B: 15, 10 (1)
  23. Transfer(1) C-A : 3 (1)
  24. Transfer(2) A-B: 15 (1)
  25. Deposit(1) User0: 20
  26. Deposit(3) User1: 20
  27. Transfer(1) User0-User1: 15 (1)
  28. Transfer(3) User1-User0: 15 (1)
  29. > batch
  30. Transfer(1) User1-User0: 1 (1)
  31. > batch
  32. > block
  33. // Exits
  34. Exit(1) A: 5
  35. `
  36. parser := NewParser(strings.NewReader(s))
  37. instructions, err := parser.Parse()
  38. assert.Nil(t, err)
  39. assert.Equal(t, 20, len(instructions.Instructions))
  40. assert.Equal(t, 10, len(instructions.Accounts))
  41. assert.Equal(t, 3, len(instructions.TokenIDs))
  42. if debug {
  43. fmt.Println(instructions)
  44. for _, instruction := range instructions.Instructions {
  45. fmt.Println(instruction.Raw())
  46. }
  47. }
  48. assert.Equal(t, TypeNewBatch, instructions.Instructions[7].Type)
  49. assert.Equal(t, "Deposit(1)User0:20", instructions.Instructions[11].Raw())
  50. assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.Instructions[8].String())
  51. assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.Instructions[14].String())
  52. assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.Instructions[10].Raw())
  53. assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.Instructions[10].String())
  54. assert.Equal(t, "Exit(1)A:5", instructions.Instructions[19].Raw())
  55. assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.Instructions[19].String())
  56. }
  57. func TestParsePoolTxs(t *testing.T) {
  58. s := `
  59. PoolTransfer(1) A-B: 6 (1)
  60. PoolTransfer(2) A-B: 3 (3)
  61. PoolTransfer(1) B-D: 3 (1)
  62. PoolTransfer(1) C-D: 3 (1)
  63. Exit(1) A: 5
  64. `
  65. parser := NewParser(strings.NewReader(s))
  66. instructions, err := parser.Parse()
  67. assert.Nil(t, err)
  68. assert.Equal(t, 5, len(instructions.Instructions))
  69. assert.Equal(t, 6, len(instructions.Accounts))
  70. assert.Equal(t, 2, len(instructions.TokenIDs))
  71. if debug {
  72. fmt.Println(instructions)
  73. for _, instruction := range instructions.Instructions {
  74. fmt.Println(instruction.Raw())
  75. }
  76. }
  77. assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.Instructions[0].Raw())
  78. assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.Instructions[1].Raw())
  79. assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.Instructions[2].Raw())
  80. assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.Instructions[3].Raw())
  81. assert.Equal(t, "Exit(1)A:5", instructions.Instructions[4].Raw())
  82. }
  83. func TestParseErrors(t *testing.T) {
  84. s := "Deposit(1) A:: 10"
  85. parser := NewParser(strings.NewReader(s))
  86. _, err := parser.Parse()
  87. assert.Equal(t, "error parsing line 0: Deposit(1)A:: 10, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
  88. s = "Deposit(1) A: 10 20"
  89. parser = NewParser(strings.NewReader(s))
  90. _, err = parser.Parse()
  91. assert.Equal(t, "error parsing line 1: 20, err: Unexpected tx type: 20", err.Error())
  92. s = "Transfer(1) A B: 10"
  93. parser = NewParser(strings.NewReader(s))
  94. _, err = parser.Parse()
  95. assert.Equal(t, "error parsing line 0: Transfer(1)AB: 10, err: Expected ':', found 'B'", err.Error())
  96. s = "Transfer(1) A-B: 10 (255)"
  97. parser = NewParser(strings.NewReader(s))
  98. _, err = parser.Parse()
  99. assert.Nil(t, err)
  100. s = "Transfer(1) A-B: 10 (256)"
  101. parser = NewParser(strings.NewReader(s))
  102. _, err = parser.Parse()
  103. assert.Equal(t, "error parsing line 0: Transfer(1)A-B:10(256), err: Fee 256 can not be bigger than 255", err.Error())
  104. s = "> btch"
  105. parser = NewParser(strings.NewReader(s))
  106. _, err = parser.Parse()
  107. assert.Equal(t, "error parsing line 0: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error())
  108. }