helper_test.go 9.0 KB

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