helper_test.go 9.8 KB

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