trilium/spec/search/lexer.spec.js

101 lines
4 KiB
JavaScript
Raw Normal View History

2020-07-21 06:01:07 +08:00
const lex = require('../../src/services/search/services/lex.js');
2020-05-18 01:43:37 +08:00
2020-05-18 05:14:24 +08:00
describe("Lexer fulltext", () => {
2020-05-18 01:43:37 +08:00
it("simple lexing", () => {
2020-07-21 06:01:07 +08:00
expect(lex("hello world").fulltextTokens.map(t => t.token))
2020-05-18 01:43:37 +08:00
.toEqual(["hello", "world"]);
});
it("use quotes to keep words together", () => {
2020-07-21 06:01:07 +08:00
expect(lex("'hello world' my friend").fulltextTokens.map(t => t.token))
2020-05-18 01:43:37 +08:00
.toEqual(["hello world", "my", "friend"]);
2020-07-21 06:01:07 +08:00
expect(lex('"hello world" my friend').fulltextTokens.map(t => t.token))
2020-05-18 01:43:37 +08:00
.toEqual(["hello world", "my", "friend"]);
2020-07-21 06:01:07 +08:00
expect(lex('`hello world` my friend').fulltextTokens.map(t => t.token))
2020-05-18 01:43:37 +08:00
.toEqual(["hello world", "my", "friend"]);
});
it("you can use different quotes and other special characters inside quotes", () => {
2020-07-21 06:01:07 +08:00
expect(lex("'i can use \" or ` or #~=*' without problem").fulltextTokens.map(t => t.token))
.toEqual(["i can use \" or ` or #~=*", "without", "problem"]);
2020-05-18 01:43:37 +08:00
});
it("if quote is not ended then it's just one long token", () => {
2020-07-21 06:01:07 +08:00
expect(lex("'unfinished quote").fulltextTokens.map(t => t.token))
2020-05-18 01:43:37 +08:00
.toEqual(["unfinished quote"]);
});
2020-05-18 05:14:24 +08:00
it("parenthesis and symbols in fulltext section are just normal characters", () => {
2020-07-21 06:01:07 +08:00
expect(lex("what's u=p <b(r*t)h>").fulltextTokens.map(t => t.token))
2020-05-18 05:14:24 +08:00
.toEqual(["what's", "u=p", "<b(r*t)h>"]);
});
it("escaping special characters", () => {
2020-07-21 06:01:07 +08:00
expect(lex("hello \\#\\~\\'").fulltextTokens.map(t => t.token))
.toEqual(["hello", "#~'"]);
2020-05-18 05:14:24 +08:00
});
});
describe("Lexer expression", () => {
it("simple attribute existence", () => {
2020-07-21 06:01:07 +08:00
expect(lex("#label ~relation").expressionTokens.map(t => t.token))
.toEqual(["#label", "~relation"]);
2020-05-18 05:14:24 +08:00
});
it("simple label operators", () => {
2020-07-21 06:01:07 +08:00
expect(lex("#label*=*text").expressionTokens.map(t => t.token))
2020-05-18 05:14:24 +08:00
.toEqual(["#label", "*=*", "text"]);
});
2020-07-20 05:23:48 +08:00
it("simple label operator with in quotes and without", () => {
2020-07-21 06:01:07 +08:00
expect(lex("#label*=*'text'").expressionTokens)
2020-07-20 05:23:48 +08:00
.toEqual([
{token: "#label", inQuotes: false},
{token: "*=*", inQuotes: false},
{token: "text", inQuotes: true}
]);
2020-07-21 06:01:07 +08:00
expect(lex("#label*=*text").expressionTokens)
2020-07-20 05:23:48 +08:00
.toEqual([
{token: "#label", inQuotes: false},
{token: "*=*", inQuotes: false},
{token: "text", inQuotes: false}
]);
});
2020-05-18 05:14:24 +08:00
it("complex expressions with and, or and parenthesis", () => {
2020-07-21 06:01:07 +08:00
expect(lex(`# (#label=text OR #second=text) AND ~relation`).expressionTokens.map(t => t.token))
.toEqual(["#", "(", "#label", "=", "text", "or", "#second", "=", "text", ")", "and", "~relation"]);
});
it("dot separated properties", () => {
2020-07-21 06:01:07 +08:00
expect(lex(`# ~author.title = 'Hugh Howey' AND note.'book title' = 'Silo'`).expressionTokens.map(t => t.token))
2020-05-24 04:18:06 +08:00
.toEqual(["#", "~author", ".", "title", "=", "hugh howey", "and", "note", ".", "book title", "=", "silo"]);
2020-05-18 05:14:24 +08:00
});
2020-05-27 06:09:19 +08:00
2020-07-19 21:25:24 +08:00
it("negation of label and relation", () => {
2020-07-21 06:01:07 +08:00
expect(lex(`#!capital ~!neighbor`).expressionTokens.map(t => t.token))
2020-07-19 21:25:24 +08:00
.toEqual(["#!capital", "~!neighbor"]);
});
2020-05-27 06:09:19 +08:00
it("negation of sub-expression", () => {
2020-07-21 06:01:07 +08:00
expect(lex(`# not(#capital) and note.noteId != "root"`).expressionTokens.map(t => t.token))
2020-05-27 06:09:19 +08:00
.toEqual(["#", "not", "(", "#capital", ")", "and", "note", ".", "noteid", "!=", "root"]);
});
2020-05-18 01:43:37 +08:00
});
2020-07-20 05:19:45 +08:00
describe("Lexer invalid queries and edge cases", () => {
it("concatenated attributes", () => {
2020-07-21 06:01:07 +08:00
expect(lex("#label~relation").expressionTokens.map(t => t.token))
2020-07-20 05:19:45 +08:00
.toEqual(["#label", "~relation"]);
});
it("spaces in attribute names and values", () => {
// invalid but should be reported by parser as an error
2020-07-21 06:01:07 +08:00
expect(lex(`#'long label'="hello o' world" ~'long relation'`).expressionTokens.map(t => t.token))
2020-07-20 05:19:45 +08:00
.toEqual(["#long label", "=", "hello o' world", "~long relation"]);
});
});