rt_statements_test.go 17 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 interpreter
  11. import (
  12. "testing"
  13. "devt.de/krotik/ecal/scope"
  14. )
  15. func TestGuardStatements(t *testing.T) {
  16. // Test normal if
  17. vs := scope.NewScope(scope.GlobalScope)
  18. _, err := UnitTestEvalAndAST(
  19. `
  20. a := 1
  21. if a == 1 {
  22. b := 1
  23. a := a + 1
  24. }
  25. `, vs,
  26. `
  27. statements
  28. :=
  29. identifier: a
  30. number: 1
  31. if
  32. guard
  33. ==
  34. identifier: a
  35. number: 1
  36. statements
  37. :=
  38. identifier: b
  39. number: 1
  40. :=
  41. identifier: a
  42. plus
  43. identifier: a
  44. number: 1
  45. `[1:])
  46. if err != nil {
  47. t.Error(err)
  48. return
  49. }
  50. if vs.String() != `
  51. GlobalScope {
  52. a (float64) : 2
  53. block: if (Line:3 Pos:1) {
  54. b (float64) : 1
  55. }
  56. }`[1:] {
  57. t.Error("Unexpected result: ", vs)
  58. return
  59. }
  60. // Test elif
  61. vs = scope.NewScope(scope.GlobalScope)
  62. _, err = UnitTestEvalAndAST(
  63. `
  64. a := 2
  65. if a == 1 {
  66. a := a + 1
  67. } elif a == 2 {
  68. a := a + 2
  69. }
  70. `, vs, `
  71. statements
  72. :=
  73. identifier: a
  74. number: 2
  75. if
  76. guard
  77. ==
  78. identifier: a
  79. number: 1
  80. statements
  81. :=
  82. identifier: a
  83. plus
  84. identifier: a
  85. number: 1
  86. guard
  87. ==
  88. identifier: a
  89. number: 2
  90. statements
  91. :=
  92. identifier: a
  93. plus
  94. identifier: a
  95. number: 2
  96. `[1:])
  97. if err != nil {
  98. t.Error(err)
  99. return
  100. }
  101. if vs.String() != `
  102. GlobalScope {
  103. a (float64) : 4
  104. block: if (Line:3 Pos:5) {
  105. }
  106. }`[1:] {
  107. t.Error("Unexpected result: ", vs)
  108. return
  109. }
  110. // Test else
  111. vs = scope.NewScope(scope.GlobalScope)
  112. _, err = UnitTestEvalAndAST(
  113. `
  114. a := 3
  115. if a == 1 {
  116. a := a + 1
  117. } elif a == 2 {
  118. a := a + 2
  119. } else {
  120. a := 99
  121. }
  122. `, vs, `
  123. statements
  124. :=
  125. identifier: a
  126. number: 3
  127. if
  128. guard
  129. ==
  130. identifier: a
  131. number: 1
  132. statements
  133. :=
  134. identifier: a
  135. plus
  136. identifier: a
  137. number: 1
  138. guard
  139. ==
  140. identifier: a
  141. number: 2
  142. statements
  143. :=
  144. identifier: a
  145. plus
  146. identifier: a
  147. number: 2
  148. guard
  149. true
  150. statements
  151. :=
  152. identifier: a
  153. number: 99
  154. `[1:])
  155. if err != nil {
  156. t.Error(err)
  157. return
  158. }
  159. if vs.String() != `
  160. GlobalScope {
  161. a (float64) : 99
  162. block: if (Line:3 Pos:5) {
  163. }
  164. }`[1:] {
  165. t.Error("Unexpected result: ", vs)
  166. return
  167. }
  168. }
  169. func TestLoopStatements(t *testing.T) {
  170. vs := scope.NewScope(scope.GlobalScope)
  171. buf := addLogFunction(vs)
  172. _, err := UnitTestEvalAndAST(
  173. `
  174. a := 10
  175. for a > 0 {
  176. testlog("Info: ", "-> ", a)
  177. a := a - 1
  178. }
  179. `, vs,
  180. `
  181. statements
  182. :=
  183. identifier: a
  184. number: 10
  185. loop
  186. guard
  187. >
  188. identifier: a
  189. number: 0
  190. statements
  191. identifier: testlog
  192. funccall
  193. string: 'Info: '
  194. string: '-> '
  195. identifier: a
  196. :=
  197. identifier: a
  198. minus
  199. identifier: a
  200. number: 1
  201. `[1:])
  202. if err != nil {
  203. t.Error(err)
  204. return
  205. }
  206. if vs.String() != `
  207. GlobalScope {
  208. a (float64) : 0
  209. testlog (*interpreter.TestLogger) : TestLogger
  210. block: loop (Line:4 Pos:1) {
  211. }
  212. }`[1:] {
  213. t.Error("Unexpected result: ", vs)
  214. return
  215. }
  216. if res := buf.String(); res != `
  217. Info: -> 10
  218. Info: -> 9
  219. Info: -> 8
  220. Info: -> 7
  221. Info: -> 6
  222. Info: -> 5
  223. Info: -> 4
  224. Info: -> 3
  225. Info: -> 2
  226. Info: -> 1`[1:] {
  227. t.Error("Unexpected result: ", res)
  228. return
  229. }
  230. vs = scope.NewScope(scope.GlobalScope)
  231. buf = addLogFunction(vs)
  232. _, err = UnitTestEvalAndAST(
  233. `
  234. for a in range(2, 10, 1) {
  235. testlog("Info", "->", a)
  236. }
  237. `, vs,
  238. `
  239. loop
  240. in
  241. identifier: a
  242. identifier: range
  243. funccall
  244. number: 2
  245. number: 10
  246. number: 1
  247. statements
  248. identifier: testlog
  249. funccall
  250. string: 'Info'
  251. string: '->'
  252. identifier: a
  253. `[1:])
  254. if err != nil {
  255. t.Error(err)
  256. return
  257. }
  258. if vs.String() != `
  259. GlobalScope {
  260. testlog (*interpreter.TestLogger) : TestLogger
  261. block: loop (Line:2 Pos:5) {
  262. a (float64) : 10
  263. }
  264. }`[1:] {
  265. t.Error("Unexpected result: ", vs)
  266. return
  267. }
  268. if res := buf.String(); res != `
  269. Info->2
  270. Info->3
  271. Info->4
  272. Info->5
  273. Info->6
  274. Info->7
  275. Info->8
  276. Info->9
  277. Info->10`[1:] {
  278. t.Error("Unexpected result: ", res)
  279. return
  280. }
  281. vs = scope.NewScope(scope.GlobalScope)
  282. buf = addLogFunction(vs)
  283. _, err = UnitTestEvalAndAST(
  284. `
  285. for a in range(10, 3, -3) {
  286. testlog("Info", "->", a)
  287. }
  288. `, vs,
  289. `
  290. loop
  291. in
  292. identifier: a
  293. identifier: range
  294. funccall
  295. number: 10
  296. number: 3
  297. minus
  298. number: 3
  299. statements
  300. identifier: testlog
  301. funccall
  302. string: 'Info'
  303. string: '->'
  304. identifier: a
  305. `[1:])
  306. if err != nil {
  307. t.Error(err)
  308. return
  309. }
  310. if vs.String() != `
  311. GlobalScope {
  312. testlog (*interpreter.TestLogger) : TestLogger
  313. block: loop (Line:2 Pos:1) {
  314. a (float64) : 4
  315. }
  316. }`[1:] {
  317. t.Error("Unexpected result: ", vs)
  318. return
  319. }
  320. if res := buf.String(); res != `
  321. Info->10
  322. Info->7
  323. Info->4`[1:] {
  324. t.Error("Unexpected result: ", res)
  325. return
  326. }
  327. // Test nested loops
  328. vs = scope.NewScope(scope.GlobalScope)
  329. buf = addLogFunction(vs)
  330. _, err = UnitTestEvalAndAST(
  331. `
  332. for a in range(10, 3, -3) {
  333. for b in range(1, 3, 1) {
  334. testlog("Info", "->", a, b)
  335. }
  336. }
  337. `, vs,
  338. `
  339. loop
  340. in
  341. identifier: a
  342. identifier: range
  343. funccall
  344. number: 10
  345. number: 3
  346. minus
  347. number: 3
  348. statements
  349. loop
  350. in
  351. identifier: b
  352. identifier: range
  353. funccall
  354. number: 1
  355. number: 3
  356. number: 1
  357. statements
  358. identifier: testlog
  359. funccall
  360. string: 'Info'
  361. string: '->'
  362. identifier: a
  363. identifier: b
  364. `[1:])
  365. if err != nil {
  366. t.Error(err)
  367. return
  368. }
  369. if vs.String() != `
  370. GlobalScope {
  371. testlog (*interpreter.TestLogger) : TestLogger
  372. block: loop (Line:2 Pos:1) {
  373. a (float64) : 4
  374. block: loop (Line:3 Pos:3) {
  375. b (float64) : 3
  376. }
  377. }
  378. }`[1:] {
  379. t.Error("Unexpected result: ", vs)
  380. return
  381. }
  382. if res := buf.String(); res != `
  383. Info->10 1
  384. Info->10 2
  385. Info->10 3
  386. Info->7 1
  387. Info->7 2
  388. Info->7 3
  389. Info->4 1
  390. Info->4 2
  391. Info->4 3`[1:] {
  392. t.Error("Unexpected result: ", res)
  393. return
  394. }
  395. // Break statement
  396. vs = scope.NewScope(scope.GlobalScope)
  397. buf = addLogFunction(vs)
  398. _, err = UnitTestEvalAndAST(
  399. `
  400. for a in range(1, 10, 1) {
  401. testlog("Info", "->", a)
  402. if a == 3 {
  403. break
  404. }
  405. }`, vs,
  406. `
  407. loop
  408. in
  409. identifier: a
  410. identifier: range
  411. funccall
  412. number: 1
  413. number: 10
  414. number: 1
  415. statements
  416. identifier: testlog
  417. funccall
  418. string: 'Info'
  419. string: '->'
  420. identifier: a
  421. if
  422. guard
  423. ==
  424. identifier: a
  425. number: 3
  426. statements
  427. break
  428. `[1:])
  429. if err != nil {
  430. t.Error(err)
  431. return
  432. }
  433. if vs.String() != `
  434. GlobalScope {
  435. testlog (*interpreter.TestLogger) : TestLogger
  436. block: loop (Line:2 Pos:1) {
  437. a (float64) : 3
  438. block: if (Line:4 Pos:3) {
  439. }
  440. }
  441. }`[1:] {
  442. t.Error("Unexpected result: ", vs)
  443. return
  444. }
  445. if res := buf.String(); res != `
  446. Info->1
  447. Info->2
  448. Info->3`[1:] {
  449. t.Error("Unexpected result: ", res)
  450. return
  451. }
  452. // Continue statement
  453. vs = scope.NewScope(scope.GlobalScope)
  454. buf = addLogFunction(vs)
  455. _, err = UnitTestEvalAndAST(
  456. `
  457. for a in range(1, 10, 1) {
  458. if a > 3 and a < 6 {
  459. continue
  460. }
  461. testlog("Info", "->", a)
  462. }`, vs,
  463. `
  464. loop
  465. in
  466. identifier: a
  467. identifier: range
  468. funccall
  469. number: 1
  470. number: 10
  471. number: 1
  472. statements
  473. if
  474. guard
  475. and
  476. >
  477. identifier: a
  478. number: 3
  479. <
  480. identifier: a
  481. number: 6
  482. statements
  483. continue
  484. identifier: testlog
  485. funccall
  486. string: 'Info'
  487. string: '->'
  488. identifier: a
  489. `[1:])
  490. if err != nil {
  491. t.Error(err)
  492. return
  493. }
  494. if vs.String() != `
  495. GlobalScope {
  496. testlog (*interpreter.TestLogger) : TestLogger
  497. block: loop (Line:2 Pos:1) {
  498. a (float64) : 10
  499. block: if (Line:3 Pos:3) {
  500. }
  501. }
  502. }`[1:] {
  503. t.Error("Unexpected result: ", vs)
  504. return
  505. }
  506. if res := buf.String(); res != `
  507. Info->1
  508. Info->2
  509. Info->3
  510. Info->6
  511. Info->7
  512. Info->8
  513. Info->9
  514. Info->10`[1:] {
  515. t.Error("Unexpected result: ", res)
  516. return
  517. }
  518. // Loop over lists
  519. vs = scope.NewScope(scope.GlobalScope)
  520. buf = addLogFunction(vs)
  521. _, err = UnitTestEvalAndAST(
  522. `
  523. for a in [1,2] {
  524. for b in [1,2,3,"Hans", 4] {
  525. testlog("Info", "->", a, "-", b)
  526. }
  527. }
  528. `, vs,
  529. `
  530. loop
  531. in
  532. identifier: a
  533. list
  534. number: 1
  535. number: 2
  536. statements
  537. loop
  538. in
  539. identifier: b
  540. list
  541. number: 1
  542. number: 2
  543. number: 3
  544. string: 'Hans'
  545. number: 4
  546. statements
  547. identifier: testlog
  548. funccall
  549. string: 'Info'
  550. string: '->'
  551. identifier: a
  552. string: '-'
  553. identifier: b
  554. `[1:])
  555. if err != nil {
  556. t.Error(err)
  557. return
  558. }
  559. if vs.String() != `
  560. GlobalScope {
  561. testlog (*interpreter.TestLogger) : TestLogger
  562. block: loop (Line:2 Pos:1) {
  563. a (float64) : 2
  564. block: loop (Line:3 Pos:3) {
  565. b (float64) : 4
  566. }
  567. }
  568. }`[1:] {
  569. t.Error("Unexpected result: ", vs)
  570. return
  571. }
  572. if res := buf.String(); res != `
  573. Info->1-1
  574. Info->1-2
  575. Info->1-3
  576. Info->1-Hans
  577. Info->1-4
  578. Info->2-1
  579. Info->2-2
  580. Info->2-3
  581. Info->2-Hans
  582. Info->2-4`[1:] {
  583. t.Error("Unexpected result: ", res)
  584. return
  585. }
  586. vs = scope.NewScope(scope.GlobalScope)
  587. buf = addLogFunction(vs)
  588. _, err = UnitTestEvalAndAST(
  589. `
  590. l := [1,2,3,4]
  591. for a in range(0, 3, 1) {
  592. testlog("Info", "-a>", a, "-", l[a])
  593. }
  594. for a in range(0, 3, 1) {
  595. testlog("Info", "-b>", a, "-", l[-a])
  596. }
  597. testlog("Info", "xxx>", l[-1])
  598. `, vs,
  599. `
  600. statements
  601. :=
  602. identifier: l
  603. list
  604. number: 1
  605. number: 2
  606. number: 3
  607. number: 4
  608. loop
  609. in
  610. identifier: a
  611. identifier: range
  612. funccall
  613. number: 0
  614. number: 3
  615. number: 1
  616. statements
  617. identifier: testlog
  618. funccall
  619. string: 'Info'
  620. string: '-a>'
  621. identifier: a
  622. string: '-'
  623. identifier: l
  624. compaccess
  625. identifier: a
  626. loop
  627. in
  628. identifier: a
  629. identifier: range
  630. funccall
  631. number: 0
  632. number: 3
  633. number: 1
  634. statements
  635. identifier: testlog
  636. funccall
  637. string: 'Info'
  638. string: '-b>'
  639. identifier: a
  640. string: '-'
  641. identifier: l
  642. compaccess
  643. minus
  644. identifier: a
  645. identifier: testlog
  646. funccall
  647. string: 'Info'
  648. string: 'xxx>'
  649. identifier: l
  650. compaccess
  651. minus
  652. number: 1
  653. `[1:])
  654. if err != nil {
  655. t.Error(err)
  656. return
  657. }
  658. if vs.String() != `
  659. GlobalScope {
  660. l ([]interface {}) : [1,2,3,4]
  661. testlog (*interpreter.TestLogger) : TestLogger
  662. block: loop (Line:3 Pos:1) {
  663. a (float64) : 3
  664. }
  665. block: loop (Line:6 Pos:1) {
  666. a (float64) : 3
  667. }
  668. }`[1:] {
  669. t.Error("Unexpected result: ", vs)
  670. return
  671. }
  672. if res := buf.String(); res != `
  673. Info-a>0-1
  674. Info-a>1-2
  675. Info-a>2-3
  676. Info-a>3-4
  677. Info-b>0-1
  678. Info-b>1-4
  679. Info-b>2-3
  680. Info-b>3-2
  681. Infoxxx>4`[1:] {
  682. t.Error("Unexpected result: ", res)
  683. return
  684. }
  685. // Loop over a map
  686. vs = scope.NewScope(scope.GlobalScope)
  687. buf = addLogFunction(vs)
  688. _, err = UnitTestEvalAndAST(
  689. `
  690. x := { "c": 0, "a":2, "b":4}
  691. for [a, b] in x {
  692. testlog("Info", "->", a, "-", b)
  693. }
  694. `, vs,
  695. `
  696. statements
  697. :=
  698. identifier: x
  699. map
  700. kvp
  701. string: 'c'
  702. number: 0
  703. kvp
  704. string: 'a'
  705. number: 2
  706. kvp
  707. string: 'b'
  708. number: 4
  709. loop
  710. in
  711. list
  712. identifier: a
  713. identifier: b
  714. identifier: x
  715. statements
  716. identifier: testlog
  717. funccall
  718. string: 'Info'
  719. string: '->'
  720. identifier: a
  721. string: '-'
  722. identifier: b
  723. `[1:])
  724. if err != nil {
  725. t.Error(err)
  726. return
  727. }
  728. if res := buf.String(); res != `
  729. Info->a-2
  730. Info->b-4
  731. Info->c-0`[1:] {
  732. t.Error("Unexpected result: ", res)
  733. return
  734. }
  735. _, err = UnitTestEval(
  736. `
  737. x := { "c": 0, "a":2, "b":4}
  738. for [1, b] in x {
  739. testlog("Info", "->", a, "-", b)
  740. }
  741. `, vs)
  742. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Must have a list of simple variables on the left side of the In expression) (Line:3 Pos:1)" {
  743. t.Error("Unexpected result:", err)
  744. return
  745. }
  746. // Test continue
  747. _, err = UnitTestEval(`
  748. for [a] in [1,2,3] {
  749. continue
  750. [a, b] := "Hans"
  751. }
  752. `[1:], vs)
  753. if err != nil {
  754. t.Error("Unexpected result:", err)
  755. return
  756. }
  757. _, err = UnitTestEval(`
  758. a := 1
  759. for a < 10 {
  760. a := a + 1
  761. continue
  762. [a,b] := "Hans"
  763. }
  764. `[1:], vs)
  765. if err != nil {
  766. t.Error("Unexpected result:", err)
  767. return
  768. }
  769. // Test single value
  770. _, err = UnitTestEval(`
  771. for a in 1 {
  772. continue
  773. [a,b] := "Hans"
  774. }
  775. `[1:], vs)
  776. if err != nil {
  777. t.Error("Unexpected result:", err)
  778. return
  779. }
  780. _, err = UnitTestEval(`
  781. for a[t] in 1 {
  782. continue
  783. [a,b] := "Hans"
  784. }
  785. `[1:], vs)
  786. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Must have a simple variable on the left side of the In expression) (Line:1 Pos:1)" {
  787. t.Error("Unexpected result:", err)
  788. return
  789. }
  790. _, err = UnitTestEval(`
  791. for [a, b] in [[1,2],[3,4],3] {
  792. }
  793. `[1:], vs)
  794. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Runtime error (Result for loop variable is not a list (value is 3)) (Line:1 Pos:1)" {
  795. t.Error("Unexpected result:", err)
  796. return
  797. }
  798. _, err = UnitTestEval(`
  799. for [a, b] in [[1,2],[3,4],[5,6,7]] {
  800. }
  801. `[1:], vs)
  802. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Runtime error (Assigned number of variables is different to number of values (2 variables vs 3 values)) (Line:1 Pos:1)" {
  803. t.Error("Unexpected result:", err)
  804. return
  805. }
  806. }
  807. func TestTryStatements(t *testing.T) {
  808. vs := scope.NewScope(scope.GlobalScope)
  809. _, err := UnitTestEvalAndAST(
  810. `
  811. try {
  812. debug("Raising custom error")
  813. raise("test 12", null, [1,2,3])
  814. } except "test 12" as e {
  815. error("Something happened: ", e)
  816. } finally {
  817. log("Cleanup")
  818. }
  819. `, vs,
  820. `
  821. try
  822. statements
  823. identifier: debug
  824. funccall
  825. string: 'Raising custom error'
  826. identifier: raise
  827. funccall
  828. string: 'test 12'
  829. null
  830. list
  831. number: 1
  832. number: 2
  833. number: 3
  834. except
  835. string: 'test 12'
  836. as
  837. identifier: e
  838. statements
  839. identifier: error
  840. funccall
  841. string: 'Something happened: '
  842. identifier: e
  843. finally
  844. statements
  845. identifier: log
  846. funccall
  847. string: 'Cleanup'
  848. `[1:])
  849. if err != nil {
  850. t.Error(err)
  851. return
  852. }
  853. if testlogger.String() != `
  854. debug: Raising custom error
  855. error: Something happened: {
  856. "data": [
  857. 1,
  858. 2,
  859. 3
  860. ],
  861. "detail": "",
  862. "error": "ECAL error in ECALTestRuntime: test 12 () (Line:4 Pos:5)",
  863. "line": 4,
  864. "pos": 5,
  865. "source": "ECALTestRuntime",
  866. "trace": [
  867. "raise(\"test 12\", null, [1, 2, 3]) (ECALEvalTest:4)"
  868. ],
  869. "type": "test 12"
  870. }
  871. Cleanup`[1:] {
  872. t.Error("Unexpected result:", testlogger.String())
  873. return
  874. }
  875. _, err = UnitTestEval(
  876. `
  877. try {
  878. debug("Raising custom error")
  879. raise("test 13", null, [1,2,3])
  880. } except "test 12" as e {
  881. error("Something happened: ", e)
  882. } except e {
  883. error("Something else happened: ", e)
  884. try {
  885. x := 1 + a
  886. } except e {
  887. log("Runtime error: ", e)
  888. }
  889. } finally {
  890. log("Cleanup")
  891. }
  892. `, vs)
  893. if err != nil {
  894. t.Error(err)
  895. return
  896. }
  897. if testlogger.String() != `
  898. debug: Raising custom error
  899. error: Something else happened: {
  900. "data": [
  901. 1,
  902. 2,
  903. 3
  904. ],
  905. "detail": "",
  906. "error": "ECAL error in ECALTestRuntime: test 13 () (Line:4 Pos:5)",
  907. "line": 4,
  908. "pos": 5,
  909. "source": "ECALTestRuntime",
  910. "trace": [
  911. "raise(\"test 13\", null, [1, 2, 3]) (ECALEvalTest:4)"
  912. ],
  913. "type": "test 13"
  914. }
  915. Runtime error: {
  916. "detail": "a=NULL",
  917. "error": "ECAL error in ECALTestRuntime: Operand is not a number (a=NULL) (Line:11 Pos:12)",
  918. "line": 11,
  919. "pos": 12,
  920. "source": "ECALTestRuntime",
  921. "trace": [],
  922. "type": "Operand is not a number"
  923. }
  924. Cleanup`[1:] {
  925. t.Error("Unexpected result:", testlogger.String())
  926. return
  927. }
  928. _, err = UnitTestEval(
  929. `
  930. try {
  931. x := 1 + "a"
  932. } except {
  933. error("This did not work")
  934. }
  935. `, vs)
  936. if err != nil {
  937. t.Error(err)
  938. return
  939. }
  940. if testlogger.String() != `
  941. error: This did not work`[1:] {
  942. t.Error("Unexpected result:", testlogger.String())
  943. return
  944. }
  945. }
  946. func TestMutexStatements(t *testing.T) {
  947. vs := scope.NewScope(scope.GlobalScope)
  948. _, err := UnitTestEvalAndAST(
  949. `
  950. mutex foo {
  951. a := 1
  952. raise("test 12", null, [1,2,3])
  953. }
  954. `, vs,
  955. `
  956. mutex
  957. identifier: foo
  958. statements
  959. :=
  960. identifier: a
  961. number: 1
  962. identifier: raise
  963. funccall
  964. string: 'test 12'
  965. null
  966. list
  967. number: 1
  968. number: 2
  969. number: 3
  970. `[1:])
  971. if err != nil {
  972. t.Error(err)
  973. return
  974. }
  975. }