helper_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 TestASTNode(t *testing.T) {
  15. n, err := ParseWithRuntime("test1", "- 1", &DummyRuntimeProvider{})
  16. if err != nil {
  17. t.Error("Cannot parse test AST:", err)
  18. return
  19. }
  20. n2, err := ParseWithRuntime("test2", "-2", &DummyRuntimeProvider{})
  21. if err != nil {
  22. t.Error("Cannot parse test AST:", err)
  23. return
  24. }
  25. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
  26. Token is different:
  27. Pos is different 2 vs 1
  28. Val is different 1 vs 2
  29. Lpos is different 3 vs 2
  30. {
  31. "ID": 6,
  32. "Pos": 2,
  33. "Val": "1",
  34. "Identifier": false,
  35. "AllowEscapes": false,
  36. "Lsource": "test1",
  37. "Lline": 1,
  38. "Lpos": 3
  39. }
  40. vs
  41. {
  42. "ID": 6,
  43. "Pos": 1,
  44. "Val": "2",
  45. "Identifier": false,
  46. "AllowEscapes": false,
  47. "Lsource": "test2",
  48. "Lline": 1,
  49. "Lpos": 2
  50. }
  51. AST Nodes:
  52. number: 1
  53. vs
  54. number: 2
  55. ` {
  56. t.Error("Unexpected result: ", msg)
  57. return
  58. }
  59. n, err = ParseWithRuntime("test1", "-1", &DummyRuntimeProvider{})
  60. if err != nil {
  61. t.Error("Cannot parse test AST:", err)
  62. return
  63. }
  64. n2, err = ParseWithRuntime("test2", "-a", &DummyRuntimeProvider{})
  65. if err != nil {
  66. t.Error("Cannot parse test AST:", err)
  67. return
  68. }
  69. if ok, msg := n.Equals(n2, true); ok || msg != `Path to difference: minus > number
  70. Name is different number vs identifier
  71. Token is different:
  72. ID is different 6 vs 7
  73. Val is different 1 vs a
  74. Identifier is different false vs true
  75. {
  76. "ID": 6,
  77. "Pos": 1,
  78. "Val": "1",
  79. "Identifier": false,
  80. "AllowEscapes": false,
  81. "Lsource": "test1",
  82. "Lline": 1,
  83. "Lpos": 2
  84. }
  85. vs
  86. {
  87. "ID": 7,
  88. "Pos": 1,
  89. "Val": "a",
  90. "Identifier": true,
  91. "AllowEscapes": false,
  92. "Lsource": "test2",
  93. "Lline": 1,
  94. "Lpos": 2
  95. }
  96. AST Nodes:
  97. number: 1
  98. vs
  99. identifier: a
  100. ` {
  101. t.Error("Unexpected result: ", msg)
  102. return
  103. }
  104. }
  105. func TestASTNode2(t *testing.T) {
  106. n, err := ParseWithRuntime("", "- 1", &DummyRuntimeProvider{})
  107. if err != nil {
  108. t.Error("Cannot parse test AST:", err)
  109. return
  110. }
  111. n2, err := ParseWithRuntime("", "a - b", &DummyRuntimeProvider{})
  112. if err != nil {
  113. t.Error("Cannot parse test AST:", err)
  114. return
  115. }
  116. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus
  117. Number of children is different 1 vs 2
  118. AST Nodes:
  119. minus
  120. number: 1
  121. vs
  122. minus
  123. identifier: a
  124. identifier: b
  125. ` {
  126. t.Error("Unexpected result: ", msg)
  127. return
  128. }
  129. n, err = ParseWithRuntime("", "-1 #test", &DummyRuntimeProvider{})
  130. if err != nil {
  131. t.Error("Cannot parse test AST:", err)
  132. return
  133. }
  134. n2, err = ParseWithRuntime("", "-1", &DummyRuntimeProvider{})
  135. if err != nil {
  136. t.Error("Cannot parse test AST:", err)
  137. return
  138. }
  139. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
  140. Number of meta data entries is different 1 vs 0
  141. AST Nodes:
  142. number: 1 # test
  143. vs
  144. number: 1
  145. ` {
  146. t.Error("Unexpected result: ", msg)
  147. return
  148. }
  149. n, err = ParseWithRuntime("", "-1 #test", &DummyRuntimeProvider{})
  150. if err != nil {
  151. t.Error("Cannot parse test AST:", err)
  152. return
  153. }
  154. n2, err = ParseWithRuntime("", "-1 #wurst", &DummyRuntimeProvider{})
  155. if err != nil {
  156. t.Error("Cannot parse test AST:", err)
  157. return
  158. }
  159. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
  160. Meta data value is different test vs wurst
  161. AST Nodes:
  162. number: 1 # test
  163. vs
  164. number: 1 # wurst
  165. ` {
  166. t.Error("Unexpected result: ", msg)
  167. return
  168. }
  169. }
  170. func TestASTNode3(t *testing.T) {
  171. n, err := ParseWithRuntime("", "1 #test", &DummyRuntimeProvider{})
  172. if err != nil {
  173. t.Error("Cannot parse test AST:", err)
  174. return
  175. }
  176. n2, err := ParseWithRuntime("", "/*test*/ 1", &DummyRuntimeProvider{})
  177. if err != nil {
  178. t.Error("Cannot parse test AST:", err)
  179. return
  180. }
  181. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: number
  182. Token is different:
  183. Pos is different 0 vs 9
  184. Lpos is different 1 vs 10
  185. {
  186. "ID": 6,
  187. "Pos": 0,
  188. "Val": "1",
  189. "Identifier": false,
  190. "AllowEscapes": false,
  191. "Lsource": "",
  192. "Lline": 1,
  193. "Lpos": 1
  194. }
  195. vs
  196. {
  197. "ID": 6,
  198. "Pos": 9,
  199. "Val": "1",
  200. "Identifier": false,
  201. "AllowEscapes": false,
  202. "Lsource": "",
  203. "Lline": 1,
  204. "Lpos": 10
  205. }
  206. Meta data type is different MetaDataPostComment vs MetaDataPreComment
  207. AST Nodes:
  208. number: 1 # test
  209. vs
  210. number: 1 # test
  211. ` {
  212. t.Error("Unexpected result: ", msg)
  213. return
  214. }
  215. }
  216. func TestASTNode4(t *testing.T) {
  217. // Test building an AST from an invalid
  218. if _, err := ASTFromJSONObject(map[string]interface{}{
  219. "value": "foo",
  220. }); err == nil || err.Error() != "Found json ast node without a name: map[value:foo]" {
  221. t.Error("Unexpected result: ", err)
  222. return
  223. }
  224. if _, err := ASTFromJSONObject(map[string]interface{}{
  225. "name": "foo",
  226. "children": []map[string]interface{}{
  227. {
  228. "value": "bar",
  229. },
  230. },
  231. }); err == nil || err.Error() != "Found json ast node without a name: map[value:bar]" {
  232. t.Error("Unexpected result: ", err)
  233. return
  234. }
  235. // Test population of missing information
  236. if ast, err := ASTFromJSONObject(map[string]interface{}{
  237. "name": "foo",
  238. }); err != nil || ast.String() != "foo\n" || ast.Token.String() != `v:""` {
  239. t.Error("Unexpected result: ", ast.Token.String(), ast.String(), err)
  240. return
  241. }
  242. if ast, err := ASTFromJSONObject(map[string]interface{}{
  243. "name": "foo",
  244. "children": []map[string]interface{}{
  245. {
  246. "name": "bar",
  247. },
  248. },
  249. }); err != nil || ast.String() != "foo\n bar\n" || ast.Token.String() != `v:""` {
  250. t.Error("Unexpected result: ", ast.Token.String(), ast.String(), err)
  251. return
  252. }
  253. }
  254. func TestLABuffer(t *testing.T) {
  255. buf := NewLABuffer(Lex("test", "1 2 3 4 5 6 7 8 9"), 3)
  256. if token, ok := buf.Next(); token.Val != "1" || !ok {
  257. t.Error("Unexpected result: ", token, ok)
  258. return
  259. }
  260. if token, ok := buf.Next(); token.Val != "2" || !ok {
  261. t.Error("Unexpected result: ", token, ok)
  262. return
  263. }
  264. testLABufferPeek(t, buf)
  265. }
  266. func testLABufferPeek(t *testing.T, buf *LABuffer) {
  267. // Check Peek
  268. if token, ok := buf.Peek(0); token.Val != "3" || !ok {
  269. t.Error("Unexpected result: ", token, ok)
  270. return
  271. }
  272. if token, ok := buf.Peek(1); token.Val != "4" || !ok {
  273. t.Error("Unexpected result: ", token, ok)
  274. return
  275. }
  276. if token, ok := buf.Peek(2); token.Val != "5" || !ok {
  277. t.Error("Unexpected result: ", token, ok)
  278. return
  279. }
  280. if token, ok := buf.Peek(3); token.ID != TokenEOF || ok {
  281. t.Error("Unexpected result: ", token, ok)
  282. return
  283. }
  284. testLABufferContinue(t, buf)
  285. }
  286. func testLABufferContinue(t *testing.T, buf *LABuffer) {
  287. // Continue
  288. if token, ok := buf.Next(); token.Val != "3" || !ok {
  289. t.Error("Unexpected result: ", token, ok)
  290. return
  291. }
  292. if token, ok := buf.Next(); token.Val != "4" || !ok {
  293. t.Error("Unexpected result: ", token, ok)
  294. return
  295. }
  296. if token, ok := buf.Next(); token.Val != "5" || !ok {
  297. t.Error("Unexpected result: ", token, ok)
  298. return
  299. }
  300. if token, ok := buf.Next(); token.Val != "6" || !ok {
  301. t.Error("Unexpected result: ", token, ok)
  302. return
  303. }
  304. if token, ok := buf.Next(); token.Val != "7" || !ok {
  305. t.Error("Unexpected result: ", token, ok)
  306. return
  307. }
  308. if token, ok := buf.Next(); token.Val != "8" || !ok {
  309. t.Error("Unexpected result: ", token, ok)
  310. return
  311. }
  312. testLABufferPeek2(t, buf)
  313. }
  314. func testLABufferPeek2(t *testing.T, buf *LABuffer) {
  315. // Check Peek
  316. if token, ok := buf.Peek(0); token.Val != "9" || !ok {
  317. t.Error("Unexpected result: ", token, ok)
  318. return
  319. }
  320. if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
  321. t.Error("Unexpected result: ", token, ok)
  322. return
  323. }
  324. if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
  325. t.Error("Unexpected result: ", token, ok)
  326. return
  327. }
  328. testLABufferContinue2(t, buf)
  329. }
  330. func testLABufferContinue2(t *testing.T, buf *LABuffer) {
  331. // Continue
  332. if token, ok := buf.Next(); token.Val != "9" || !ok {
  333. t.Error("Unexpected result: ", token, ok)
  334. return
  335. }
  336. // Check Peek
  337. if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
  338. t.Error("Unexpected result: ", token, ok)
  339. return
  340. }
  341. if token, ok := buf.Peek(1); token.ID != TokenEOF || ok {
  342. t.Error("Unexpected result: ", token, ok)
  343. return
  344. }
  345. // Continue
  346. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  347. t.Error("Unexpected result: ", token, ok)
  348. return
  349. }
  350. }
  351. func TestLABuffer2(t *testing.T) {
  352. // New Buffer
  353. buf := NewLABuffer(Lex("test", "1 2 3"), 3)
  354. if token, _ := buf.Next(); token.Val != "1" {
  355. t.Error("Unexpected result: ", token)
  356. return
  357. }
  358. if token, _ := buf.Next(); token.Val != "2" {
  359. t.Error("Unexpected result: ", token)
  360. return
  361. }
  362. // Check Peek
  363. if token, _ := buf.Peek(0); token.Val != "3" {
  364. t.Error("Unexpected result: ", token)
  365. return
  366. }
  367. if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
  368. t.Error("Unexpected result: ", token, ok)
  369. return
  370. }
  371. if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
  372. t.Error("Unexpected result: ", token, ok)
  373. return
  374. }
  375. if token, _ := buf.Next(); token.Val != "3" {
  376. t.Error("Unexpected result: ", token)
  377. return
  378. }
  379. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  380. t.Error("Unexpected result: ", token, ok)
  381. return
  382. }
  383. }
  384. func TestLABuffer3(t *testing.T) {
  385. // New Buffer - test edge case
  386. buf := NewLABuffer(Lex("test", ""), 0)
  387. if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
  388. t.Error("Unexpected result: ", token, ok)
  389. return
  390. }
  391. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  392. t.Error("Unexpected result: ", token, ok)
  393. return
  394. }
  395. if token, ok := buf.Peek(0); token.ID != TokenEOF || ok {
  396. t.Error("Unexpected result: ", token, ok)
  397. return
  398. }
  399. if token, ok := buf.Next(); token.ID != TokenEOF || ok {
  400. t.Error("Unexpected result: ", token, ok)
  401. return
  402. }
  403. }