rt_statements_test.go 17 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. }