rt_statements_test.go 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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 (ECALEvalTest): 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 (ECALEvalTest): 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 (ECALEvalTest): 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 (ECALEvalTest): 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 (ECALEvalTest): test 12 () (Line:4 Pos:5)",
  870. "line": 4,
  871. "pos": 5,
  872. "source": "ECALTestRuntime (ECALEvalTest)",
  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 (ECALEvalTest): test 13 () (Line:4 Pos:5)",
  914. "line": 4,
  915. "pos": 5,
  916. "source": "ECALTestRuntime (ECALEvalTest)",
  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 (ECALEvalTest): Operand is not a number (a=NULL) (Line:11 Pos:12)",
  925. "line": 11,
  926. "pos": 12,
  927. "source": "ECALTestRuntime (ECALEvalTest)",
  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 (ECALEvalTest): Operand is not a number (a) (Line:4 Pos:12)",
  973. "line": 4,
  974. "pos": 12,
  975. "source": "ECALTestRuntime (ECALEvalTest)",
  976. "trace": [],
  977. "type": "Operand is not a number"
  978. },
  979. "detail": "This did not work",
  980. "error": "ECAL error in ECALTestRuntime (ECALEvalTest): usererror (This did not work) (Line:6 Pos:3)",
  981. "line": 6,
  982. "pos": 3,
  983. "source": "ECALTestRuntime (ECALEvalTest)",
  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. a := 2
  1017. mutex foo {
  1018. a := 1
  1019. raise("test 12", null, [1,2,3])
  1020. }
  1021. `, vs,
  1022. `
  1023. statements
  1024. :=
  1025. identifier: a
  1026. number: 2
  1027. mutex
  1028. identifier: foo
  1029. statements
  1030. :=
  1031. identifier: a
  1032. number: 1
  1033. identifier: raise
  1034. funccall
  1035. string: 'test 12'
  1036. null
  1037. list
  1038. number: 1
  1039. number: 2
  1040. number: 3
  1041. `[1:])
  1042. if err == nil || err.Error() != "ECAL error in ECALTestRuntime (ECALEvalTest): test 12 () (Line:5 Pos:5)" {
  1043. t.Error(err)
  1044. return
  1045. }
  1046. if vs.String() != `GlobalScope {
  1047. a (float64) : 1
  1048. block: mutex (Line:3 Pos:1) {
  1049. }
  1050. }` {
  1051. t.Error("Unexpected variable scope:", vs)
  1052. }
  1053. // Can take mutex twice
  1054. _, err = UnitTestEvalAndAST(
  1055. `
  1056. a := 2
  1057. mutex foo {
  1058. a := 1
  1059. mutex foo {
  1060. a := 3
  1061. }
  1062. }
  1063. `, vs,
  1064. `
  1065. statements
  1066. :=
  1067. identifier: a
  1068. number: 2
  1069. mutex
  1070. identifier: foo
  1071. statements
  1072. :=
  1073. identifier: a
  1074. number: 1
  1075. mutex
  1076. identifier: foo
  1077. statements
  1078. :=
  1079. identifier: a
  1080. number: 3
  1081. `[1:])
  1082. if err != nil {
  1083. t.Error(err)
  1084. return
  1085. }
  1086. if vs.String() != `GlobalScope {
  1087. a (float64) : 3
  1088. block: mutex (Line:3 Pos:1) {
  1089. block: mutex (Line:5 Pos:2) {
  1090. }
  1091. }
  1092. }` {
  1093. t.Error("Unexpected variable scope:", vs)
  1094. }
  1095. }