parser_func_test.go 6.1 KB

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