lexer.spec.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const lex = require('../../src/services/search/services/lex');
  2. describe("Lexer fulltext", () => {
  3. it("simple lexing", () => {
  4. expect(lex("hello world").fulltextTokens.map(t => t.token))
  5. .toEqual(["hello", "world"]);
  6. });
  7. it("use quotes to keep words together", () => {
  8. expect(lex("'hello world' my friend").fulltextTokens.map(t => t.token))
  9. .toEqual(["hello world", "my", "friend"]);
  10. expect(lex('"hello world" my friend').fulltextTokens.map(t => t.token))
  11. .toEqual(["hello world", "my", "friend"]);
  12. expect(lex('`hello world` my friend').fulltextTokens.map(t => t.token))
  13. .toEqual(["hello world", "my", "friend"]);
  14. });
  15. it("you can use different quotes and other special characters inside quotes", () => {
  16. expect(lex("'i can use \" or ` or #~=*' without problem").fulltextTokens.map(t => t.token))
  17. .toEqual(["i can use \" or ` or #~=*", "without", "problem"]);
  18. });
  19. it("I can use backslash to escape quotes", () => {
  20. expect(lex("hello \\\"world\\\"").fulltextTokens.map(t => t.token))
  21. .toEqual(["hello", '"world"']);
  22. expect(lex("hello \\\'world\\\'").fulltextTokens.map(t => t.token))
  23. .toEqual(["hello", "'world'"]);
  24. expect(lex("hello \\\`world\\\`").fulltextTokens.map(t => t.token))
  25. .toEqual(["hello", '`world`']);
  26. expect(lex('"hello \\\"world\\\"').fulltextTokens.map(t => t.token))
  27. .toEqual(['hello "world"']);
  28. expect(lex("'hello \\\'world\\\''").fulltextTokens.map(t => t.token))
  29. .toEqual(["hello 'world'"]);
  30. expect(lex("`hello \\\`world\\\``").fulltextTokens.map(t => t.token))
  31. .toEqual(["hello `world`"]);
  32. expect(lex("\\#token").fulltextTokens.map(t => t.token))
  33. .toEqual(["#token"]);
  34. });
  35. it("quote inside a word does not have a special meaning", () => {
  36. const lexResult = lex("d'Artagnan is dead #hero = d'Artagnan");
  37. expect(lexResult.fulltextTokens.map(t => t.token))
  38. .toEqual(["d'artagnan", "is", "dead"]);
  39. expect(lexResult.expressionTokens.map(t => t.token))
  40. .toEqual(['#hero', '=', "d'artagnan"]);
  41. });
  42. it("if quote is not ended then it's just one long token", () => {
  43. expect(lex("'unfinished quote").fulltextTokens.map(t => t.token))
  44. .toEqual(["unfinished quote"]);
  45. });
  46. it("parenthesis and symbols in fulltext section are just normal characters", () => {
  47. expect(lex("what's u=p <b(r*t)h>").fulltextTokens.map(t => t.token))
  48. .toEqual(["what's", "u=p", "<b(r*t)h>"]);
  49. });
  50. it("operator characters in expressions are separate tokens", () => {
  51. expect(lex("# abc+=-def**-+d").expressionTokens.map(t => t.token))
  52. .toEqual(["#", "abc", "+=-", "def", "**-+", "d"]);
  53. });
  54. it("escaping special characters", () => {
  55. expect(lex("hello \\#\\~\\'").fulltextTokens.map(t => t.token))
  56. .toEqual(["hello", "#~'"]);
  57. });
  58. });
  59. describe("Lexer expression", () => {
  60. it("simple attribute existence", () => {
  61. expect(lex("#label ~relation").expressionTokens.map(t => t.token))
  62. .toEqual(["#label", "~relation"]);
  63. });
  64. it("simple label operators", () => {
  65. expect(lex("#label*=*text").expressionTokens.map(t => t.token))
  66. .toEqual(["#label", "*=*", "text"]);
  67. });
  68. it("simple label operator with in quotes", () => {
  69. expect(lex("#label*=*'text'").expressionTokens)
  70. .toEqual([
  71. {token: "#label", inQuotes: false, startIndex: 0, endIndex: 5},
  72. {token: "*=*", inQuotes: false, startIndex: 6, endIndex: 8},
  73. {token: "text", inQuotes: true, startIndex: 10, endIndex: 13}
  74. ]);
  75. });
  76. it("simple label operator with param without quotes", () => {
  77. expect(lex("#label*=*text").expressionTokens)
  78. .toEqual([
  79. {token: "#label", inQuotes: false, startIndex: 0, endIndex: 5},
  80. {token: "*=*", inQuotes: false, startIndex: 6, endIndex: 8},
  81. {token: "text", inQuotes: false, startIndex: 9, endIndex: 12}
  82. ]);
  83. });
  84. it("simple label operator with empty string param", () => {
  85. expect(lex("#label = ''").expressionTokens)
  86. .toEqual([
  87. {token: "#label", inQuotes: false, startIndex: 0, endIndex: 5},
  88. {token: "=", inQuotes: false, startIndex: 7, endIndex: 7},
  89. // weird case for empty strings which ends up with endIndex < startIndex :-(
  90. {token: "", inQuotes: true, startIndex: 10, endIndex: 9}
  91. ]);
  92. });
  93. it("note. prefix also separates fulltext from expression", () => {
  94. expect(lex(`hello fulltext note.labels.capital = Prague`).expressionTokens.map(t => t.token))
  95. .toEqual(["note", ".", "labels", ".", "capital", "=", "prague"]);
  96. });
  97. it("note. prefix in quotes will note start expression", () => {
  98. expect(lex(`hello fulltext "note.txt"`).expressionTokens.map(t => t.token))
  99. .toEqual([]);
  100. expect(lex(`hello fulltext "note.txt"`).fulltextTokens.map(t => t.token))
  101. .toEqual(["hello", "fulltext", "note.txt"]);
  102. });
  103. it("complex expressions with and, or and parenthesis", () => {
  104. expect(lex(`# (#label=text OR #second=text) AND ~relation`).expressionTokens.map(t => t.token))
  105. .toEqual(["#", "(", "#label", "=", "text", "or", "#second", "=", "text", ")", "and", "~relation"]);
  106. });
  107. it("dot separated properties", () => {
  108. expect(lex(`# ~author.title = 'Hugh Howey' AND note.'book title' = 'Silo'`).expressionTokens.map(t => t.token))
  109. .toEqual(["#", "~author", ".", "title", "=", "hugh howey", "and", "note", ".", "book title", "=", "silo"]);
  110. });
  111. it("negation of label and relation", () => {
  112. expect(lex(`#!capital ~!neighbor`).expressionTokens.map(t => t.token))
  113. .toEqual(["#!capital", "~!neighbor"]);
  114. });
  115. it("negation of sub-expression", () => {
  116. expect(lex(`# not(#capital) and note.noteId != "root"`).expressionTokens.map(t => t.token))
  117. .toEqual(["#", "not", "(", "#capital", ")", "and", "note", ".", "noteid", "!=", "root"]);
  118. });
  119. });
  120. describe("Lexer invalid queries and edge cases", () => {
  121. it("concatenated attributes", () => {
  122. expect(lex("#label~relation").expressionTokens.map(t => t.token))
  123. .toEqual(["#label", "~relation"]);
  124. });
  125. it("trailing escape \\", () => {
  126. expect(lex('abc \\').fulltextTokens.map(t => t.token))
  127. .toEqual(["abc", "\\"]);
  128. });
  129. });