parser_func_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 TestImportParsing(t *testing.T) {
  15. input := `import "foo/bar.ecal" as foobar
  16. i := foobar`
  17. expectedOutput := `
  18. statements
  19. import
  20. string: 'foo/bar.ecal'
  21. identifier: foobar
  22. :=
  23. identifier: i
  24. identifier: foobar
  25. `[1:]
  26. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  27. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  28. return
  29. }
  30. }
  31. func TestFuncParsing(t *testing.T) {
  32. input := `import "foo/bar.ecal" as foobar
  33. func myfunc(a, b, c=1) {
  34. foo := a and b and c
  35. return foo
  36. }
  37. `
  38. expectedOutput := `
  39. statements
  40. import
  41. string: 'foo/bar.ecal'
  42. identifier: foobar
  43. function
  44. identifier: myfunc
  45. params
  46. identifier: a
  47. identifier: b
  48. preset
  49. identifier: c
  50. number: 1
  51. statements
  52. :=
  53. identifier: foo
  54. and
  55. and
  56. identifier: a
  57. identifier: b
  58. identifier: c
  59. return
  60. identifier: foo
  61. `[1:]
  62. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  63. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  64. return
  65. }
  66. input = `
  67. func myfunc() {
  68. a := 1
  69. return
  70. b := 2
  71. return
  72. }
  73. `
  74. expectedOutput = `
  75. function
  76. identifier: myfunc
  77. params
  78. statements
  79. :=
  80. identifier: a
  81. number: 1
  82. return
  83. :=
  84. identifier: b
  85. number: 2
  86. return
  87. `[1:]
  88. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  89. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  90. return
  91. }
  92. }
  93. func TestFunctionCalling(t *testing.T) {
  94. input := `import "foo/bar.ecal" as foobar
  95. foobar.test()`
  96. expectedOutput := `
  97. statements
  98. import
  99. string: 'foo/bar.ecal'
  100. identifier: foobar
  101. identifier: foobar
  102. identifier: test
  103. funccall
  104. `[1:]
  105. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  106. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  107. return
  108. }
  109. input = `a := 1
  110. a().foo := x2.foo()
  111. a.b.c().foo := a()
  112. `
  113. expectedOutput = `
  114. statements
  115. :=
  116. identifier: a
  117. number: 1
  118. :=
  119. identifier: a
  120. funccall
  121. identifier: foo
  122. identifier: x2
  123. identifier: foo
  124. funccall
  125. :=
  126. identifier: a
  127. identifier: b
  128. identifier: c
  129. funccall
  130. identifier: foo
  131. identifier: a
  132. funccall
  133. `[1:]
  134. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  135. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  136. return
  137. }
  138. input = `a(1+2).foo := x2.foo(foo)
  139. a.b.c(x()).foo := a(1,a(),3, x, y) + 1
  140. `
  141. expectedOutput = `
  142. statements
  143. :=
  144. identifier: a
  145. funccall
  146. plus
  147. number: 1
  148. number: 2
  149. identifier: foo
  150. identifier: x2
  151. identifier: foo
  152. funccall
  153. identifier: foo
  154. :=
  155. identifier: a
  156. identifier: b
  157. identifier: c
  158. funccall
  159. identifier: x
  160. funccall
  161. identifier: foo
  162. plus
  163. identifier: a
  164. funccall
  165. number: 1
  166. identifier: a
  167. funccall
  168. number: 3
  169. identifier: x
  170. identifier: y
  171. number: 1
  172. `[1:]
  173. if res, err := UnitTestParseWithPPResult("mytest", input, ""); err != nil || fmt.Sprint(res) != expectedOutput {
  174. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  175. return
  176. }
  177. }