parser_func_test.go 6.1 KB

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