prettyprinter_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 parser
  11. import (
  12. "testing"
  13. )
  14. func TestErrorHandling(t *testing.T) {
  15. input := "c:= a + b"
  16. astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
  17. if err != nil {
  18. t.Errorf("Unexpected parser output:\n%vError: %v", astres, err)
  19. return
  20. }
  21. // Make ast invalid
  22. astres.Children[1].Children[1] = nil
  23. ppres, err := PrettyPrint(astres)
  24. if err == nil || err.Error() != "Nil pointer in AST" {
  25. t.Errorf("Unexpected result: %v error: %v", ppres, err)
  26. return
  27. }
  28. }
  29. func TestArithmeticExpressionPrinting(t *testing.T) {
  30. input := "a + b * 5 /2-1"
  31. expectedOutput := `
  32. minus
  33. plus
  34. identifier: a
  35. div
  36. times
  37. identifier: b
  38. number: 5
  39. number: 2
  40. number: 1
  41. `[1:]
  42. if err := UnitTestPrettyPrinting(input, expectedOutput,
  43. "a + b * 5 / 2 - 1"); err != nil {
  44. t.Error(err)
  45. return
  46. }
  47. input = `-a + "\"'b"`
  48. expectedOutput = `
  49. plus
  50. minus
  51. identifier: a
  52. string: '"'b'
  53. `[1:]
  54. if err := UnitTestPrettyPrinting(input, expectedOutput,
  55. `-a + "\"'b"`); err != nil {
  56. t.Error(err)
  57. return
  58. }
  59. input = `a // 5 % (50 + 1)`
  60. expectedOutput = `
  61. modint
  62. divint
  63. identifier: a
  64. number: 5
  65. plus
  66. number: 50
  67. number: 1
  68. `[1:]
  69. if err := UnitTestPrettyPrinting(input, expectedOutput,
  70. `a // 5 % (50 + 1)`); err != nil {
  71. t.Error(err)
  72. return
  73. }
  74. input = "(a + 1) * 5 / (6 - 2)"
  75. expectedOutput = `
  76. div
  77. times
  78. plus
  79. identifier: a
  80. number: 1
  81. number: 5
  82. minus
  83. number: 6
  84. number: 2
  85. `[1:]
  86. if err := UnitTestPrettyPrinting(input, expectedOutput,
  87. "(a + 1) * 5 / (6 - 2)"); err != nil {
  88. t.Error(err)
  89. return
  90. }
  91. input = "a + (1 * 5) / 6 - 2"
  92. expectedOutput = `
  93. minus
  94. plus
  95. identifier: a
  96. div
  97. times
  98. number: 1
  99. number: 5
  100. number: 6
  101. number: 2
  102. `[1:]
  103. if err := UnitTestPrettyPrinting(input, expectedOutput,
  104. "a + 1 * 5 / 6 - 2"); err != nil {
  105. t.Error(err)
  106. return
  107. }
  108. }
  109. func TestLogicalExpressionPrinting(t *testing.T) {
  110. input := "not (a + 1) * 5 and tRue or not 1 - 5 != '!test'"
  111. expectedOutput := `
  112. or
  113. and
  114. not
  115. times
  116. plus
  117. identifier: a
  118. number: 1
  119. number: 5
  120. true
  121. not
  122. !=
  123. minus
  124. number: 1
  125. number: 5
  126. string: '!test'
  127. `[1:]
  128. if err := UnitTestPrettyPrinting(input, expectedOutput,
  129. "not (a + 1) * 5 and true or not 1 - 5 != \"!test\""); err != nil {
  130. t.Error(err)
  131. return
  132. }
  133. input = "not x < null and a > b or 1 <= c and 2 >= false or c == true"
  134. expectedOutput = `
  135. or
  136. or
  137. and
  138. not
  139. <
  140. identifier: x
  141. null
  142. >
  143. identifier: a
  144. identifier: b
  145. and
  146. <=
  147. number: 1
  148. identifier: c
  149. >=
  150. number: 2
  151. false
  152. ==
  153. identifier: c
  154. true
  155. `[1:]
  156. if err := UnitTestPrettyPrinting(input, expectedOutput,
  157. "not x < null and a > b or 1 <= c and 2 >= false or c == true"); err != nil {
  158. t.Error(err)
  159. return
  160. }
  161. input = "a hasPrefix 'a' and b hassuffix 'c' or d like '^.*' and 3 notin x"
  162. expectedOutput = `
  163. or
  164. and
  165. hasprefix
  166. identifier: a
  167. string: 'a'
  168. hassuffix
  169. identifier: b
  170. string: 'c'
  171. and
  172. like
  173. identifier: d
  174. string: '^.*'
  175. notin
  176. number: 3
  177. identifier: x
  178. `[1:]
  179. if err := UnitTestPrettyPrinting(input, expectedOutput,
  180. `a hasprefix "a" and b hassuffix "c" or d like "^.*" and 3 notin x`); err != nil {
  181. t.Error(err)
  182. return
  183. }
  184. }
  185. func TestSpecialCasePrinting1(t *testing.T) {
  186. input := `a := {"a":1,"b":1,"c":1,"d" : 1, "e":1,"f":1,"g":1,"h":1,}`
  187. if err := UnitTestPrettyPrinting(input, "",
  188. `a := {
  189. "a" : 1,
  190. "b" : 1,
  191. "c" : 1,
  192. "d" : 1,
  193. "e" : 1,
  194. "f" : 1,
  195. "g" : 1,
  196. "h" : 1
  197. }`); err != nil {
  198. t.Error(err)
  199. return
  200. }
  201. input = `a := {"a":1,"b":1,"c":1,"d" : {"a":1,"b":{"a":1,"b":1,"c":1,"d":1},"c":1,"d" : 1, "e":1,"f":{"a":1,"b":1},"g":1,"h":1,}, "e":1,"f":1,"g":1,"h":1,}`
  202. if err := UnitTestPrettyPrinting(input, "",
  203. `a := {
  204. "a" : 1,
  205. "b" : 1,
  206. "c" : 1,
  207. "d" : {
  208. "a" : 1,
  209. "b" : {
  210. "a" : 1,
  211. "b" : 1,
  212. "c" : 1,
  213. "d" : 1
  214. },
  215. "c" : 1,
  216. "d" : 1,
  217. "e" : 1,
  218. "f" : {"a" : 1, "b" : 1},
  219. "g" : 1,
  220. "h" : 1
  221. },
  222. "e" : 1,
  223. "f" : 1,
  224. "g" : 1,
  225. "h" : 1
  226. }`); err != nil {
  227. t.Error(err)
  228. return
  229. }
  230. input = `a := [1,2,3,[1,2,[1,2],3,[1,2,3,4],[1,2,3,4,5],4,5],4,5]`
  231. if err := UnitTestPrettyPrinting(input, "",
  232. `a := [
  233. 1,
  234. 2,
  235. 3,
  236. [
  237. 1,
  238. 2,
  239. [1, 2],
  240. 3,
  241. [1, 2, 3, 4],
  242. [
  243. 1,
  244. 2,
  245. 3,
  246. 4,
  247. 5
  248. ],
  249. 4,
  250. 5
  251. ],
  252. 4,
  253. 5
  254. ]`); err != nil {
  255. t.Error(err)
  256. return
  257. }
  258. input = `a := [1,2,3,[1,2,{"a":1,"b":1,"c":1,"d":1},3,[1,2,3,4],4,5],4,5]`
  259. if err := UnitTestPrettyPrinting(input, "",
  260. `a := [
  261. 1,
  262. 2,
  263. 3,
  264. [
  265. 1,
  266. 2,
  267. {
  268. "a" : 1,
  269. "b" : 1,
  270. "c" : 1,
  271. "d" : 1
  272. },
  273. 3,
  274. [1, 2, 3, 4],
  275. 4,
  276. 5
  277. ],
  278. 4,
  279. 5
  280. ]`); err != nil {
  281. t.Error(err)
  282. return
  283. }
  284. }
  285. func TestSpecialCasePrinting2(t *testing.T) {
  286. input := `
  287. a := 1
  288. a := 2
  289. sink RegisterNewPlayer
  290. kindmatch ["foo",2]
  291. statematch {"a":1,"b":1,"c":1,"d":1}
  292. scopematch []
  293. suppresses ["abs"]
  294. priority 0
  295. {
  296. log("1223")
  297. log("1223")
  298. func foo (z=[1,2,3,4,5]) {
  299. a := 1
  300. b := 2
  301. }
  302. log("1223")
  303. try {
  304. x := [1,2,3,4]
  305. raise("test 12", null, [1,2,3])
  306. } except e {
  307. p := 1
  308. }
  309. }
  310. `
  311. if err := UnitTestPrettyPrinting(input, "",
  312. `a := 1
  313. a := 2
  314. sink RegisterNewPlayer
  315. kindmatch ["foo", 2]
  316. statematch {
  317. "a" : 1,
  318. "b" : 1,
  319. "c" : 1,
  320. "d" : 1
  321. }
  322. scopematch []
  323. suppresses ["abs"]
  324. priority 0
  325. {
  326. log("1223")
  327. log("1223")
  328. func foo(z=[
  329. 1,
  330. 2,
  331. 3,
  332. 4,
  333. 5
  334. ]) {
  335. a := 1
  336. b := 2
  337. }
  338. log("1223")
  339. try {
  340. x := [1, 2, 3, 4]
  341. raise("test 12", null, [1, 2, 3])
  342. } except e {
  343. p := 1
  344. }
  345. }`); err != nil {
  346. t.Error(err)
  347. }
  348. input = `
  349. /*
  350. Some initial comment
  351. bla
  352. */
  353. a := 1
  354. func aaa() {
  355. mutex myresource {
  356. globalResource := "new value"
  357. }
  358. func myfunc(a, b, c=1) {
  359. a := 1 + 1 # Test
  360. }
  361. x := [ 1,2,3,4,5]
  362. a:=1;b:=1
  363. /*Foo*/
  364. Foo := {
  365. "super" : [ Bar ]
  366. /*
  367. * Object IDs
  368. */
  369. "id" : 0 # aaaa
  370. "idx" : 0
  371. /*Constructor*/
  372. "init" : func(id)
  373. {
  374. super[0]()
  375. this.id := id
  376. }
  377. /*
  378. Return the object ID
  379. */
  380. "getId" : func() {
  381. return this.idx
  382. }
  383. /*
  384. Set the object ID
  385. */
  386. "setId" : func(id) {
  387. this.idx := id
  388. }
  389. }
  390. for a in range(2, 10, 2) {
  391. a := 1
  392. }
  393. for a > 0 {
  394. a := 1
  395. }
  396. if a == 1 {
  397. a := a + 1
  398. } elif a == 2 {
  399. a := a + 2
  400. } else {
  401. a := 99
  402. }
  403. try {
  404. raise("MyError", "My error message", [1,2,3])
  405. } except "MyError" as e {
  406. log(e)
  407. }
  408. }
  409. b:=1
  410. `
  411. if err := UnitTestPrettyPrinting(input, "",
  412. `/*
  413. Some initial comment
  414. bla
  415. */
  416. a := 1
  417. func aaa() {
  418. mutex myresource {
  419. globalResource := "new value"
  420. }
  421. func myfunc(a, b, c=1) {
  422. a := 1 + 1 # Test
  423. }
  424. x := [
  425. 1,
  426. 2,
  427. 3,
  428. 4,
  429. 5
  430. ]
  431. a := 1
  432. b := 1
  433. /* Foo */
  434. Foo := {
  435. "super" : [Bar],
  436. /*
  437. * Object IDs
  438. */
  439. "id" : 0 # aaaa,
  440. "idx" : 0,
  441. /* Constructor */
  442. "init" : func (id) {
  443. super[0]()
  444. this.id := id
  445. },
  446. /*
  447. Return the object ID
  448. */
  449. "getId" : func () {
  450. return this.idx
  451. },
  452. /*
  453. Set the object ID
  454. */
  455. "setId" : func (id) {
  456. this.idx := id
  457. }
  458. }
  459. for a in range(2, 10, 2) {
  460. a := 1
  461. }
  462. for a > 0 {
  463. a := 1
  464. }
  465. if a == 1 {
  466. a := a + 1
  467. } elif a == 2 {
  468. a := a + 2
  469. } else {
  470. a := 99
  471. }
  472. try {
  473. raise("MyError", "My error message", [1, 2, 3])
  474. } except "MyError" as e {
  475. log(e)
  476. }
  477. }
  478. b := 1`); err != nil {
  479. t.Error(err)
  480. return
  481. }
  482. }