rt_statements_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. "type": "test 12"
  867. }
  868. Cleanup`[1:] {
  869. t.Error("Unexpected result:", testlogger.String())
  870. return
  871. }
  872. _, err = UnitTestEval(
  873. `
  874. try {
  875. debug("Raising custom error")
  876. raise("test 13", null, [1,2,3])
  877. } except "test 12" as e {
  878. error("Something happened: ", e)
  879. } except e {
  880. error("Something else happened: ", e)
  881. try {
  882. x := 1 + a
  883. } except e {
  884. log("Runtime error: ", e)
  885. }
  886. } finally {
  887. log("Cleanup")
  888. }
  889. `, vs)
  890. if err != nil {
  891. t.Error(err)
  892. return
  893. }
  894. if testlogger.String() != `
  895. debug: Raising custom error
  896. error: Something else happened: {
  897. "data": [
  898. 1,
  899. 2,
  900. 3
  901. ],
  902. "detail": "",
  903. "error": "ECAL error in ECALTestRuntime: test 13 () (Line:4 Pos:5)",
  904. "line": 4,
  905. "pos": 5,
  906. "source": "ECALTestRuntime",
  907. "type": "test 13"
  908. }
  909. Runtime error: {
  910. "detail": "a=NULL",
  911. "error": "ECAL error in ECALTestRuntime: Operand is not a number (a=NULL) (Line:11 Pos:12)",
  912. "line": 11,
  913. "pos": 12,
  914. "source": "ECALTestRuntime",
  915. "type": "Operand is not a number"
  916. }
  917. Cleanup`[1:] {
  918. t.Error("Unexpected result:", testlogger.String())
  919. return
  920. }
  921. _, err = UnitTestEval(
  922. `
  923. try {
  924. x := 1 + "a"
  925. } except {
  926. error("This did not work")
  927. }
  928. `, vs)
  929. if err != nil {
  930. t.Error(err)
  931. return
  932. }
  933. if testlogger.String() != `
  934. error: This did not work`[1:] {
  935. t.Error("Unexpected result:", testlogger.String())
  936. return
  937. }
  938. }