forked from azu/gitbook-plugin-include-codeblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-test.js
205 lines (201 loc) · 8.95 KB
/
parser-test.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// LICENSE : MIT
"use strict";
import assert from "power-assert";
import { defaultKeyValueMap } from "../src/options.js";
import {
containIncludeCommand,
splitLabelToCommands,
strip,
parseValue,
parseVariablesFromLabel
} from "../src/parser";
const kvmap = defaultKeyValueMap;
describe("parse", function() {
describe("#splitLabelToCommands", function() {
it("should split label to commands", function() {
const commands = splitLabelToCommands("import");
assert.equal(commands.length, 1);
assert.equal(commands[0], "import");
});
it("should not contain space in command", function() {
const commands = splitLabelToCommands("command1 command2, command3 ");
// Should 3 but 4..
assert(commands.length > 3);
assert(commands.indexOf("command1") !== -1);
assert(commands.indexOf("command2") !== -1);
assert(commands.indexOf("command3") !== -1);
});
});
describe("containIncludeLabel", function() {
it("support import", function() {
const commands = splitLabelToCommands("import");
assert(containIncludeCommand(commands));
});
it("support include", function() {
const commands = splitLabelToCommands("include");
assert(containIncludeCommand(commands));
});
it("support command split by space", function() {
const commands = splitLabelToCommands("import title");
assert(containIncludeCommand(commands));
});
it("support command split by ,", function() {
const commands = splitLabelToCommands("import title");
assert(containIncludeCommand(commands));
});
});
describe("parseValue", function() {
it("should unescape string parameter", function() {
const result = parseValue(
'"\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/' +
"\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~" +
'&<>AA"',
"string",
""
);
assert.equal(result, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~&<>AA");
});
it("should backslash unescape commonmark defined characters only", function() {
const result = parseValue('"\\r\\n\\[\\]"', "string", "");
assert.equal(result, "\\r\\n[]"); // \r and \n should not be unescaped.
});
it("should validate markers", function() {
let result = parseValue('" marker0 , marker1 "', "string", "marker");
assert.equal(result, " marker0 , marker1 ");
result = parseValue('"~invalid~"', "string", "marker");
assert.equal(result, undefined);
});
it("should parse boolean values", function() {
let result = parseValue("true", "boolean", "");
assert.equal(result, true);
result = parseValue('"true"', "boolean", "");
assert.equal(result, true);
result = parseValue("'true'", "boolean", "");
assert.equal(result, true);
result = parseValue("false", "boolean", "");
assert.equal(result, false);
result = parseValue('"false"', "boolean", "");
assert.equal(result, false);
result = parseValue("'false'", "boolean", "");
assert.equal(result, false);
});
});
describe("parseVariablesFromLabel ", function() {
it("should retrieve edit boolean", function() {
const resmap = parseVariablesFromLabel(kvmap, "include,edit:true");
const results = resmap;
assert.equal(results.edit, true);
assert.equal(results.marker, "");
});
it("should retrieve edit boolean with quotes", function() {
const resmap = parseVariablesFromLabel(kvmap, 'include,edit:"true"');
const results = resmap;
assert.equal(results.edit, true);
assert.equal(results.marker, "");
});
it("should retrieve string title", function() {
const resmap = parseVariablesFromLabel(kvmap, 'include,title:"a test"');
const results = resmap;
assert.equal(results.title, "a test");
assert.equal(results.marker, "");
});
it("should retrieve include marker", function() {
const resmap = parseVariablesFromLabel(kvmap, '[include:"marker0"](/path/to/file.ext)');
const results = resmap;
assert.equal(results.marker, "marker0");
});
it("should retrieve import marker", function() {
const resmap = parseVariablesFromLabel(kvmap, '[import:"marker0"](/path/to/file.ext)');
const results = resmap;
assert.equal(results.marker, "marker0");
});
it("should retrieve include marker with title", function() {
const resmap = parseVariablesFromLabel(
kvmap,
'[include:"marker0",title:"test"](/path/to/file.ext)'
);
const results = resmap;
assert.equal(results.marker, "marker0");
assert.equal(results.title, "test");
});
it("should retrieve import multi markers", function() {
const resmap = parseVariablesFromLabel(
kvmap,
'[import:"marker0,marker1,marker2"](/path/to/file.ext)'
);
const results = resmap;
assert.equal(results.marker, "marker0,marker1,marker2");
});
it("should retrieve each attribute", function() {
const resmap = parseVariablesFromLabel(
kvmap,
'include:"marker",title:"a test",id:"code1",class:"myclass",edit=false,check="true",theme:"monokai",template:"ace",unindent:true,fixlang:"false"'
);
const results = resmap;
assert.equal(results.marker, "marker");
assert.equal(results.title, "a test");
assert.equal(results.id, "code1");
assert.equal(results.class, "myclass");
assert.equal(results.edit, false);
assert.equal(results.check, true);
assert.equal(results.theme, "monokai");
assert.equal(results.template, "ace");
assert.equal(results.unindent, true);
assert.equal(results.fixlang, false);
});
it("should retrieve nothing", function() {
const resmap = parseVariablesFromLabel(kvmap, "[import](/path/to/file.ext)");
const results = resmap;
assert.equal(results.marker, "");
});
it("should handle characters for string parameter", function() {
const resmap = parseVariablesFromLabel(
kvmap,
'import,title="test+with-special*string"'
);
const results = resmap;
assert.equal(results.title, "test+with-special*string");
});
it("should unescape string parameter", function() {
const resmap = parseVariablesFromLabel(
kvmap,
'import,title="\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/' +
"\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~" +
'&<>AA"'
);
const results = resmap;
assert.equal(results.title, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~&<>AA");
});
it("should backslash unescape commonmark defined characters only", function() {
const resmap = parseVariablesFromLabel(kvmap, 'import,title="\\r\\n\\[\\]"');
const results = resmap;
assert.equal(results.title, "\\r\\n[]"); // \r and \n should not be unescaped.
});
it("should not parse string argument into another key-value pair", function() {
assert.equal(kvmap.edit, false);
const resmap = parseVariablesFromLabel(kvmap, 'import,title="edit:true"');
const results = resmap;
assert.equal(results.edit, false);
assert.equal(results.title, "edit:true");
});
});
// inspired from https://github.com/rails/rails/blob/master/activesupport/test/core_ext/string_ext_test.rb
describe("strip", function() {
it("strips leading space from empty string", function() {
const stripped = strip("");
assert.equal(stripped, "");
});
it("strips leading space from one-liner", function() {
const stripped = strip(" x");
assert.equal(stripped, "x");
});
it("strips leading space from multi-liner with no margin", function() {
const stripped = strip("foo\n bar\nbaz\n");
assert.equal(stripped, "foo\n bar\nbaz\n");
});
it("strips leading space from multi-liner", function() {
const stripped = strip(" foo\n bar\n baz\n");
assert.equal(stripped, "foo\n bar\nbaz\n");
});
});
});