helper_test.go 8.8 KB

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