Notes/spec-es6/attribute_parser.spec.ts

101 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-01-09 18:07:02 +02:00
import * as attributeParser from "../src/public/app/services/attribute_parser.js";
2024-06-09 12:16:09 +02:00
2025-01-09 18:07:02 +02:00
import { describe, it, expect, execute } from "./mini_test.js";
2020-06-03 17:28:57 +02:00
describe("Lexing", () => {
2020-06-03 17:28:57 +02:00
it("simple label", () => {
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("#label").map((t: any) => t.text)).toEqual(["#label"]);
});
it("simple label with trailing spaces", () => {
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex(" #label ").map((t: any) => 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", () => {
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);
2020-07-13 23:27:23 +02:00
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);
2020-07-13 23:27:23 +02:00
});
2020-06-04 00:04:57 +02:00
it("label with value", () => {
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("#label=Hallo").map((t: any) => 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.lex("#label=Hallo");
2020-06-06 12:56:24 +02:00
expect(tokens[0].startIndex).toEqual(0);
expect(tokens[0].endIndex).toEqual(5);
});
2020-06-04 00:04:57 +02:00
it("relation with value", () => {
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM").map((t: any) => t.text)).toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
2020-06-04 00:04:57 +02:00
});
it("use quotes to define value", () => {
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text)).toEqual(["#label a", "=", 'hello"` world']);
2020-06-04 00:04:57 +02:00
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text)).toEqual(["#label a", "=", "hello'` world"]);
2020-06-04 00:04:57 +02:00
2025-01-09 18:07:02 +02:00
expect(attributeParser.lex("#`label a` = `hello'\" world`").map((t: any) => t.text)).toEqual(["#label a", "=", "hello'\" world"]);
2020-06-04 00:04:57 +02:00
});
});
describe("Parser", () => {
it("simple label", () => {
2025-01-09 18:07:02 +02:00
const attrs = attributeParser.parse(["#token"].map((t: any) => ({ text: t })));
2020-06-04 00:04:57 +02:00
expect(attrs.length).toEqual(1);
2025-01-09 18:07:02 +02:00
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", () => {
2025-01-09 18:07:02 +02:00
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({ text: t })));
2020-07-13 23:27:23 +02:00
expect(attrs.length).toEqual(1);
2025-01-09 18:07:02 +02:00
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
2020-07-13 23:27:23 +02:00
expect(attrs[0].isInheritable).toBeTruthy();
2020-06-04 00:04:57 +02:00
expect(attrs[0].value).toBeFalsy();
});
it("label with value", () => {
2025-01-09 18:07:02 +02:00
const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({ text: t })));
2020-06-04 00:04:57 +02:00
expect(attrs.length).toEqual(1);
2025-01-09 18:07:02 +02:00
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
2020-06-04 00:04:57 +02:00
expect(attrs[0].value).toEqual("val");
});
it("relation", () => {
2025-01-09 18:07:02 +02:00
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
2025-01-09 18:07:02 +02:00
expect(attrs[0].type).toEqual("relation");
expect(attrs[0].name).toEqual("token");
2025-01-09 18:07:02 +02:00
expect(attrs[0].value).toEqual("NFi2gL4xtPxM");
2025-01-09 18:07:02 +02:00
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({ text: t })));
2020-06-04 00:04:57 +02:00
expect(attrs.length).toEqual(1);
2025-01-09 18:07:02 +02:00
expect(attrs[0].type).toEqual("relation");
2020-06-04 00:04:57 +02:00
expect(attrs[0].name).toEqual("token");
2025-01-09 18:07:02 +02:00
expect(attrs[0].value).toEqual("NFi2gL4xtPxM");
2020-06-04 00:04:57 +02:00
});
});
describe("error cases", () => {
it("error cases", () => {
2025-01-09 18:07:02 +02:00
expect(() => attributeParser.lexAndParse("~token")).toThrow('Relation "~token" in "~token" should point to a note.');
2020-06-04 00:04:57 +02:00
2025-01-09 18:07:02 +02:00
expect(() => attributeParser.lexAndParse("#a&b/s")).toThrow(`Attribute name "a&b/s" contains disallowed characters, only alphanumeric characters, colon and underscore are allowed.`);
2025-01-09 18:07:02 +02:00
expect(() => attributeParser.lexAndParse("#")).toThrow(`Attribute name is empty, please fill the name.`);
});
2020-06-03 17:28:57 +02:00
});
execute();