parser_main_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package parser
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestStatementParsing(t *testing.T) {
  16. // Comment parsing without statements
  17. input := `a := 1
  18. b := 2; c:= 3`
  19. expectedOutput := `
  20. statements
  21. :=
  22. identifier: a
  23. number: 1
  24. :=
  25. identifier: b
  26. number: 2
  27. :=
  28. identifier: c
  29. number: 3
  30. `[1:]
  31. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  32. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  33. return
  34. }
  35. }
  36. func TestIdentifierParsing(t *testing.T) {
  37. input := `a := 1
  38. a.foo := 2
  39. a.b.c.foo := a.b
  40. `
  41. expectedOutput := `
  42. statements
  43. :=
  44. identifier: a
  45. number: 1
  46. :=
  47. identifier: a
  48. identifier: foo
  49. number: 2
  50. :=
  51. identifier: a
  52. identifier: b
  53. identifier: c
  54. identifier: foo
  55. identifier: a
  56. identifier: b
  57. `[1:]
  58. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  59. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  60. return
  61. }
  62. input = `a := b[1 + 1]
  63. a[4].foo["aaa"] := c[i]
  64. `
  65. expectedOutput = `
  66. statements
  67. :=
  68. identifier: a
  69. identifier: b
  70. compaccess
  71. plus
  72. number: 1
  73. number: 1
  74. :=
  75. identifier: a
  76. compaccess
  77. number: 4
  78. identifier: foo
  79. compaccess
  80. string: 'aaa'
  81. identifier: c
  82. compaccess
  83. identifier: i
  84. `[1:]
  85. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  86. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  87. return
  88. }
  89. }
  90. func TestCommentParsing(t *testing.T) {
  91. // Comment parsing without statements
  92. input := `/* This is a comment */ a := 1 + 1 # foo bar`
  93. expectedOutput := `
  94. :=
  95. identifier: a # This is a comment
  96. plus
  97. number: 1
  98. number: 1 # foo bar
  99. `[1:]
  100. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  101. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  102. return
  103. }
  104. input = `/* foo */ 1 # foo bar`
  105. expectedOutput = `
  106. number: 1 # foo foo bar
  107. `[1:]
  108. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  109. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  110. return
  111. }
  112. input = `# 123
  113. /* foo */ 1 # foo bar`
  114. expectedOutput = `
  115. number: 1 # foo foo bar
  116. `[1:]
  117. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  118. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  119. return
  120. }
  121. }
  122. func TestErrorConditions(t *testing.T) {
  123. input := ``
  124. if ast, err := Parse("test", input); err == nil || err.Error() != "Parse error in test: Unexpected end" {
  125. t.Errorf("Unexpected result: %v\nAST:\n%v", err, ast)
  126. return
  127. }
  128. input = `a := 1 a`
  129. if ast, err := Parse("test", input); err == nil || err.Error() != `Parse error in test: Unexpected end (extra token id:7 ("a")) (Line:1 Pos:8)` {
  130. t.Errorf("Unexpected result: %v\nAST:\n%v", err, ast)
  131. return
  132. }
  133. tokenStringEntry := astNodeMap[TokenSTRING]
  134. delete(astNodeMap, TokenSTRING)
  135. defer func() {
  136. astNodeMap[TokenSTRING] = tokenStringEntry
  137. }()
  138. input = `"foo"`
  139. if ast, err := Parse("test", input); err == nil || err.Error() != `Parse error in test: Unknown term (id:5 (v:"foo")) (Line:1 Pos:1)` {
  140. t.Errorf("Unexpected result: %v\nAST:\n%v", err, ast)
  141. return
  142. }
  143. // Test parser functions
  144. input = `a := 1 + a`
  145. p := &parser{"test", nil, NewLABuffer(Lex("test", input), 3), nil}
  146. node, _ := p.next()
  147. p.node = node
  148. if err := skipToken(p, TokenAND); err == nil || err.Error() != "Parse error in test: Unexpected term (a) (Line:1 Pos:1)" {
  149. t.Errorf("Unexpected result: %v", err)
  150. return
  151. }
  152. if err := acceptChild(p, node, TokenAND); err == nil || err.Error() != "Parse error in test: Unexpected term (a) (Line:1 Pos:1)" {
  153. t.Errorf("Unexpected result: %v", err)
  154. return
  155. }
  156. }