parser_main_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "fmt"
  12. "testing"
  13. )
  14. func TestStatementParsing(t *testing.T) {
  15. // Comment parsing without statements
  16. input := `a := 1
  17. b := 2; c:= 3`
  18. expectedOutput := `
  19. statements
  20. :=
  21. identifier: a
  22. number: 1
  23. :=
  24. identifier: b
  25. number: 2
  26. :=
  27. identifier: c
  28. number: 3
  29. `[1:]
  30. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  31. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  32. return
  33. }
  34. }
  35. func TestIdentifierParsing(t *testing.T) {
  36. input := `a := 1
  37. a.foo := 2
  38. a.b.c.foo := a.b
  39. `
  40. expectedOutput := `
  41. statements
  42. :=
  43. identifier: a
  44. number: 1
  45. :=
  46. identifier: a
  47. identifier: foo
  48. number: 2
  49. :=
  50. identifier: a
  51. identifier: b
  52. identifier: c
  53. identifier: foo
  54. identifier: a
  55. identifier: b
  56. `[1:]
  57. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  58. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  59. return
  60. }
  61. input = `a := b[1 + 1]
  62. a[4].foo["aaa"] := c[i]
  63. `
  64. expectedOutput = `
  65. statements
  66. :=
  67. identifier: a
  68. identifier: b
  69. compaccess
  70. plus
  71. number: 1
  72. number: 1
  73. :=
  74. identifier: a
  75. compaccess
  76. number: 4
  77. identifier: foo
  78. compaccess
  79. string: 'aaa'
  80. identifier: c
  81. compaccess
  82. identifier: i
  83. `[1:]
  84. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  85. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  86. return
  87. }
  88. }
  89. func TestCommentParsing(t *testing.T) {
  90. // Comment parsing without statements
  91. input := `/* This is a comment*/ a := 1 + 1 # foo bar`
  92. expectedOutput := `
  93. :=
  94. identifier: a # This is a comment
  95. plus
  96. number: 1
  97. number: 1 # foo bar
  98. `[1:]
  99. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  100. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  101. return
  102. }
  103. input = `/* foo */ 1 # foo bar`
  104. expectedOutput = `
  105. number: 1 # foo foo bar
  106. `[1:]
  107. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  108. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  109. return
  110. }
  111. }