helper_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. n, err = ParseWithRuntime("", "- 1", &DummyRuntimeProvider{})
  105. if err != nil {
  106. t.Error("Cannot parse test AST:", err)
  107. return
  108. }
  109. n2, err = ParseWithRuntime("", "a - b", &DummyRuntimeProvider{})
  110. if err != nil {
  111. t.Error("Cannot parse test AST:", err)
  112. return
  113. }
  114. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus
  115. Number of children is different 1 vs 2
  116. AST Nodes:
  117. minus
  118. number: 1
  119. vs
  120. minus
  121. identifier: a
  122. identifier: b
  123. ` {
  124. t.Error("Unexpected result: ", msg)
  125. return
  126. }
  127. n, err = ParseWithRuntime("", "-1 #test", &DummyRuntimeProvider{})
  128. if err != nil {
  129. t.Error("Cannot parse test AST:", err)
  130. return
  131. }
  132. n2, err = ParseWithRuntime("", "-1", &DummyRuntimeProvider{})
  133. if err != nil {
  134. t.Error("Cannot parse test AST:", err)
  135. return
  136. }
  137. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
  138. Number of meta data entries is different 1 vs 0
  139. AST Nodes:
  140. number: 1 # test
  141. vs
  142. number: 1
  143. ` {
  144. t.Error("Unexpected result: ", msg)
  145. return
  146. }
  147. n, err = ParseWithRuntime("", "-1 #test", &DummyRuntimeProvider{})
  148. if err != nil {
  149. t.Error("Cannot parse test AST:", err)
  150. return
  151. }
  152. n2, err = ParseWithRuntime("", "-1 #wurst", &DummyRuntimeProvider{})
  153. if err != nil {
  154. t.Error("Cannot parse test AST:", err)
  155. return
  156. }
  157. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: minus > number
  158. Meta data value is different test vs wurst
  159. AST Nodes:
  160. number: 1 # test
  161. vs
  162. number: 1 # wurst
  163. ` {
  164. t.Error("Unexpected result: ", msg)
  165. return
  166. }
  167. n, err = ParseWithRuntime("", "1 #test", &DummyRuntimeProvider{})
  168. if err != nil {
  169. t.Error("Cannot parse test AST:", err)
  170. return
  171. }
  172. n2, err = ParseWithRuntime("", "/*test*/ 1", &DummyRuntimeProvider{})
  173. if err != nil {
  174. t.Error("Cannot parse test AST:", err)
  175. return
  176. }
  177. if ok, msg := n.Equals(n2, false); ok || msg != `Path to difference: number
  178. Token is different:
  179. Pos is different 0 vs 9
  180. Lpos is different 1 vs 10
  181. {
  182. "ID": 6,
  183. "Pos": 0,
  184. "Val": "1",
  185. "Identifier": false,
  186. "AllowEscapes": false,
  187. "Lsource": "",
  188. "Lline": 1,
  189. "Lpos": 1
  190. }
  191. vs
  192. {
  193. "ID": 6,
  194. "Pos": 9,
  195. "Val": "1",
  196. "Identifier": false,
  197. "AllowEscapes": false,
  198. "Lsource": "",
  199. "Lline": 1,
  200. "Lpos": 10
  201. }
  202. Meta data type is different MetaDataPostComment vs MetaDataPreComment
  203. AST Nodes:
  204. number: 1 # test
  205. vs
  206. number: 1 # test
  207. ` {
  208. t.Error("Unexpected result: ", msg)
  209. return
  210. }
  211. // Test building an AST from an invalid
  212. if _, err := ASTFromJSONObject(map[string]interface{}{
  213. "value": "foo",
  214. }); err == nil || err.Error() != "Found json ast node without a name: map[value:foo]" {
  215. t.Error("Unexpected result: ", err)
  216. return
  217. }
  218. if _, err := ASTFromJSONObject(map[string]interface{}{
  219. "name": "foo",
  220. "children": []map[string]interface{}{
  221. {
  222. "value": "bar",
  223. },
  224. },
  225. }); err == nil || err.Error() != "Found json ast node without a name: map[value:bar]" {
  226. t.Error("Unexpected result: ", err)
  227. return
  228. }
  229. // Test population of missing information
  230. if ast, err := ASTFromJSONObject(map[string]interface{}{
  231. "name": "foo",
  232. }); err != nil || ast.String() != "foo\n" || ast.Token.String() != `v:""` {
  233. t.Error("Unexpected result: ", ast.Token.String(), ast.String(), err)
  234. return
  235. }
  236. if ast, err := ASTFromJSONObject(map[string]interface{}{
  237. "name": "foo",
  238. "children": []map[string]interface{}{
  239. {
  240. "name": "bar",
  241. },
  242. },
  243. }); err != nil || ast.String() != "foo\n bar\n" || ast.Token.String() != `v:""` {
  244. t.Error("Unexpected result: ", ast.Token.String(), ast.String(), err)
  245. return
  246. }
  247. }
  248. func TestLABuffer(t *testing.T) {
  249. buf := NewLABuffer(Lex("test", "1 2 3 4 5 6 7 8 9"), 3)
  250. if token, ok := buf.Next(); token.Val != "1" || !ok {
  251. t.Error("Unexpected result: ", token, ok)
  252. return
  253. }
  254. if token, ok := buf.Next(); token.Val != "2" || !ok {
  255. t.Error("Unexpected result: ", token, ok)
  256. return
  257. }
  258. // Check Peek
  259. if token, ok := buf.Peek(0); token.Val != "3" || !ok {
  260. t.Error("Unexpected result: ", token, ok)
  261. return
  262. }
  263. if token, ok := buf.Peek(1); token.Val != "4" || !ok {
  264. t.Error("Unexpected result: ", token, ok)
  265. return
  266. }
  267. if token, ok := buf.Peek(2); token.Val != "5" || !ok {
  268. t.Error("Unexpected result: ", token, ok)
  269. return
  270. }
  271. if token, ok := buf.Peek(3); token.ID != TokenEOF || ok {
  272. t.Error("Unexpected result: ", token, ok)
  273. return
  274. }
  275. // Continue
  276. if token, ok := buf.Next(); token.Val != "3" || !ok {
  277. t.Error("Unexpected result: ", token, ok)
  278. return
  279. }
  280. if token, ok := buf.Next(); token.Val != "4" || !ok {
  281. t.Error("Unexpected result: ", token, ok)
  282. return
  283. }
  284. if token, ok := buf.Next(); token.Val != "5" || !ok {
  285. t.Error("Unexpected result: ", token, ok)
  286. return
  287. }
  288. if token, ok := buf.Next(); token.Val != "6" || !ok {
  289. t.Error("Unexpected result: ", token, ok)
  290. return
  291. }
  292. if token, ok := buf.Next(); token.Val != "7" || !ok {
  293. t.Error("Unexpected result: ", token, ok)
  294. return
  295. }
  296. if token, ok := buf.Next(); token.Val != "8" || !ok {
  297. t.Error("Unexpected result: ", token, ok)
  298. return
  299. }
  300. // Check Peek
  301. if token, ok := buf.Peek(0); token.Val != "9" || !ok {
  302. t.Error("Unexpected result: ", token, ok)
  303. return
  304. }
  305. if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
  306. t.Error("Unexpected result: ", token, ok)
  307. return
  308. }
  309. if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
  310. t.Error("Unexpected result: ", token, ok)
  311. return
  312. }
  313. // Continue
  314. if token, ok := buf.Next(); token.Val != "9" || !ok {
  315. t.Error("Unexpected result: ", token, ok)
  316. return
  317. }
  318. // Check Peek
  319. if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
  320. t.Error("Unexpected result: ", token, ok)
  321. return
  322. }
  323. if token, ok := buf.Peek(1); token.ID != TokenEOF || ok {
  324. t.Error("Unexpected result: ", token, ok)
  325. return
  326. }
  327. // Continue
  328. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  329. t.Error("Unexpected result: ", token, ok)
  330. return
  331. }
  332. // New Buffer
  333. buf = NewLABuffer(Lex("test", "1 2 3"), 3)
  334. if token, ok := buf.Next(); token.Val != "1" || !ok {
  335. t.Error("Unexpected result: ", token, ok)
  336. return
  337. }
  338. if token, ok := buf.Next(); token.Val != "2" || !ok {
  339. t.Error("Unexpected result: ", token, ok)
  340. return
  341. }
  342. // Check Peek
  343. if token, ok := buf.Peek(0); token.Val != "3" || !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. if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
  352. t.Error("Unexpected result: ", token, ok)
  353. return
  354. }
  355. if token, ok := buf.Next(); token.Val != "3" || !ok {
  356. t.Error("Unexpected result: ", token, ok)
  357. return
  358. }
  359. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  360. t.Error("Unexpected result: ", token, ok)
  361. return
  362. }
  363. // New Buffer - test edge case
  364. buf = NewLABuffer(Lex("test", ""), 0)
  365. if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
  366. t.Error("Unexpected result: ", token, ok)
  367. return
  368. }
  369. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  370. t.Error("Unexpected result: ", token, ok)
  371. return
  372. }
  373. if token, ok := buf.Peek(0); token.ID != TokenEOF || ok {
  374. t.Error("Unexpected result: ", token, ok)
  375. return
  376. }
  377. if token, ok := buf.Next(); token.ID != TokenEOF || ok {
  378. t.Error("Unexpected result: ", token, ok)
  379. return
  380. }
  381. }