parser_func_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 TestSinkParsing(t *testing.T) {
  32. input := `
  33. sink fooBar
  34. kindmatch [ "priority", "t.do.bla" ],
  35. scopematch [ "data.read", "data.write" ],
  36. statematch { "priority:" : 5, test: 1, "bla 1": null },
  37. priority 0,
  38. suppresses [ "test1", test2 ]
  39. {
  40. print("test1");
  41. print("test2")
  42. }
  43. `
  44. expectedOutput := `
  45. sink
  46. identifier: fooBar
  47. kindmatch
  48. list
  49. string: 'priority'
  50. string: 't.do.bla'
  51. scopematch
  52. list
  53. string: 'data.read'
  54. string: 'data.write'
  55. statematch
  56. map
  57. kvp
  58. string: 'priority:'
  59. number: 5
  60. kvp
  61. identifier: test
  62. number: 1
  63. kvp
  64. string: 'bla 1'
  65. null
  66. priority
  67. number: 0
  68. suppresses
  69. list
  70. string: 'test1'
  71. identifier: test2
  72. statements
  73. identifier: print
  74. funccall
  75. string: 'test1'
  76. identifier: print
  77. funccall
  78. string: 'test2'
  79. `[1:]
  80. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  81. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  82. return
  83. }
  84. input = `
  85. sink mySink
  86. kindmatch [ "priority", t.do.bla ]
  87. {
  88. }
  89. `
  90. expectedOutput = `
  91. sink
  92. identifier: mySink
  93. kindmatch
  94. list
  95. string: 'priority'
  96. identifier: t
  97. identifier: do
  98. identifier: bla
  99. statements
  100. `[1:]
  101. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  102. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  103. return
  104. }
  105. input = `
  106. sink fooBar
  107. ==
  108. kindmatch [ "priority", "t.do.bla" ]
  109. {
  110. }
  111. `
  112. if _, err := UnitTestParse("mytest", input); err.Error() !=
  113. "Parse error in mytest: Term cannot start an expression (==) (Line:3 Pos:5)" {
  114. t.Error(err)
  115. return
  116. }
  117. }
  118. func TestFuncParsing(t *testing.T) {
  119. input := `import "foo/bar.ecal" as foobar
  120. func myfunc(a, b, c=1) {
  121. foo := a and b and c
  122. return foo
  123. }
  124. `
  125. expectedOutput := `
  126. statements
  127. import
  128. string: 'foo/bar.ecal'
  129. identifier: foobar
  130. function
  131. identifier: myfunc
  132. params
  133. identifier: a
  134. identifier: b
  135. preset
  136. identifier: c
  137. number: 1
  138. statements
  139. :=
  140. identifier: foo
  141. and
  142. and
  143. identifier: a
  144. identifier: b
  145. identifier: c
  146. return
  147. identifier: foo
  148. `[1:]
  149. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  150. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  151. return
  152. }
  153. input = `
  154. func myfunc() {
  155. a := 1
  156. return
  157. b := 2
  158. return
  159. }
  160. `
  161. expectedOutput = `
  162. function
  163. identifier: myfunc
  164. params
  165. statements
  166. :=
  167. identifier: a
  168. number: 1
  169. return
  170. :=
  171. identifier: b
  172. number: 2
  173. return
  174. `[1:]
  175. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  176. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  177. return
  178. }
  179. }
  180. func TestFunctionCalling(t *testing.T) {
  181. input := `import "foo/bar.ecal" as foobar
  182. foobar.test()`
  183. expectedOutput := `
  184. statements
  185. import
  186. string: 'foo/bar.ecal'
  187. identifier: foobar
  188. identifier: foobar
  189. identifier: test
  190. funccall
  191. `[1:]
  192. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  193. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  194. return
  195. }
  196. input = `a := 1
  197. a().foo := x2.foo()
  198. a.b.c().foo := a()
  199. `
  200. expectedOutput = `
  201. statements
  202. :=
  203. identifier: a
  204. number: 1
  205. :=
  206. identifier: a
  207. funccall
  208. identifier: foo
  209. identifier: x2
  210. identifier: foo
  211. funccall
  212. :=
  213. identifier: a
  214. identifier: b
  215. identifier: c
  216. funccall
  217. identifier: foo
  218. identifier: a
  219. funccall
  220. `[1:]
  221. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  222. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  223. return
  224. }
  225. input = `a(1+2).foo := x2.foo(foo)
  226. a.b.c(x()).foo := a(1,a(),3, x, y) + 1
  227. `
  228. expectedOutput = `
  229. statements
  230. :=
  231. identifier: a
  232. funccall
  233. plus
  234. number: 1
  235. number: 2
  236. identifier: foo
  237. identifier: x2
  238. identifier: foo
  239. funccall
  240. identifier: foo
  241. :=
  242. identifier: a
  243. identifier: b
  244. identifier: c
  245. funccall
  246. identifier: x
  247. funccall
  248. identifier: foo
  249. plus
  250. identifier: a
  251. funccall
  252. number: 1
  253. identifier: a
  254. funccall
  255. number: 3
  256. identifier: x
  257. identifier: y
  258. number: 1
  259. `[1:]
  260. if res, err := UnitTestParseWithPPResult("mytest", input, ""); err != nil || fmt.Sprint(res) != expectedOutput {
  261. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  262. return
  263. }
  264. }