parser_main_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  113. func TestErrorConditions(t *testing.T) {
  114. input := ``
  115. if ast, err := Parse("test", input); err == nil || err.Error() != "Parse error in test: Unexpected end" {
  116. t.Errorf("Unexpected result: %v\nAST:\n%v", err, ast)
  117. return
  118. }
  119. input = `a := 1 a`
  120. 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)` {
  121. t.Errorf("Unexpected result: %v\nAST:\n%v", err, ast)
  122. return
  123. }
  124. tokenStringEntry := astNodeMap[TokenSTRING]
  125. delete(astNodeMap, TokenSTRING)
  126. defer func() {
  127. astNodeMap[TokenSTRING] = tokenStringEntry
  128. }()
  129. input = `"foo"`
  130. if ast, err := Parse("test", input); err == nil || err.Error() != `Parse error in test: Unknown term (id:5 ("foo")) (Line:1 Pos:1)` {
  131. t.Errorf("Unexpected result: %v\nAST:\n%v", err, ast)
  132. return
  133. }
  134. // Test parser functions
  135. input = `a := 1 + a`
  136. p := &parser{"test", nil, NewLABuffer(Lex("test", input), 3), nil}
  137. node, _ := p.next()
  138. p.node = node
  139. if err := skipToken(p, TokenAND); err == nil || err.Error() != "Parse error in test: Unexpected term (a) (Line:1 Pos:1)" {
  140. t.Errorf("Unexpected result: %v", err)
  141. return
  142. }
  143. if err := acceptChild(p, node, TokenAND); err == nil || err.Error() != "Parse error in test: Unexpected term (a) (Line:1 Pos:1)" {
  144. t.Errorf("Unexpected result: %v", err)
  145. return
  146. }
  147. }