rt_statements_test.go 17 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. }
  328. func TestLoopStatements2(t *testing.T) {
  329. // Test nested loops
  330. vs := scope.NewScope(scope.GlobalScope)
  331. buf := addLogFunction(vs)
  332. _, err := UnitTestEvalAndAST(
  333. `
  334. for a in range(10, 3, -3) {
  335. for b in range(1, 3, 1) {
  336. testlog("Info", "->", a, b)
  337. }
  338. }
  339. `, vs,
  340. `
  341. loop
  342. in
  343. identifier: a
  344. identifier: range
  345. funccall
  346. number: 10
  347. number: 3
  348. minus
  349. number: 3
  350. statements
  351. loop
  352. in
  353. identifier: b
  354. identifier: range
  355. funccall
  356. number: 1
  357. number: 3
  358. number: 1
  359. statements
  360. identifier: testlog
  361. funccall
  362. string: 'Info'
  363. string: '->'
  364. identifier: a
  365. identifier: b
  366. `[1:])
  367. if err != nil {
  368. t.Error(err)
  369. return
  370. }
  371. if vs.String() != `
  372. GlobalScope {
  373. testlog (*interpreter.TestLogger) : TestLogger
  374. block: loop (Line:2 Pos:1) {
  375. a (float64) : 4
  376. block: loop (Line:3 Pos:3) {
  377. b (float64) : 3
  378. }
  379. }
  380. }`[1:] {
  381. t.Error("Unexpected result: ", vs)
  382. return
  383. }
  384. if res := buf.String(); res != `
  385. Info->10 1
  386. Info->10 2
  387. Info->10 3
  388. Info->7 1
  389. Info->7 2
  390. Info->7 3
  391. Info->4 1
  392. Info->4 2
  393. Info->4 3`[1:] {
  394. t.Error("Unexpected result: ", res)
  395. return
  396. }
  397. // Break statement
  398. vs = scope.NewScope(scope.GlobalScope)
  399. buf = addLogFunction(vs)
  400. _, err = UnitTestEvalAndAST(
  401. `
  402. for a in range(1, 10, 1) {
  403. testlog("Info", "->", a)
  404. if a == 3 {
  405. break
  406. }
  407. }`, vs,
  408. `
  409. loop
  410. in
  411. identifier: a
  412. identifier: range
  413. funccall
  414. number: 1
  415. number: 10
  416. number: 1
  417. statements
  418. identifier: testlog
  419. funccall
  420. string: 'Info'
  421. string: '->'
  422. identifier: a
  423. if
  424. guard
  425. ==
  426. identifier: a
  427. number: 3
  428. statements
  429. break
  430. `[1:])
  431. if err != nil {
  432. t.Error(err)
  433. return
  434. }
  435. if vs.String() != `
  436. GlobalScope {
  437. testlog (*interpreter.TestLogger) : TestLogger
  438. block: loop (Line:2 Pos:1) {
  439. a (float64) : 3
  440. block: if (Line:4 Pos:3) {
  441. }
  442. }
  443. }`[1:] {
  444. t.Error("Unexpected result: ", vs)
  445. return
  446. }
  447. if res := buf.String(); res != `
  448. Info->1
  449. Info->2
  450. Info->3`[1:] {
  451. t.Error("Unexpected result: ", res)
  452. return
  453. }
  454. // Continue statement
  455. vs = scope.NewScope(scope.GlobalScope)
  456. buf = addLogFunction(vs)
  457. _, err = UnitTestEvalAndAST(
  458. `
  459. for a in range(1, 10, 1) {
  460. if a > 3 and a < 6 {
  461. continue
  462. }
  463. testlog("Info", "->", a)
  464. }`, vs,
  465. `
  466. loop
  467. in
  468. identifier: a
  469. identifier: range
  470. funccall
  471. number: 1
  472. number: 10
  473. number: 1
  474. statements
  475. if
  476. guard
  477. and
  478. >
  479. identifier: a
  480. number: 3
  481. <
  482. identifier: a
  483. number: 6
  484. statements
  485. continue
  486. identifier: testlog
  487. funccall
  488. string: 'Info'
  489. string: '->'
  490. identifier: a
  491. `[1:])
  492. if err != nil {
  493. t.Error(err)
  494. return
  495. }
  496. if vs.String() != `
  497. GlobalScope {
  498. testlog (*interpreter.TestLogger) : TestLogger
  499. block: loop (Line:2 Pos:1) {
  500. a (float64) : 10
  501. block: if (Line:3 Pos:3) {
  502. }
  503. }
  504. }`[1:] {
  505. t.Error("Unexpected result: ", vs)
  506. return
  507. }
  508. if res := buf.String(); res != `
  509. Info->1
  510. Info->2
  511. Info->3
  512. Info->6
  513. Info->7
  514. Info->8
  515. Info->9
  516. Info->10`[1:] {
  517. t.Error("Unexpected result: ", res)
  518. return
  519. }
  520. }
  521. func TestLoopStatements3(t *testing.T) {
  522. // Loop over lists
  523. vs := scope.NewScope(scope.GlobalScope)
  524. buf := addLogFunction(vs)
  525. _, err := UnitTestEvalAndAST(
  526. `
  527. for a in [1,2] {
  528. for b in [1,2,3,"Hans", 4] {
  529. testlog("Info", "->", a, "-", b)
  530. }
  531. }
  532. `, vs,
  533. `
  534. loop
  535. in
  536. identifier: a
  537. list
  538. number: 1
  539. number: 2
  540. statements
  541. loop
  542. in
  543. identifier: b
  544. list
  545. number: 1
  546. number: 2
  547. number: 3
  548. string: 'Hans'
  549. number: 4
  550. statements
  551. identifier: testlog
  552. funccall
  553. string: 'Info'
  554. string: '->'
  555. identifier: a
  556. string: '-'
  557. identifier: b
  558. `[1:])
  559. if err != nil {
  560. t.Error(err)
  561. return
  562. }
  563. if vs.String() != `
  564. GlobalScope {
  565. testlog (*interpreter.TestLogger) : TestLogger
  566. block: loop (Line:2 Pos:1) {
  567. a (float64) : 2
  568. block: loop (Line:3 Pos:3) {
  569. b (float64) : 4
  570. }
  571. }
  572. }`[1:] {
  573. t.Error("Unexpected result: ", vs)
  574. return
  575. }
  576. if res := buf.String(); res != `
  577. Info->1-1
  578. Info->1-2
  579. Info->1-3
  580. Info->1-Hans
  581. Info->1-4
  582. Info->2-1
  583. Info->2-2
  584. Info->2-3
  585. Info->2-Hans
  586. Info->2-4`[1:] {
  587. t.Error("Unexpected result: ", res)
  588. return
  589. }
  590. vs = scope.NewScope(scope.GlobalScope)
  591. buf = addLogFunction(vs)
  592. _, err = UnitTestEvalAndAST(
  593. `
  594. l := [1,2,3,4]
  595. for a in range(0, 3, 1) {
  596. testlog("Info", "-a>", a, "-", l[a])
  597. }
  598. for a in range(0, 3, 1) {
  599. testlog("Info", "-b>", a, "-", l[-a])
  600. }
  601. testlog("Info", "xxx>", l[-1])
  602. `, vs,
  603. `
  604. statements
  605. :=
  606. identifier: l
  607. list
  608. number: 1
  609. number: 2
  610. number: 3
  611. number: 4
  612. loop
  613. in
  614. identifier: a
  615. identifier: range
  616. funccall
  617. number: 0
  618. number: 3
  619. number: 1
  620. statements
  621. identifier: testlog
  622. funccall
  623. string: 'Info'
  624. string: '-a>'
  625. identifier: a
  626. string: '-'
  627. identifier: l
  628. compaccess
  629. identifier: a
  630. loop
  631. in
  632. identifier: a
  633. identifier: range
  634. funccall
  635. number: 0
  636. number: 3
  637. number: 1
  638. statements
  639. identifier: testlog
  640. funccall
  641. string: 'Info'
  642. string: '-b>'
  643. identifier: a
  644. string: '-'
  645. identifier: l
  646. compaccess
  647. minus
  648. identifier: a
  649. identifier: testlog
  650. funccall
  651. string: 'Info'
  652. string: 'xxx>'
  653. identifier: l
  654. compaccess
  655. minus
  656. number: 1
  657. `[1:])
  658. if err != nil {
  659. t.Error(err)
  660. return
  661. }
  662. if vs.String() != `
  663. GlobalScope {
  664. l ([]interface {}) : [1,2,3,4]
  665. testlog (*interpreter.TestLogger) : TestLogger
  666. block: loop (Line:3 Pos:1) {
  667. a (float64) : 3
  668. }
  669. block: loop (Line:6 Pos:1) {
  670. a (float64) : 3
  671. }
  672. }`[1:] {
  673. t.Error("Unexpected result: ", vs)
  674. return
  675. }
  676. if res := buf.String(); res != `
  677. Info-a>0-1
  678. Info-a>1-2
  679. Info-a>2-3
  680. Info-a>3-4
  681. Info-b>0-1
  682. Info-b>1-4
  683. Info-b>2-3
  684. Info-b>3-2
  685. Infoxxx>4`[1:] {
  686. t.Error("Unexpected result: ", res)
  687. return
  688. }
  689. // Loop over a map
  690. vs = scope.NewScope(scope.GlobalScope)
  691. buf = addLogFunction(vs)
  692. _, err = UnitTestEvalAndAST(
  693. `
  694. x := { "c": 0, "a":2, "b":4}
  695. for [a, b] in x {
  696. testlog("Info", "->", a, "-", b)
  697. }
  698. `, vs,
  699. `
  700. statements
  701. :=
  702. identifier: x
  703. map
  704. kvp
  705. string: 'c'
  706. number: 0
  707. kvp
  708. string: 'a'
  709. number: 2
  710. kvp
  711. string: 'b'
  712. number: 4
  713. loop
  714. in
  715. list
  716. identifier: a
  717. identifier: b
  718. identifier: x
  719. statements
  720. identifier: testlog
  721. funccall
  722. string: 'Info'
  723. string: '->'
  724. identifier: a
  725. string: '-'
  726. identifier: b
  727. `[1:])
  728. if err != nil {
  729. t.Error(err)
  730. return
  731. }
  732. if res := buf.String(); res != `
  733. Info->a-2
  734. Info->b-4
  735. Info->c-0`[1:] {
  736. t.Error("Unexpected result: ", res)
  737. return
  738. }
  739. _, err = UnitTestEval(
  740. `
  741. x := { "c": 0, "a":2, "b":4}
  742. for [1, b] in x {
  743. testlog("Info", "->", a, "-", b)
  744. }
  745. `, vs)
  746. 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)" {
  747. t.Error("Unexpected result:", err)
  748. return
  749. }
  750. }
  751. func TestLoopStatements4(t *testing.T) {
  752. vs := scope.NewScope(scope.GlobalScope)
  753. // Test continue
  754. _, err := UnitTestEval(`
  755. for [a] in [1,2,3] {
  756. continue
  757. [a, b] := "Hans"
  758. }
  759. `[1:], vs)
  760. if err != nil {
  761. t.Error("Unexpected result:", err)
  762. return
  763. }
  764. _, err = UnitTestEval(`
  765. a := 1
  766. for a < 10 {
  767. a := a + 1
  768. continue
  769. [a,b] := "Hans"
  770. }
  771. `[1:], vs)
  772. if err != nil {
  773. t.Error("Unexpected result:", err)
  774. return
  775. }
  776. // Test single value
  777. _, err = UnitTestEval(`
  778. for a in 1 {
  779. continue
  780. [a,b] := "Hans"
  781. }
  782. `[1:], vs)
  783. if err != nil {
  784. t.Error("Unexpected result:", err)
  785. return
  786. }
  787. _, err = UnitTestEval(`
  788. for a[t] in 1 {
  789. continue
  790. [a,b] := "Hans"
  791. }
  792. `[1:], vs)
  793. 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)" {
  794. t.Error("Unexpected result:", err)
  795. return
  796. }
  797. _, err = UnitTestEval(`
  798. for [a, b] in [[1,2],[3,4],3] {
  799. }
  800. `[1:], vs)
  801. 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)" {
  802. t.Error("Unexpected result:", err)
  803. return
  804. }
  805. _, err = UnitTestEval(`
  806. for [a, b] in [[1,2],[3,4],[5,6,7]] {
  807. }
  808. `[1:], vs)
  809. 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)" {
  810. t.Error("Unexpected result:", err)
  811. return
  812. }
  813. }
  814. func TestTryStatements(t *testing.T) {
  815. vs := scope.NewScope(scope.GlobalScope)
  816. _, err := UnitTestEvalAndAST(
  817. `
  818. try {
  819. debug("Raising custom error")
  820. raise("test 12", null, [1,2,3])
  821. } except "test 12" as e {
  822. error("Something happened: ", e)
  823. } finally {
  824. log("Cleanup")
  825. }
  826. `, vs,
  827. `
  828. try
  829. statements
  830. identifier: debug
  831. funccall
  832. string: 'Raising custom error'
  833. identifier: raise
  834. funccall
  835. string: 'test 12'
  836. null
  837. list
  838. number: 1
  839. number: 2
  840. number: 3
  841. except
  842. string: 'test 12'
  843. as
  844. identifier: e
  845. statements
  846. identifier: error
  847. funccall
  848. string: 'Something happened: '
  849. identifier: e
  850. finally
  851. statements
  852. identifier: log
  853. funccall
  854. string: 'Cleanup'
  855. `[1:])
  856. if err != nil {
  857. t.Error(err)
  858. return
  859. }
  860. if testlogger.String() != `
  861. debug: Raising custom error
  862. error: Something happened: {
  863. "data": [
  864. 1,
  865. 2,
  866. 3
  867. ],
  868. "detail": "",
  869. "error": "ECAL error in ECALTestRuntime: test 12 () (Line:4 Pos:5)",
  870. "line": 4,
  871. "pos": 5,
  872. "source": "ECALTestRuntime",
  873. "trace": [
  874. "raise(\"test 12\", null, [1, 2, 3]) (ECALEvalTest:4)"
  875. ],
  876. "type": "test 12"
  877. }
  878. Cleanup`[1:] {
  879. t.Error("Unexpected result:", testlogger.String())
  880. return
  881. }
  882. _, err = UnitTestEval(
  883. `
  884. try {
  885. debug("Raising custom error")
  886. raise("test 13", null, [1,2,3])
  887. } except "test 12" as e {
  888. error("Something happened: ", e)
  889. } except e {
  890. error("Something else happened: ", e)
  891. try {
  892. x := 1 + a
  893. } except e {
  894. log("Runtime error: ", e)
  895. }
  896. } finally {
  897. log("Cleanup")
  898. }
  899. `, vs)
  900. if err != nil {
  901. t.Error(err)
  902. return
  903. }
  904. if testlogger.String() != `
  905. debug: Raising custom error
  906. error: Something else happened: {
  907. "data": [
  908. 1,
  909. 2,
  910. 3
  911. ],
  912. "detail": "",
  913. "error": "ECAL error in ECALTestRuntime: test 13 () (Line:4 Pos:5)",
  914. "line": 4,
  915. "pos": 5,
  916. "source": "ECALTestRuntime",
  917. "trace": [
  918. "raise(\"test 13\", null, [1, 2, 3]) (ECALEvalTest:4)"
  919. ],
  920. "type": "test 13"
  921. }
  922. Runtime error: {
  923. "detail": "a=NULL",
  924. "error": "ECAL error in ECALTestRuntime: Operand is not a number (a=NULL) (Line:11 Pos:12)",
  925. "line": 11,
  926. "pos": 12,
  927. "source": "ECALTestRuntime",
  928. "trace": [],
  929. "type": "Operand is not a number"
  930. }
  931. Cleanup`[1:] {
  932. t.Error("Unexpected result:", testlogger.String())
  933. return
  934. }
  935. _, err = UnitTestEval(
  936. `
  937. try {
  938. x := 1 + "a"
  939. } except {
  940. error("This did not work")
  941. }
  942. `, vs)
  943. if err != nil {
  944. t.Error(err)
  945. return
  946. }
  947. if testlogger.String() != `
  948. error: This did not work`[1:] {
  949. t.Error("Unexpected result:", testlogger.String())
  950. return
  951. }
  952. }
  953. func TestMutexStatements(t *testing.T) {
  954. vs := scope.NewScope(scope.GlobalScope)
  955. _, err := UnitTestEvalAndAST(
  956. `
  957. mutex foo {
  958. a := 1
  959. raise("test 12", null, [1,2,3])
  960. }
  961. `, vs,
  962. `
  963. mutex
  964. identifier: foo
  965. statements
  966. :=
  967. identifier: a
  968. number: 1
  969. identifier: raise
  970. funccall
  971. string: 'test 12'
  972. null
  973. list
  974. number: 1
  975. number: 2
  976. number: 3
  977. `[1:])
  978. if err != nil {
  979. t.Error(err)
  980. return
  981. }
  982. }