trilium/spec/search/parser.spec.js

137 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-06-03 22:24:41 +08:00
const ParsingContext = require("../../src/services/search/parsing_context.js");
const parser = require('../../src/services/search/parser.js');
2020-05-20 06:03:33 +08:00
describe("Parser", () => {
it("fulltext parser without content", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: ["hello", "hi"],
expressionTokens: [],
parsingContext: new ParsingContext({includeNoteContent: false})
2020-05-21 17:18:15 +08:00
});
2020-05-20 06:03:33 +08:00
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("NoteCacheFulltextExp");
expect(rootExp.tokens).toEqual(["hello", "hi"]);
});
it("fulltext parser with content", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: ["hello", "hi"],
expressionTokens: [],
parsingContext: new ParsingContext({includeNoteContent: true})
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("OrExp");
const [firstSub, secondSub] = rootExp.subExpressions;
expect(firstSub.constructor.name).toEqual("NoteCacheFulltextExp");
expect(firstSub.tokens).toEqual(["hello", "hi"]);
expect(secondSub.constructor.name).toEqual("NoteContentFulltextExp");
expect(secondSub.tokens).toEqual(["hello", "hi"]);
});
it("simple label comparison", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#mylabel", "=", "text"],
parsingContext: new ParsingContext()
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(rootExp.attributeType).toEqual("label");
expect(rootExp.attributeName).toEqual("mylabel");
expect(rootExp.comparator).toBeTruthy();
});
it("simple label AND", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: [],
2020-05-27 06:09:19 +08:00
expressionTokens: ["#first", "=", "text", "and", "#second", "=", "text"],
2020-05-21 17:46:01 +08:00
parsingContext: new ParsingContext(true)
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("AndExp");
const [firstSub, secondSub] = rootExp.subExpressions;
expect(firstSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(secondSub.attributeName).toEqual("second");
});
it("simple label AND without explicit AND", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "#second", "=", "text"],
parsingContext: new ParsingContext()
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("AndExp");
const [firstSub, secondSub] = rootExp.subExpressions;
expect(firstSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(secondSub.attributeName).toEqual("second");
});
it("simple label OR", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "or", "#second", "=", "text"],
parsingContext: new ParsingContext()
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("OrExp");
const [firstSub, secondSub] = rootExp.subExpressions;
expect(firstSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(secondSub.attributeName).toEqual("second");
});
it("fulltext and simple label", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: ["hello"],
expressionTokens: ["#mylabel", "=", "text"],
parsingContext: new ParsingContext()
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("AndExp");
const [firstSub, secondSub] = rootExp.subExpressions;
expect(firstSub.constructor.name).toEqual("NoteCacheFulltextExp");
expect(firstSub.tokens).toEqual(["hello"]);
expect(secondSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(secondSub.attributeName).toEqual("mylabel");
});
it("label sub-expression", () => {
2020-05-21 17:18:15 +08:00
const rootExp = parser({
fulltextTokens: [],
expressionTokens: ["#first", "=", "text", "or", ["#second", "=", "text", "and", "#third", "=", "text"]],
parsingContext: new ParsingContext()
2020-05-21 17:18:15 +08:00
});
2020-05-21 05:20:39 +08:00
expect(rootExp.constructor.name).toEqual("OrExp");
const [firstSub, secondSub] = rootExp.subExpressions;
expect(firstSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(firstSub.attributeName).toEqual("first");
expect(secondSub.constructor.name).toEqual("AndExp");
const [firstSubSub, secondSubSub] = secondSub.subExpressions;
expect(firstSubSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(firstSubSub.attributeName).toEqual("second");
expect(secondSubSub.constructor.name).toEqual("LabelComparisonExp");
2020-05-21 05:20:39 +08:00
expect(secondSubSub.attributeName).toEqual("third");
2020-05-20 06:03:33 +08:00
});
});