-
Notifications
You must be signed in to change notification settings - Fork 76
/
attribute_parser.spec.ts
112 lines (87 loc) · 4.15 KB
/
attribute_parser.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as attributeParser from '../src/public/app/services/attribute_parser.js';
import {describe, it, expect, execute} from './mini_test.js';
describe("Lexing", () => {
it("simple label", () => {
expect(attributeParser.lex("#label").map((t: any) => t.text))
.toEqual(["#label"]);
});
it("simple label with trailing spaces", () => {
expect(attributeParser.lex(" #label ").map((t: any) => t.text))
.toEqual(["#label"]);
});
it("inherited label", () => {
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text))
.toEqual(["#label", "(", "inheritable", ")"]);
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text))
.toEqual(["#label", "(", "inheritable", ")"]);
});
it("label with value", () => {
expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text))
.toEqual(["#label", "=", "Hallo"]);
});
it("label with value", () => {
const tokens = attributeParser.lex("#label=Hallo");
expect(tokens[0].startIndex).toEqual(0);
expect(tokens[0].endIndex).toEqual(5);
});
it("relation with value", () => {
expect(attributeParser.lex('~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM').map((t: any) => t.text))
.toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
});
it("use quotes to define value", () => {
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text))
.toEqual(["#label a", "=", 'hello"` world']);
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text))
.toEqual(["#label a", "=", "hello'` world"]);
expect(attributeParser.lex('#`label a` = `hello\'" world`').map((t: any) => t.text))
.toEqual(["#label a", "=", "hello'\" world"]);
});
});
describe("Parser", () => {
it("simple label", () => {
const attrs = attributeParser.parse(["#token"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
expect(attrs[0].name).toEqual('token');
expect(attrs[0].isInheritable).toBeFalsy();
expect(attrs[0].value).toBeFalsy();
});
it("inherited label", () => {
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({text: t})));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual('label');
expect(attrs[0].name).toEqual('token');
expect(attrs[0].isInheritable).toBeTruthy();
expect(attrs[0].value).toBeFalsy();
});
it("label with value", () => {
const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({text: t})));
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.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');
attrs = attributeParser.parse(["~token", "=", "#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');
});
});
describe("error cases", () => {
it("error cases", () => {
expect(() => attributeParser.lexAndParse('~token'))
.toThrow('Relation "~token" in "~token" should point to a note.');
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.`);
});
});
execute();