parser_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package parser
  11. import (
  12. "bytes"
  13. "encoding/json"
  14. "fmt"
  15. "testing"
  16. )
  17. /*
  18. Test RuntimeProvider provides runtime components for a parse tree.
  19. */
  20. type TestRuntimeProvider struct {
  21. }
  22. /*
  23. Runtime returns a runtime component for a given ASTNode.
  24. */
  25. func (trp *TestRuntimeProvider) Runtime(node *ASTNode) Runtime {
  26. return &TestRuntime{}
  27. }
  28. /*
  29. Test Runtime provides the runtime for an ASTNode.
  30. */
  31. type TestRuntime struct {
  32. }
  33. /*
  34. Validate this runtime component and all its child components.
  35. */
  36. func (tr *TestRuntime) Validate() error {
  37. return nil
  38. }
  39. /*
  40. Eval evaluate this runtime component.
  41. */
  42. func (tr *TestRuntime) Eval() (interface{}, error) {
  43. return nil, nil
  44. }
  45. func TestSimpleExpressionParsing(t *testing.T) {
  46. // Test error output
  47. input := `"bl\*a"`
  48. if _, err := Parse("mytest", input); err.Error() !=
  49. "Parse error in mytest: Lexical error (invalid syntax while parsing escape sequences) (Line:1 Pos:1)" {
  50. t.Error(err)
  51. return
  52. }
  53. // Test incomplete expression
  54. input = `a *`
  55. if _, err := Parse("mytest", input); err.Error() !=
  56. "Parse error in mytest: Unexpected end" {
  57. t.Error(err)
  58. return
  59. }
  60. // Test prefix operator
  61. input = ` + a - -5`
  62. expectedOutput := `
  63. minus
  64. plus
  65. value: "a"
  66. minus
  67. value: "5"
  68. `[1:]
  69. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  70. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  71. return
  72. }
  73. // Test simple arithmetics
  74. input = "a + b * 5 /2"
  75. expectedOutput = `
  76. plus
  77. value: "a"
  78. div
  79. times
  80. value: "b"
  81. value: "5"
  82. value: "2"
  83. `[1:]
  84. if res, err := ParseWithRuntime("mytest", input, &TestRuntimeProvider{}); err != nil || fmt.Sprint(res) != expectedOutput {
  85. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  86. return
  87. }
  88. // Test brackets
  89. input = "a + 1 * (5 + 6)"
  90. expectedOutput = `
  91. plus
  92. value: "a"
  93. times
  94. value: "1"
  95. plus
  96. value: "5"
  97. value: "6"
  98. `[1:]
  99. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  100. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  101. return
  102. }
  103. input = "(a + 1) * 5 / (6 - 2)"
  104. expectedOutput = `
  105. div
  106. times
  107. plus
  108. value: "a"
  109. value: "1"
  110. value: "5"
  111. minus
  112. value: "6"
  113. value: "2"
  114. `[1:]
  115. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  116. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  117. return
  118. }
  119. input = "a + 1 * [1,2,[1,2],3]"
  120. expectedOutput = `
  121. plus
  122. value: "a"
  123. times
  124. value: "1"
  125. list
  126. value: "1"
  127. value: "2"
  128. list
  129. value: "1"
  130. value: "2"
  131. value: "3"
  132. `[1:]
  133. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  134. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  135. return
  136. }
  137. // Test logical operators
  138. input = "not (a + 1) * 5 and tRue or not 1 - 5 != !test"
  139. expectedOutput = `
  140. or
  141. and
  142. not
  143. times
  144. plus
  145. value: "a"
  146. value: "1"
  147. value: "5"
  148. true
  149. not
  150. !=
  151. minus
  152. value: "1"
  153. value: "5"
  154. value: "!test"
  155. `[1:]
  156. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  157. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  158. return
  159. }
  160. }
  161. func TestQueryParsing(t *testing.T) {
  162. // Test get expressions
  163. input := `
  164. GeT Song FROM group test`
  165. expectedOutput := `
  166. get
  167. value: "Song"
  168. from
  169. group
  170. value: "test"
  171. `[1:]
  172. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  173. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  174. return
  175. }
  176. // Test lookup expressions
  177. input = `
  178. lOOkup Song "a","b","c", "blaД"
  179. FROM group test`
  180. expectedOutput = `
  181. lookup
  182. value: "Song"
  183. value: "a"
  184. value: "b"
  185. value: "c"
  186. value: "blaД"
  187. from
  188. group
  189. value: "test"
  190. `[1:]
  191. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  192. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  193. return
  194. }
  195. // Test where clause
  196. input = `
  197. GeT bla FROM group test where x = 1 AND b = -1 where True`
  198. expectedOutput = `
  199. get
  200. value: "bla"
  201. from
  202. group
  203. value: "test"
  204. where
  205. and
  206. =
  207. value: "x"
  208. value: "1"
  209. =
  210. value: "b"
  211. minus
  212. value: "1"
  213. where
  214. true
  215. `[1:]
  216. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  217. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  218. return
  219. }
  220. input = `
  221. GeT bla where nest.nint = 1 AND b = -1`
  222. expectedOutput = `
  223. get
  224. value: "bla"
  225. where
  226. and
  227. =
  228. value: "nest.nint"
  229. value: "1"
  230. =
  231. value: "b"
  232. minus
  233. value: "1"
  234. `[1:]
  235. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  236. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  237. return
  238. }
  239. // Test traverse clause
  240. input = `
  241. GeT bla TraverSE :::bla where true or false TraverSE test:::xxx where false TraverSE :::ttt where true END END END where 1 = 1`
  242. expectedOutput = `
  243. get
  244. value: "bla"
  245. traverse
  246. value: ":::bla"
  247. where
  248. or
  249. true
  250. false
  251. traverse
  252. value: "test:::xxx"
  253. where
  254. false
  255. traverse
  256. value: ":::ttt"
  257. where
  258. true
  259. where
  260. =
  261. value: "1"
  262. value: "1"
  263. `[1:]
  264. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  265. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  266. return
  267. }
  268. // Test functions
  269. input = `
  270. GeT Song where @a() or @count("File:File:StoredData:Data") > 1 and @boolfunc1(123,"test", aaa)`
  271. expectedOutput = `
  272. get
  273. value: "Song"
  274. where
  275. or
  276. func
  277. value: "a"
  278. and
  279. >
  280. func
  281. value: "count"
  282. value: "File:File:"...
  283. value: "1"
  284. func
  285. value: "boolfunc1"
  286. value: "123"
  287. value: "test"
  288. value: "aaa"
  289. `[1:]
  290. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  291. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  292. return
  293. }
  294. }
  295. func TestShowParsing(t *testing.T) {
  296. // Test simple show expression
  297. input := `
  298. get song where true primary 1:song show name, state`
  299. expectedOutput := `
  300. get
  301. value: "song"
  302. where
  303. true
  304. primary
  305. value: "1:song"
  306. show
  307. showterm: "name"
  308. showterm: "state"
  309. `[1:]
  310. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  311. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  312. return
  313. }
  314. input = `
  315. get song where true primary 1:song show name, state, @test(12, r"34") AS Bla FORMAT x, key`
  316. expectedOutput = `
  317. get
  318. value: "song"
  319. where
  320. true
  321. primary
  322. value: "1:song"
  323. show
  324. showterm: "name"
  325. showterm: "state"
  326. showterm
  327. func
  328. value: "test"
  329. value: "12"
  330. value: "34"
  331. as
  332. value: "Bla"
  333. format
  334. value: "x"
  335. showterm: "key"
  336. `[1:]
  337. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  338. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  339. return
  340. }
  341. input = `
  342. get song where true primary 1:song show @test(12, r"34") format x`
  343. expectedOutput = `
  344. get
  345. value: "song"
  346. where
  347. true
  348. primary
  349. value: "1:song"
  350. show
  351. showterm
  352. func
  353. value: "test"
  354. value: "12"
  355. value: "34"
  356. format
  357. value: "x"
  358. `[1:]
  359. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  360. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  361. return
  362. }
  363. input = `
  364. get song where true primary 1:song show
  365. Song:title AS r'Title (mytitle)',
  366. r'Song!2:t title' AS "Title test" FORMAT text:bla_bla_blub:dudududu,
  367. x:kind`
  368. expectedOutput = `
  369. get
  370. value: "song"
  371. where
  372. true
  373. primary
  374. value: "1:song"
  375. show
  376. showterm: "Song:title"
  377. as
  378. value: "Title (myt"...
  379. showterm: "Song!2:t t"...
  380. as
  381. value: "Title test"
  382. format
  383. value: "text:bla_b"...
  384. showterm: "x:kind"
  385. `[1:]
  386. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  387. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  388. return
  389. }
  390. input = `
  391. get song where true // 'div' show bla wIth orderinG(ASCending aa,Descending bb), FILTERING(ISNOTNULL test2,UNIQUE test3, uniquecount test3), nulltraversal(true)`
  392. expectedOutput = `
  393. get
  394. value: "song"
  395. where
  396. divint
  397. true
  398. value: "div"
  399. show
  400. showterm: "bla"
  401. with
  402. ordering
  403. asc
  404. value: "aa"
  405. desc
  406. value: "bb"
  407. filtering
  408. isnotnull
  409. value: "test2"
  410. unique
  411. value: "test3"
  412. uniquecount
  413. value: "test3"
  414. nulltraversal
  415. true
  416. `[1:]
  417. if res, err := Parse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  418. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  419. return
  420. }
  421. }
  422. func TestParserErrorCases(t *testing.T) {
  423. if res, err := ParseWithRuntime("mytest", "", &TestRuntimeProvider{}); err.Error() !=
  424. "Parse error in mytest: Unexpected end" {
  425. t.Error("Unexpected result", res, err)
  426. return
  427. }
  428. if res, err := ParseWithRuntime("mytest", "GET r\"aa", &TestRuntimeProvider{}); err.Error() !=
  429. "Parse error in mytest: Lexical error (Invalid node kind 'r\"aa' - can only contain [a-zA-Z0-9_]) (Line:1 Pos:5)" {
  430. t.Error("Unexpected result", res, err)
  431. return
  432. }
  433. if res, err := ParseWithRuntime("mytest", "= GET", &TestRuntimeProvider{}); err.Error() !=
  434. "Parse error in mytest: Term cannot start an expression (=) (Line:1 Pos:1)" {
  435. t.Error("Unexpected result", res, err)
  436. return
  437. }
  438. if res, err := ParseWithRuntime("mytest", "get a where 1 (", &TestRuntimeProvider{}); err.Error() !=
  439. "Parse error in mytest: Term can only start an expression (() (Line:1 Pos:15)" {
  440. t.Error("Unexpected result", res, err)
  441. return
  442. }
  443. if res, err := ParseWithRuntime("mytest", "get a where (=", &TestRuntimeProvider{}); err.Error() !=
  444. "Parse error in mytest: Term cannot start an expression (=) (Line:1 Pos:14)" {
  445. t.Error("Unexpected result", res, err)
  446. return
  447. }
  448. // Test "Get" parsing with invalid lexer output
  449. res, err := testParserRun([]LexToken{
  450. {TokenGET, 1, "", 1, 1},
  451. {TokenGET, 1, "", 1, 1},
  452. {TokenEOF, 1, "", 1, 1},
  453. })
  454. if err.Error() != "Parse error in special test: Unexpected term (Line:1 Pos:1)" {
  455. t.Error("Unexpected result", res, err)
  456. return
  457. }
  458. res, err = testParserRun([]LexToken{
  459. {TokenLOOKUP, 1, "", 1, 1},
  460. {TokenGET, 1, "", 1, 1},
  461. {TokenEOF, 1, "", 1, 1},
  462. })
  463. if err.Error() != "Parse error in special test: Unexpected term (Line:1 Pos:1)" {
  464. t.Error("Unexpected result", res, err)
  465. return
  466. }
  467. if res, err := ParseWithRuntime("mytest", "lookup x", &TestRuntimeProvider{}); err.Error() !=
  468. "Parse error in mytest: Unexpected end" {
  469. t.Error("Unexpected result", res, err)
  470. return
  471. }
  472. if res, err := ParseWithRuntime("mytest", "lookup x '123', GET", &TestRuntimeProvider{}); err.Error() !=
  473. "Parse error in mytest: Unexpected term (GET) (Line:1 Pos:17)" {
  474. t.Error("Unexpected result", res, err)
  475. return
  476. }
  477. if res, err := ParseWithRuntime("mytest", "lookup x '123' GET", &TestRuntimeProvider{}); err.Error() !=
  478. "Parse error in mytest: Unexpected end" {
  479. t.Error("Unexpected result", res, err)
  480. return
  481. }
  482. if res, err := ParseWithRuntime("mytest", "GET x FROM GeT", &TestRuntimeProvider{}); err.Error() !=
  483. "Parse error in mytest: Unexpected term (GeT) (Line:1 Pos:12)" {
  484. t.Error("Unexpected result", res, err)
  485. return
  486. }
  487. if res, err := ParseWithRuntime("mytest", "GET x traverse GeT", &TestRuntimeProvider{}); err.Error() !=
  488. "Parse error in mytest: Unexpected term (GeT) (Line:1 Pos:16)" {
  489. t.Error("Unexpected result", res, err)
  490. return
  491. }
  492. if res, err := ParseWithRuntime("mytest", "GET x traverse ::: GeT", &TestRuntimeProvider{}); err.Error() !=
  493. "Parse error in mytest: Unexpected end" {
  494. t.Error("Unexpected result", res, err)
  495. return
  496. }
  497. if res, err := ParseWithRuntime("mytest", "GET x where @where(", &TestRuntimeProvider{}); err.Error() !=
  498. "Parse error in mytest: Unexpected term (where) (Line:1 Pos:14)" {
  499. t.Error("Unexpected result", res, err)
  500. return
  501. }
  502. if res, err := ParseWithRuntime("mytest", "GET x where @xxx)", &TestRuntimeProvider{}); err.Error() !=
  503. "Parse error in mytest: Unexpected term ()) (Line:1 Pos:17)" {
  504. t.Error("Unexpected result", res, err)
  505. return
  506. }
  507. if res, err := ParseWithRuntime("mytest", "GET x where @xxx(12", &TestRuntimeProvider{}); err.Error() !=
  508. "Parse error in mytest: Unexpected end (Line:1 Pos:18)" {
  509. t.Error("Unexpected result", res, err)
  510. return
  511. }
  512. if res, err := ParseWithRuntime("mytest", "GET x where @xxx(abc,", &TestRuntimeProvider{}); err.Error() !=
  513. "Parse error in mytest: Unexpected end" {
  514. t.Error("Unexpected result", res, err)
  515. return
  516. }
  517. if res, err := ParseWithRuntime("mytest", "GET x show a AS get", &TestRuntimeProvider{}); err.Error() !=
  518. "Parse error in mytest: Unexpected term (get) (Line:1 Pos:17)" {
  519. t.Error("Unexpected result", res, err)
  520. return
  521. }
  522. if res, err := ParseWithRuntime("mytest", "GET x show x, a FORMAT get", &TestRuntimeProvider{}); err.Error() !=
  523. "Parse error in mytest: Unexpected term (get) (Line:1 Pos:24)" {
  524. t.Error("Unexpected result", res, err)
  525. return
  526. }
  527. if res, err := ParseWithRuntime("mytest", "GET x show @bla(1,", &TestRuntimeProvider{}); err.Error() !=
  528. "Parse error in mytest: Unexpected end" {
  529. t.Error("Unexpected result", res, err)
  530. return
  531. }
  532. if res, err := ParseWithRuntime("mytest", "get a with ordering)", &TestRuntimeProvider{}); err.Error() !=
  533. "Parse error in mytest: Unexpected term ()) (Line:1 Pos:20)" {
  534. t.Error("Unexpected result", res, err)
  535. return
  536. }
  537. if res, err := ParseWithRuntime("mytest", "get a with ordering(=", &TestRuntimeProvider{}); err.Error() !=
  538. "Parse error in mytest: Term cannot start an expression (=) (Line:1 Pos:21)" {
  539. t.Error("Unexpected result", res, err)
  540. return
  541. }
  542. if res, err := ParseWithRuntime("mytest", "get a where [1,2", &TestRuntimeProvider{}); err.Error() !=
  543. "Parse error in mytest: Unexpected end" {
  544. t.Error("Unexpected result", res, err)
  545. return
  546. }
  547. var TokenUnknown LexTokenID = -5
  548. res, err = testParserRun([]LexToken{
  549. {TokenUnknown, 1, "", 1, 1},
  550. {TokenEOF, 1, "", 1, 1},
  551. })
  552. if err.Error() != "Parse error in special test: Unknown term (id:-5 (\"\")) (Line:1 Pos:1)" {
  553. t.Error("Unexpected result", res, err)
  554. return
  555. }
  556. res, err = testParserRun([]LexToken{
  557. {TokenVALUE, 1, "", 1, 1},
  558. {TokenMINUS, 1, "", 1, 1},
  559. {TokenUnknown, 1, "", 1, 1},
  560. {TokenEOF, 1, "", 1, 1},
  561. })
  562. if err.Error() != "Parse error in special test: Unknown term (id:-5 (\"\")) (Line:1 Pos:1)" {
  563. t.Error("Unexpected result", res, err)
  564. return
  565. }
  566. }
  567. func TestAstPlainRepresentation(t *testing.T) {
  568. input := `
  569. get song where true // 'div' show bla wIth orderinG(ASCending aa,Descending bb), FILTERING(ISNOTNULL test2,UNIQUE test3, uniquecount test3), nulltraversal(true)`
  570. expectedOutput := `
  571. get
  572. value: "song"
  573. where
  574. divint
  575. true
  576. value: "div"
  577. show
  578. showterm: "bla"
  579. with
  580. ordering
  581. asc
  582. value: "aa"
  583. desc
  584. value: "bb"
  585. filtering
  586. isnotnull
  587. value: "test2"
  588. unique
  589. value: "test3"
  590. uniquecount
  591. value: "test3"
  592. nulltraversal
  593. true
  594. `[1:]
  595. res, err := Parse("mytest", input)
  596. if err != nil || fmt.Sprint(res) != expectedOutput {
  597. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  598. return
  599. }
  600. plainres := res.Plain()
  601. jsonplainres, err := json.Marshal(plainres)
  602. if err != nil {
  603. t.Error(err)
  604. return
  605. }
  606. out := bytes.Buffer{}
  607. err = json.Indent(&out, []byte(jsonplainres), "", " ")
  608. if err != nil {
  609. t.Error(err)
  610. return
  611. }
  612. if out.String() != `
  613. {
  614. "children": [
  615. {
  616. "name": "value",
  617. "value": "song"
  618. },
  619. {
  620. "children": [
  621. {
  622. "children": [
  623. {
  624. "name": "true",
  625. "value": "true"
  626. },
  627. {
  628. "name": "value",
  629. "value": "div"
  630. }
  631. ],
  632. "name": "divint",
  633. "value": "//"
  634. }
  635. ],
  636. "name": "where",
  637. "value": "where"
  638. },
  639. {
  640. "children": [
  641. {
  642. "name": "showterm",
  643. "value": "bla"
  644. }
  645. ],
  646. "name": "show",
  647. "value": "show"
  648. },
  649. {
  650. "children": [
  651. {
  652. "children": [
  653. {
  654. "children": [
  655. {
  656. "name": "value",
  657. "value": "aa"
  658. }
  659. ],
  660. "name": "asc",
  661. "value": "ASCending"
  662. },
  663. {
  664. "children": [
  665. {
  666. "name": "value",
  667. "value": "bb"
  668. }
  669. ],
  670. "name": "desc",
  671. "value": "Descending"
  672. }
  673. ],
  674. "name": "ordering",
  675. "value": "orderinG"
  676. },
  677. {
  678. "children": [
  679. {
  680. "children": [
  681. {
  682. "name": "value",
  683. "value": "test2"
  684. }
  685. ],
  686. "name": "isnotnull",
  687. "value": "ISNOTNULL"
  688. },
  689. {
  690. "children": [
  691. {
  692. "name": "value",
  693. "value": "test3"
  694. }
  695. ],
  696. "name": "unique",
  697. "value": "UNIQUE"
  698. },
  699. {
  700. "children": [
  701. {
  702. "name": "value",
  703. "value": "test3"
  704. }
  705. ],
  706. "name": "uniquecount",
  707. "value": "uniquecount"
  708. }
  709. ],
  710. "name": "filtering",
  711. "value": "FILTERING"
  712. },
  713. {
  714. "children": [
  715. {
  716. "name": "true",
  717. "value": "true"
  718. }
  719. ],
  720. "name": "nulltraversal",
  721. "value": "nulltraversal"
  722. }
  723. ],
  724. "name": "with",
  725. "value": "wIth"
  726. }
  727. ],
  728. "name": "get",
  729. "value": "get"
  730. }`[1:] {
  731. t.Error("Unexpected result: ", out.String())
  732. return
  733. }
  734. // Now convert the plain ast back into a normal AST and pretty print the result
  735. astfromplain, err := ASTFromPlain(plainres)
  736. if err != nil {
  737. t.Error(err)
  738. return
  739. }
  740. // Check that the generated AST is equal to the expected output
  741. if fmt.Sprint(astfromplain) != expectedOutput {
  742. t.Error("Unexpected output:", astfromplain)
  743. return
  744. }
  745. ppquery, err := PrettyPrint(astfromplain)
  746. if err != nil {
  747. t.Error(err)
  748. return
  749. }
  750. if ppquery != `
  751. get song where true // div
  752. show
  753. bla
  754. with
  755. ordering(ascending aa, descending bb),
  756. filtering(isnotnull test2, unique test3, uniquecount test3),
  757. nulltraversal(true)`[1:] {
  758. t.Error("Unexpected output:", ppquery)
  759. return
  760. }
  761. // Test parsing from JSON (this will produce a []interface{} for children)
  762. data := make(map[string]interface{})
  763. json.NewDecoder(bytes.NewBufferString(`{
  764. "name" : "get",
  765. "value" : "get",
  766. "children" : [{
  767. "name" : "value",
  768. "value" : "bla"
  769. }]
  770. }`)).Decode(&data)
  771. astfromplain, err = ASTFromPlain(data)
  772. if err != nil {
  773. t.Error(err)
  774. return
  775. }
  776. if fmt.Sprint(astfromplain) != `
  777. get
  778. value: "bla"
  779. `[1:] {
  780. t.Error("Unexpected result:", astfromplain)
  781. return
  782. }
  783. // Test error message
  784. if _, err := ASTFromPlain(map[string]interface{}{
  785. "name": "bla",
  786. }); err.Error() != "Found plain ast node without a value: map[name:bla]" {
  787. t.Error("Unexpected error:", err)
  788. return
  789. }
  790. if _, err := ASTFromPlain(map[string]interface{}{
  791. "name": "bla",
  792. "value": "",
  793. "children": []map[string]interface{}{{
  794. "fame": "bla",
  795. }},
  796. }); err.Error() != "Found plain ast node without a name: map[fame:bla]" {
  797. t.Error("Unexpected error:", err)
  798. return
  799. }
  800. }
  801. /*
  802. Special function to test lexer runs which might be prevented by the actual lexer.
  803. */
  804. func testParserRun(tokens []LexToken) (*ASTNode, error) {
  805. // Create channel which is filled with the given lex tokens
  806. tokenChan := make(chan LexToken)
  807. run := func() {
  808. for _, item := range tokens {
  809. tokenChan <- item
  810. }
  811. }
  812. go run()
  813. // Create parser which processes the given tokens
  814. p := &parser{"special test", nil, tokenChan, nil}
  815. node, err := p.next()
  816. if err != nil {
  817. return nil, err
  818. }
  819. p.node = node
  820. return p.run(0)
  821. }