parser_main_test.go 4.0 KB

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