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.

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