Notes/spec-es6/attribute_parser.spec.ts

113 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2024-06-09 12:16:09 +02:00
import * as attributeParser from '../src/public/app/services/attribute_parser.js';
2024-07-24 20:31:26 +03: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", () => {
2024-06-09 12:16:09 +02:00
expect(attributeParser.lex("#label").map((t: any) => t.text))
.toEqual(["#label"]);
});
it("simple label with trailing spaces", () => {
2024-06-09 12:16:09 +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", () => {
2024-06-09 12:16:09 +02:00
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text))
2020-07-13 23:27:23 +02:00
.toEqual(["#label", "(", "inheritable", ")"]);
2024-06-09 12:16:09 +02:00
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text))
2020-07-13 23:27:23 +02:00
.toEqual(["#label", "(", "inheritable", ")"]);
});
2020-06-04 00:04:57 +02:00
it("label with value", () => {
2024-06-09 12:16:09 +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", () => {
2024-06-09 12:16:09 +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", () => {
2024-06-09 12:16:09 +02:00
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text))
2020-06-04 00:04:57 +02:00
.toEqual(["#label a", "=", 'hello"` world']);
2024-06-09 12:16:09 +02:00
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text))
2020-06-04 00:04:57 +02:00
.toEqual(["#label a", "=", "hello'` world"]);
2024-06-09 12:16:09 +02:00
expect(attributeParser.lex('#`label a` = `hello\'" world`').map((t: any) => t.text))
2020-06-04 00:04:57 +02:00
.toEqual(["#label a", "=", "hello'\" world"]);
});
});
describe("Parser", () => {
it("simple label", () => {
2024-06-09 12:16:09 +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);
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", () => {
2024-06-09 12:16:09 +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);
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", () => {
2024-06-09 12:16:09 +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);
expect(attrs[0].type).toEqual('label');
expect(attrs[0].name).toEqual('token');
expect(attrs[0].value).toEqual("val");
});
it("relation", () => {
2024-06-09 12:16:09 +02:00
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({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');
2024-06-09 12:16:09 +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);
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
});
});
describe("error cases", () => {
it("error cases", () => {
expect(() => attributeParser.lexAndParse('~token'))
.toThrow('Relation "~token" in "~token" should point to a note.');
2020-06-04 00:04:57 +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.`);
expect(() => attributeParser.lexAndParse("#"))
.toThrow(`Attribute name is empty, please fill the name.`);
});
2020-06-03 17:28:57 +02:00
});
execute();