helper_test.go 8.8 KB

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