Notes/spec-es6/attribute_parser.spec.js

106 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-06-03 17:28:57 +02:00
import attributeParser from '../src/public/app/services/attribute_parser.js';
import {describe, it, expect, execute} from './mini_test.js';
2020-07-09 23:59:27 +02:00
describe("Preprocessor", () => {
it("relation with value", () => {
expect(attributeParser.preprocess('<p>~relation&nbsp;= <a class="reference-link" href="#root/RclIpMauTOKS/NFi2gL4xtPxM" some-attr="abc" data-note-path="root/RclIpMauTOKS/NFi2gL4xtPxM">note</a>&nbsp;</p>'))
.toEqual("~relation = #root/RclIpMauTOKS/NFi2gL4xtPxM ");
});
});
2020-06-04 00:04:57 +02:00
describe("Lexer", () => {
2020-06-03 17:28:57 +02:00
it("simple label", () => {
expect(attributeParser.lexer("#label").map(t => t.text))
.toEqual(["#label"]);
2020-06-03 17:28:57 +02:00
});
2020-06-04 00:04:57 +02:00
2020-07-13 23:27:23 +02:00
it("inherited label", () => {
expect(attributeParser.lexer("#label(inheritable)").map(t => t.text))
.toEqual(["#label", "(", "inheritable", ")"]);
expect(attributeParser.lexer("#label ( inheritable ) ").map(t => t.text))
.toEqual(["#label", "(", "inheritable", ")"]);
});
2020-06-04 00:04:57 +02:00
it("label with value", () => {
expect(attributeParser.lexer("#label=Hallo").map(t => t.text))
.toEqual(["#label", "=", "Hallo"]);
2020-06-04 00:04:57 +02:00
});
2020-06-06 12:56:24 +02:00
it("label with value", () => {
const tokens = attributeParser.lexer("#label=Hallo");
expect(tokens[0].startIndex).toEqual(0);
expect(tokens[0].endIndex).toEqual(5);
});
2020-06-04 00:04:57 +02:00
it("relation with value", () => {
2020-07-09 23:59:27 +02:00
expect(attributeParser.lexer('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map(t => t.text))
.toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
2020-06-04 00:04:57 +02:00
});
it("use quotes to define value", () => {
expect(attributeParser.lexer("#'label a'='hello\"` world'").map(t => t.text))
2020-06-04 00:04:57 +02:00
.toEqual(["#label a", "=", 'hello"` world']);
expect(attributeParser.lexer('#"label a" = "hello\'` world"').map(t => t.text))
2020-06-04 00:04:57 +02:00
.toEqual(["#label a", "=", "hello'` world"]);
expect(attributeParser.lexer('#`label a` = `hello\'" world`').map(t => t.text))
2020-06-04 00:04:57 +02:00
.toEqual(["#label a", "=", "hello'\" world"]);
});
});
describe("Parser", () => {
it("simple label", () => {
const attrs = attributeParser.parser(["#token"].map(t => ({text: t})));
2020-06-04 00:04:57 +02:00
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
expect(attrs[0].name).toEqual('token');
2020-07-13 23:27:23 +02:00
expect(attrs[0].isInheritable).toBeFalsy();
expect(attrs[0].value).toBeFalsy();
});
it("inherited label", () => {
const attrs = attributeParser.parser(["#token", "(", "inheritable", ")"].map(t => ({text: t})));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
expect(attrs[0].name).toEqual('token');
expect(attrs[0].isInheritable).toBeTruthy();
2020-06-04 00:04:57 +02:00
expect(attrs[0].value).toBeFalsy();
});
it("label with value", () => {
const attrs = attributeParser.parser(["#token", "=", "val"].map(t => ({text: t})));
2020-06-04 00:04:57 +02:00
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
expect(attrs[0].name).toEqual('token');
expect(attrs[0].value).toEqual("val");
});
it("relation", () => {
let attrs = attributeParser.parser(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map(t => ({text: t})));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('relation');
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual('NFi2gL4xtPxM');
attrs = attributeParser.parser(["~token", "=", "#NFi2gL4xtPxM"].map(t => ({text: t})));
2020-06-04 00:04:57 +02:00
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('relation');
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual('NFi2gL4xtPxM');
2020-06-04 00:04:57 +02:00
});
2020-07-13 23:27:23 +02:00
// it("error cases", () => {
// expect(() => attributeParser.parser(["~token"].map(t => ({text: t})), "~token"))
// .toThrow('Relation "~token" should point to a note.');
// });
2020-06-03 17:28:57 +02:00
});
execute();