rt_statements_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. _, err = UnitTestEval(
  953. `
  954. try {
  955. try {
  956. x := 1 + "a"
  957. } except e {
  958. raise("usererror", "This did not work", e)
  959. }
  960. } except e {
  961. error(e)
  962. }
  963. `, vs)
  964. if err != nil {
  965. t.Error(err)
  966. return
  967. }
  968. if testlogger.String() != `
  969. error: {
  970. "data": {
  971. "detail": "a",
  972. "error": "ECAL error in ECALTestRuntime: Operand is not a number (a) (Line:4 Pos:12)",
  973. "line": 4,
  974. "pos": 12,
  975. "source": "ECALTestRuntime",
  976. "trace": [],
  977. "type": "Operand is not a number"
  978. },
  979. "detail": "This did not work",
  980. "error": "ECAL error in ECALTestRuntime: usererror (This did not work) (Line:6 Pos:3)",
  981. "line": 6,
  982. "pos": 3,
  983. "source": "ECALTestRuntime",
  984. "trace": [
  985. "raise(\"usererror\", \"This did not work\", e) (ECALEvalTest:6)"
  986. ],
  987. "type": "usererror"
  988. }`[1:] {
  989. t.Error("Unexpected result:", testlogger.String())
  990. return
  991. }
  992. _, err = UnitTestEval(
  993. `
  994. try {
  995. x := 1
  996. } except e {
  997. raise("usererror", "This did not work", e)
  998. } otherwise {
  999. log("all good")
  1000. }
  1001. `, vs)
  1002. if err != nil {
  1003. t.Error(err)
  1004. return
  1005. }
  1006. if testlogger.String() != `
  1007. all good`[1:] {
  1008. t.Error("Unexpected result:", testlogger.String())
  1009. return
  1010. }
  1011. }
  1012. func TestMutexStatements(t *testing.T) {
  1013. vs := scope.NewScope(scope.GlobalScope)
  1014. _, err := UnitTestEvalAndAST(
  1015. `
  1016. mutex foo {
  1017. a := 1
  1018. raise("test 12", null, [1,2,3])
  1019. }
  1020. `, vs,
  1021. `
  1022. mutex
  1023. identifier: foo
  1024. statements
  1025. :=
  1026. identifier: a
  1027. number: 1
  1028. identifier: raise
  1029. funccall
  1030. string: 'test 12'
  1031. null
  1032. list
  1033. number: 1
  1034. number: 2
  1035. number: 3
  1036. `[1:])
  1037. if err != nil {
  1038. t.Error(err)
  1039. return
  1040. }
  1041. }