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.

86 lines
2.4 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 TestParse(t *testing.T) {
  10. s := `
  11. // deposits
  12. A (1): 10
  13. A (2): 20
  14. B (1): 5
  15. // L2 transactions
  16. A-B (1): 6 1
  17. B-C (1): 3 1
  18. // set new batch, label does not affect
  19. > batch1
  20. C-A (1): 3 1
  21. A-B (2): 15 1
  22. User0 (1): 20
  23. User1 (3) : 20
  24. User0-User1 (1): 15 1
  25. User1-User0 (3): 15 1
  26. // Exits
  27. A (1) E: 5
  28. `
  29. parser := NewParser(strings.NewReader(s))
  30. instructions, err := parser.Parse()
  31. assert.Nil(t, err)
  32. assert.Equal(t, 13, len(instructions.Instructions))
  33. // assert.Equal(t, 5, len(instructions.Accounts))
  34. fmt.Println(instructions.Accounts)
  35. assert.Equal(t, 3, len(instructions.TokenIDs))
  36. if debug {
  37. fmt.Println(instructions)
  38. for _, instruction := range instructions.Instructions {
  39. fmt.Println(instruction.Raw())
  40. }
  41. }
  42. assert.Equal(t, TypeNewBatch, instructions.Instructions[5].Type)
  43. assert.Equal(t, "User0 (1): 20", instructions.Instructions[8].Raw())
  44. assert.Equal(t, "Type: Create&Deposit, From: User0, Amount: 20, TokenID: 1,\n", instructions.Instructions[8].String())
  45. assert.Equal(t, "User0-User1 (1): 15 1", instructions.Instructions[10].Raw())
  46. assert.Equal(t, "Type: Transfer, From: User0, To: User1, Amount: 15, Fee: 1, TokenID: 1,\n", instructions.Instructions[10].String())
  47. assert.Equal(t, "A (1)E: 5", instructions.Instructions[12].Raw())
  48. assert.Equal(t, "Type: ForceExit, From: A, Amount: 5, TokenID: 1,\n", instructions.Instructions[12].String())
  49. }
  50. func TestParseErrors(t *testing.T) {
  51. s := "A (1):: 10"
  52. parser := NewParser(strings.NewReader(s))
  53. _, err := parser.Parse()
  54. assert.Equal(t, "error parsing line 0: A(1):: 10, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
  55. s = "A (1): 10 20"
  56. parser = NewParser(strings.NewReader(s))
  57. _, err = parser.Parse()
  58. assert.Equal(t, "error parsing line 1: 20, err: strconv.Atoi: parsing \"\": invalid syntax", err.Error())
  59. s = "A B (1): 10"
  60. parser = NewParser(strings.NewReader(s))
  61. _, err = parser.Parse()
  62. assert.Equal(t, "error parsing line 0: AB(1): 10, err: strconv.Atoi: parsing \"(\": invalid syntax", err.Error())
  63. s = "A-B (1): 10 255"
  64. parser = NewParser(strings.NewReader(s))
  65. _, err = parser.Parse()
  66. assert.Nil(t, err)
  67. s = "A-B (1): 10 256"
  68. parser = NewParser(strings.NewReader(s))
  69. _, err = parser.Parse()
  70. assert.Equal(t, "error parsing line 0: A-B(1):10256, err: Fee 256 can not be bigger than 255", err.Error())
  71. }