parser_func_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. input = `
  180. func() {
  181. a := 1
  182. return
  183. b := 2
  184. return
  185. }
  186. `
  187. expectedOutput = `
  188. function
  189. params
  190. statements
  191. :=
  192. identifier: a
  193. number: 1
  194. return
  195. :=
  196. identifier: b
  197. number: 2
  198. return
  199. `[1:]
  200. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  201. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  202. return
  203. }
  204. }
  205. func TestFunctionCalling(t *testing.T) {
  206. input := `import "foo/bar.ecal" as foobar
  207. foobar.test()`
  208. expectedOutput := `
  209. statements
  210. import
  211. string: 'foo/bar.ecal'
  212. identifier: foobar
  213. identifier: foobar
  214. identifier: test
  215. funccall
  216. `[1:]
  217. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  218. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  219. return
  220. }
  221. input = `a := 1
  222. a().foo := x2.foo()
  223. a.b.c().foo := a()
  224. `
  225. expectedOutput = `
  226. statements
  227. :=
  228. identifier: a
  229. number: 1
  230. :=
  231. identifier: a
  232. funccall
  233. identifier: foo
  234. identifier: x2
  235. identifier: foo
  236. funccall
  237. :=
  238. identifier: a
  239. identifier: b
  240. identifier: c
  241. funccall
  242. identifier: foo
  243. identifier: a
  244. funccall
  245. `[1:]
  246. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  247. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  248. return
  249. }
  250. input = `a(1+2).foo := x2.foo(foo)
  251. a.b.c(x()).foo := a(1,a(),3, x, y) + 1
  252. `
  253. expectedOutput = `
  254. statements
  255. :=
  256. identifier: a
  257. funccall
  258. plus
  259. number: 1
  260. number: 2
  261. identifier: foo
  262. identifier: x2
  263. identifier: foo
  264. funccall
  265. identifier: foo
  266. :=
  267. identifier: a
  268. identifier: b
  269. identifier: c
  270. funccall
  271. identifier: x
  272. funccall
  273. identifier: foo
  274. plus
  275. identifier: a
  276. funccall
  277. number: 1
  278. identifier: a
  279. funccall
  280. number: 3
  281. identifier: x
  282. identifier: y
  283. number: 1
  284. `[1:]
  285. if res, err := UnitTestParseWithPPResult("mytest", input, ""); err != nil || fmt.Sprint(res) != expectedOutput {
  286. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  287. return
  288. }
  289. }