error_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 util
  11. import (
  12. "encoding/json"
  13. "fmt"
  14. "strings"
  15. "sync"
  16. "testing"
  17. "devt.de/krotik/ecal/parser"
  18. "devt.de/krotik/ecal/scope"
  19. )
  20. func TestRuntimeError(t *testing.T) {
  21. ast, _ := parser.Parse("foo", "a")
  22. err1 := NewRuntimeError("foo", fmt.Errorf("foo"), "bar", ast)
  23. if err1.Error() != "ECAL error in foo: foo (bar) (Line:1 Pos:1)" {
  24. t.Error("Unexpected result:", err1)
  25. return
  26. }
  27. ast.Token = nil
  28. err2 := NewRuntimeError("foo", fmt.Errorf("foo"), "bar", ast)
  29. if err2.Error() != "ECAL error in foo: foo (bar)" {
  30. t.Error("Unexpected result:", err2)
  31. return
  32. }
  33. ast, _ = parser.Parse("foo", "a:=1")
  34. err3 := NewRuntimeError("foo", fmt.Errorf("foo"), "bar", ast)
  35. ast, _ = parser.Parse("bar1", "print(b)")
  36. err3.(TraceableRuntimeError).AddTrace(ast)
  37. ast, _ = parser.Parse("bar2", "raise(c)")
  38. err3.(TraceableRuntimeError).AddTrace(ast)
  39. ast, _ = parser.Parse("bar3", "1 + d")
  40. err3.(TraceableRuntimeError).AddTrace(ast)
  41. trace := strings.Join(err3.(TraceableRuntimeError).GetTraceString(), "\n")
  42. if trace != `print(b) (bar1:1)
  43. raise(c) (bar2:1)
  44. 1 + d (bar3:1)` {
  45. t.Error("Unexpected result:", trace)
  46. return
  47. }
  48. err4 := &RuntimeErrorWithDetail{err3.(*RuntimeError), nil, nil}
  49. res, _ := json.MarshalIndent(err4.RuntimeError, "", " ")
  50. if string(res) != `{
  51. "Detail": "bar",
  52. "Node": {
  53. "Name": ":=",
  54. "Token": {
  55. "ID": 39,
  56. "Pos": 1,
  57. "Val": ":=",
  58. "Identifier": false,
  59. "AllowEscapes": false,
  60. "PrefixNewlines": 0,
  61. "Lsource": "foo",
  62. "Lline": 1,
  63. "Lpos": 2
  64. },
  65. "Meta": null,
  66. "Children": [
  67. {
  68. "Name": "identifier",
  69. "Token": {
  70. "ID": 7,
  71. "Pos": 0,
  72. "Val": "a",
  73. "Identifier": true,
  74. "AllowEscapes": false,
  75. "PrefixNewlines": 0,
  76. "Lsource": "foo",
  77. "Lline": 1,
  78. "Lpos": 1
  79. },
  80. "Meta": null,
  81. "Children": [],
  82. "Runtime": null
  83. },
  84. {
  85. "Name": "number",
  86. "Token": {
  87. "ID": 6,
  88. "Pos": 3,
  89. "Val": "1",
  90. "Identifier": false,
  91. "AllowEscapes": false,
  92. "PrefixNewlines": 0,
  93. "Lsource": "foo",
  94. "Lline": 1,
  95. "Lpos": 4
  96. },
  97. "Meta": null,
  98. "Children": [],
  99. "Runtime": null
  100. }
  101. ],
  102. "Runtime": null
  103. },
  104. "Source": "foo",
  105. "Trace": [
  106. {
  107. "Name": "identifier",
  108. "Token": {
  109. "ID": 7,
  110. "Pos": 0,
  111. "Val": "print",
  112. "Identifier": true,
  113. "AllowEscapes": false,
  114. "PrefixNewlines": 0,
  115. "Lsource": "bar1",
  116. "Lline": 1,
  117. "Lpos": 1
  118. },
  119. "Meta": null,
  120. "Children": [
  121. {
  122. "Name": "funccall",
  123. "Token": null,
  124. "Meta": null,
  125. "Children": [
  126. {
  127. "Name": "identifier",
  128. "Token": {
  129. "ID": 7,
  130. "Pos": 6,
  131. "Val": "b",
  132. "Identifier": true,
  133. "AllowEscapes": false,
  134. "PrefixNewlines": 0,
  135. "Lsource": "bar1",
  136. "Lline": 1,
  137. "Lpos": 7
  138. },
  139. "Meta": null,
  140. "Children": [],
  141. "Runtime": null
  142. }
  143. ],
  144. "Runtime": null
  145. }
  146. ],
  147. "Runtime": null
  148. },
  149. {
  150. "Name": "identifier",
  151. "Token": {
  152. "ID": 7,
  153. "Pos": 0,
  154. "Val": "raise",
  155. "Identifier": true,
  156. "AllowEscapes": false,
  157. "PrefixNewlines": 0,
  158. "Lsource": "bar2",
  159. "Lline": 1,
  160. "Lpos": 1
  161. },
  162. "Meta": null,
  163. "Children": [
  164. {
  165. "Name": "funccall",
  166. "Token": null,
  167. "Meta": null,
  168. "Children": [
  169. {
  170. "Name": "identifier",
  171. "Token": {
  172. "ID": 7,
  173. "Pos": 6,
  174. "Val": "c",
  175. "Identifier": true,
  176. "AllowEscapes": false,
  177. "PrefixNewlines": 0,
  178. "Lsource": "bar2",
  179. "Lline": 1,
  180. "Lpos": 7
  181. },
  182. "Meta": null,
  183. "Children": [],
  184. "Runtime": null
  185. }
  186. ],
  187. "Runtime": null
  188. }
  189. ],
  190. "Runtime": null
  191. },
  192. {
  193. "Name": "plus",
  194. "Token": {
  195. "ID": 33,
  196. "Pos": 2,
  197. "Val": "+",
  198. "Identifier": false,
  199. "AllowEscapes": false,
  200. "PrefixNewlines": 0,
  201. "Lsource": "bar3",
  202. "Lline": 1,
  203. "Lpos": 3
  204. },
  205. "Meta": null,
  206. "Children": [
  207. {
  208. "Name": "number",
  209. "Token": {
  210. "ID": 6,
  211. "Pos": 0,
  212. "Val": "1",
  213. "Identifier": false,
  214. "AllowEscapes": false,
  215. "PrefixNewlines": 0,
  216. "Lsource": "bar3",
  217. "Lline": 1,
  218. "Lpos": 1
  219. },
  220. "Meta": null,
  221. "Children": [],
  222. "Runtime": null
  223. },
  224. {
  225. "Name": "identifier",
  226. "Token": {
  227. "ID": 7,
  228. "Pos": 4,
  229. "Val": "d",
  230. "Identifier": true,
  231. "AllowEscapes": false,
  232. "PrefixNewlines": 0,
  233. "Lsource": "bar3",
  234. "Lline": 1,
  235. "Lpos": 5
  236. },
  237. "Meta": null,
  238. "Children": [],
  239. "Runtime": null
  240. }
  241. ],
  242. "Runtime": null
  243. }
  244. ],
  245. "Type": "foo"
  246. }` {
  247. t.Error("Unexpected result:", string(res))
  248. return
  249. }
  250. s := scope.NewScope("aa")
  251. s.SetValue("xx", 123)
  252. err4 = &RuntimeErrorWithDetail{err3.(*RuntimeError), s, sync.Mutex{}}
  253. res, _ = json.MarshalIndent(err4, "", " ")
  254. if string(res) != `{
  255. "Data": {},
  256. "Detail": "bar",
  257. "Environment": {
  258. "xx": 123
  259. },
  260. "Node": {
  261. "Name": ":=",
  262. "Token": {
  263. "ID": 39,
  264. "Pos": 1,
  265. "Val": ":=",
  266. "Identifier": false,
  267. "AllowEscapes": false,
  268. "PrefixNewlines": 0,
  269. "Lsource": "foo",
  270. "Lline": 1,
  271. "Lpos": 2
  272. },
  273. "Meta": null,
  274. "Children": [
  275. {
  276. "Name": "identifier",
  277. "Token": {
  278. "ID": 7,
  279. "Pos": 0,
  280. "Val": "a",
  281. "Identifier": true,
  282. "AllowEscapes": false,
  283. "PrefixNewlines": 0,
  284. "Lsource": "foo",
  285. "Lline": 1,
  286. "Lpos": 1
  287. },
  288. "Meta": null,
  289. "Children": [],
  290. "Runtime": null
  291. },
  292. {
  293. "Name": "number",
  294. "Token": {
  295. "ID": 6,
  296. "Pos": 3,
  297. "Val": "1",
  298. "Identifier": false,
  299. "AllowEscapes": false,
  300. "PrefixNewlines": 0,
  301. "Lsource": "foo",
  302. "Lline": 1,
  303. "Lpos": 4
  304. },
  305. "Meta": null,
  306. "Children": [],
  307. "Runtime": null
  308. }
  309. ],
  310. "Runtime": null
  311. },
  312. "Source": "foo",
  313. "Trace": [
  314. {
  315. "Name": "identifier",
  316. "Token": {
  317. "ID": 7,
  318. "Pos": 0,
  319. "Val": "print",
  320. "Identifier": true,
  321. "AllowEscapes": false,
  322. "PrefixNewlines": 0,
  323. "Lsource": "bar1",
  324. "Lline": 1,
  325. "Lpos": 1
  326. },
  327. "Meta": null,
  328. "Children": [
  329. {
  330. "Name": "funccall",
  331. "Token": null,
  332. "Meta": null,
  333. "Children": [
  334. {
  335. "Name": "identifier",
  336. "Token": {
  337. "ID": 7,
  338. "Pos": 6,
  339. "Val": "b",
  340. "Identifier": true,
  341. "AllowEscapes": false,
  342. "PrefixNewlines": 0,
  343. "Lsource": "bar1",
  344. "Lline": 1,
  345. "Lpos": 7
  346. },
  347. "Meta": null,
  348. "Children": [],
  349. "Runtime": null
  350. }
  351. ],
  352. "Runtime": null
  353. }
  354. ],
  355. "Runtime": null
  356. },
  357. {
  358. "Name": "identifier",
  359. "Token": {
  360. "ID": 7,
  361. "Pos": 0,
  362. "Val": "raise",
  363. "Identifier": true,
  364. "AllowEscapes": false,
  365. "PrefixNewlines": 0,
  366. "Lsource": "bar2",
  367. "Lline": 1,
  368. "Lpos": 1
  369. },
  370. "Meta": null,
  371. "Children": [
  372. {
  373. "Name": "funccall",
  374. "Token": null,
  375. "Meta": null,
  376. "Children": [
  377. {
  378. "Name": "identifier",
  379. "Token": {
  380. "ID": 7,
  381. "Pos": 6,
  382. "Val": "c",
  383. "Identifier": true,
  384. "AllowEscapes": false,
  385. "PrefixNewlines": 0,
  386. "Lsource": "bar2",
  387. "Lline": 1,
  388. "Lpos": 7
  389. },
  390. "Meta": null,
  391. "Children": [],
  392. "Runtime": null
  393. }
  394. ],
  395. "Runtime": null
  396. }
  397. ],
  398. "Runtime": null
  399. },
  400. {
  401. "Name": "plus",
  402. "Token": {
  403. "ID": 33,
  404. "Pos": 2,
  405. "Val": "+",
  406. "Identifier": false,
  407. "AllowEscapes": false,
  408. "PrefixNewlines": 0,
  409. "Lsource": "bar3",
  410. "Lline": 1,
  411. "Lpos": 3
  412. },
  413. "Meta": null,
  414. "Children": [
  415. {
  416. "Name": "number",
  417. "Token": {
  418. "ID": 6,
  419. "Pos": 0,
  420. "Val": "1",
  421. "Identifier": false,
  422. "AllowEscapes": false,
  423. "PrefixNewlines": 0,
  424. "Lsource": "bar3",
  425. "Lline": 1,
  426. "Lpos": 1
  427. },
  428. "Meta": null,
  429. "Children": [],
  430. "Runtime": null
  431. },
  432. {
  433. "Name": "identifier",
  434. "Token": {
  435. "ID": 7,
  436. "Pos": 4,
  437. "Val": "d",
  438. "Identifier": true,
  439. "AllowEscapes": false,
  440. "PrefixNewlines": 0,
  441. "Lsource": "bar3",
  442. "Lline": 1,
  443. "Lpos": 5
  444. },
  445. "Meta": null,
  446. "Children": [],
  447. "Runtime": null
  448. }
  449. ],
  450. "Runtime": null
  451. }
  452. ],
  453. "Type": "foo"
  454. }` {
  455. t.Error("Unexpected result:", string(res))
  456. return
  457. }
  458. }