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.

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