-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlibxml-test.spec.ts
261 lines (259 loc) · 10.7 KB
/
libxml-test.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { Libxml } from "../";
import fs from "fs";
describe("Node-Libxml", function () {
it("Should return a list of errored path if dtd path is bad", function () {
let libxml = new Libxml();
libxml.loadDtds(["tests/dtd/mydoctype-not-existing.dtd"]);
expect(libxml).toHaveProperty("dtdsLoadedErrors");
expect(Array.isArray(libxml.dtdsLoadedErrors)).toBe(true);
expect(libxml.dtdsLoadedErrors).toContain(
"tests/dtd/mydoctype-not-existing.dtd"
);
libxml.freeXml();
libxml.freeDtds();
});
// Wellformed & valid
it("Should return wellformed & valid on a wellformed & valid xml", function () {
let libxml = new Libxml();
let testDefaultWf = libxml.loadXml("tests/data/test-default.xml");
libxml.loadDtds(["tests/dtd/mydoctype.dtd"]);
let testDefaultV = libxml.validateAgainstDtds();
expect(testDefaultWf).toBeTruthy;
expect(typeof testDefaultV).toBe("string");
libxml.freeXml();
libxml.freeDtds();
});
it("should return wellformed on a wellformed XMl in utf8-bom & other encoding", function () {
let libxml = new Libxml();
let testNotUtf8 = libxml.loadXml("tests/data/test-default-not-utf8.xml");
expect(testNotUtf8).toBeTruthy;
expect(libxml).not.toHaveProperty("wellformedErrors");
libxml.freeXml();
});
it("should return wellformed on a wellformed XMl in utf8-bom & other encoding FROM STRING", function () {
let libxml = new Libxml();
let defaultNotUtf8 = fs.readFileSync(
"tests/data/test-default-not-utf8.xml",
"utf8"
);
let testNotUtf8 = libxml.loadXmlFromString(defaultNotUtf8);
expect(testNotUtf8).toBeTruthy;
expect(libxml).not.toHaveProperty("wellFormedErrors");
libxml.freeXml();
});
// Wellformed & invalid
it("Should return wellformed & invalid on a wellformed BUT invalid xml", function () {
let libxml = new Libxml();
let testInvalidWf = libxml.loadXml("tests/data/test-not-valid-dtd.xml");
libxml.loadDtds(["tests/dtd/mydoctype.dtd"]);
expect(libxml.getMaxErrorNumber()).toEqual(30);
let testInvalid = libxml.validateAgainstDtds(3);
expect(libxml.getMaxErrorNumber()).toEqual(30);
expect(testInvalidWf).toBeTruthy;
expect(testInvalid).toBeFalsy;
expect(libxml).toHaveProperty("validationDtdErrors");
expect(typeof libxml.validationDtdErrors).toBe("object");
expect(
libxml.validationDtdErrors?.["tests/dtd/mydoctype.dtd"].length
).toEqual(3);
libxml.freeDtds();
libxml.freeXml();
});
it("Should return wellformed & invalid on a wellformed BUT invalid xml FROM STRING XML", function () {
let libxml = new Libxml();
let testInvalidWfStr = fs.readFileSync(
"tests/data/test-not-valid-dtd.xml",
"utf8"
);
let testInvalidWf = libxml.loadXmlFromString(testInvalidWfStr);
libxml.loadDtds(["tests/dtd/mydoctype.dtd"]);
libxml.setMaxErrorNumber(30);
expect(libxml.getMaxErrorNumber()).toEqual(30);
let testInvalid = libxml.validateAgainstDtds(3);
expect(libxml.getMaxErrorNumber()).toEqual(30);
expect(testInvalidWf).toBeTruthy;
expect(testInvalid).toBeFalsy;
expect(libxml).toHaveProperty("validationDtdErrors");
expect(typeof libxml.validationDtdErrors).toBe("object");
expect(
libxml.validationDtdErrors?.["tests/dtd/mydoctype.dtd"].length
).toEqual(3);
libxml.freeDtds();
libxml.freeXml();
});
// not wellformed
it("Should return Not Wellformed & invalid on a not wellformed xml", function () {
let libxml = new Libxml();
let wellformed = libxml.loadXml("tests/data/test-not-wellformed.xml");
libxml.loadDtds(["tests/dtd/mydoctype.dtd"]);
let wellformedV = libxml.validateAgainstDtds();
expect(wellformed).toBeFalsy;
expect(wellformedV).toBeFalsy;
expect(libxml).toHaveProperty("wellformedErrors");
expect(Array.isArray(libxml.wellformedErrors)).toBe(true);
libxml.freeXml();
expect(libxml).not.toHaveProperty("wellformedErrors");
});
it("Should return an object containing DTD", function () {
let libxml = new Libxml();
let loaded = libxml.loadXml("tests/data/test-default.xml");
expect(loaded).toBeTruthy;
let currentDtd = libxml.getDtd();
expect(typeof currentDtd).toBe("object");
expect(currentDtd).toHaveProperty("name", "article");
expect(currentDtd).toHaveProperty("externalId", "my doctype of doom");
expect(currentDtd).toHaveProperty("systemId", "mydoctype.dtd");
libxml.freeXml();
});
it("Should return correct xpath values", function () {
let libxml = new Libxml();
let xmlfile = libxml.loadXml("tests/data/test-default.xml");
expect(xmlfile).toBeTruthy;
let info = libxml.xpathSelect("string(//infos)"),
infodust = libxml.xpathSelect("number(//infosdust)"),
attrib = libxml.xpathSelect("string(//infos/@attrib)"),
infoExist = libxml.xpathSelect("boolean(//infos)"),
info23NotExist = libxml.xpathSelect("boolean(//infos23)"),
xpathToMyExist = libxml.xpathSelect("boolean(/xpath/to/my)"),
nbElementsInMy = libxml.xpathSelect("count(//my//*)"),
nbElementsInXpath = libxml.xpathSelect("count(/xpath//*)");
expect(info).toContain("trezaq");
expect(infodust).toEqual(23);
expect(attrib).toContain("example");
expect(typeof infoExist).toBe("boolean");
expect(infoExist).toBeTruthy;
expect(typeof info23NotExist).toBe("boolean");
expect(info23NotExist).toBeFalsy;
expect(typeof xpathToMyExist).toBe("boolean");
expect(xpathToMyExist).toBeTruthy;
expect(typeof nbElementsInMy).toBe("number");
expect(nbElementsInMy).toEqual(2);
expect(typeof nbElementsInXpath).toBe("number");
expect(nbElementsInXpath).toEqual(4);
libxml.freeXml();
});
// // // ABOVE IS ALL THE SAME WITH MEMORY MANUAL MANAGEMENT
it("should free XML memory & infos when asked in manual mod On not wellformed xml", function () {
let libxml = new Libxml();
let wellformed = libxml.loadXml("tests/data/test-not-wellformed.xml");
expect(wellformed).toBeFalsy;
expect(libxml).toHaveProperty("wellformedErrors");
expect(Array.isArray(libxml.wellformedErrors)).toBe(true);
expect(libxml.wellformedErrors?.length).toBeGreaterThanOrEqual(1);
libxml.freeXml();
expect(libxml).not.toHaveProperty("wellformedErrors");
});
it("should not crash when freeUp memory xml multiple time!", function () {
let libxml = new Libxml();
let wellFormed = libxml.loadXml("tests/data/test-default.xml");
expect(wellFormed).toBeTruthy;
expect(libxml).not.toHaveProperty("wellformedErrors");
libxml.freeXml();
libxml.freeXml();
libxml.freeXml();
expect(libxml).not.toHaveProperty("wellformedErrors");
});
it("Should return intended values after multiple CLEAN!", function () {
let libxml = new Libxml();
let wellformed = libxml.loadXml("tests/data/test-not-valid-dtd.xml");
libxml.loadDtds(["tests/dtd/mydoctype.dtd"]);
let wellformedV = libxml.validateAgainstDtds();
expect(wellformed).toBeTruthy;
expect(wellformedV).toBeFalsy;
expect(libxml).not.toHaveProperty("wellformedErrors");
libxml.freeXml();
libxml.freeDtds();
libxml.freeDtds();
wellformed = libxml.loadXml("tests/data/test-default.xml");
libxml.loadDtds(["tests/dtd/mydoctype.dtd", "tests/dtd/myBADdoctype.dtd"]);
wellformedV = libxml.validateAgainstDtds();
expect(wellformed).toBeTruthy;
expect(typeof wellformedV).toBe("string");
expect(wellformedV).toEqual("tests/dtd/mydoctype.dtd");
expect(libxml).not.toHaveProperty("wellformedErrors");
});
it("should not crash when loads multiple dtd in two rounds", function () {
let libxml = new Libxml();
let wellformed = libxml.loadXml("tests/data/test-not-valid-dtd.xml");
libxml.loadDtds(["tests/dtd/mydoctype.dtd", "tests/dtd/myBADdoctype.dtd"]);
libxml.loadDtds([
"tests/dtd/mydoctype2.dtd",
"tests/dtd/myBADdoctype2.dtd",
]);
let wellformedV = libxml.validateAgainstDtds();
expect(wellformed).toBeTruthy;
expect(wellformedV).toBeFalsy;
expect(libxml).not.toHaveProperty("wellformedErrors");
expect(libxml).toHaveProperty("validationDtdErrors");
libxml.validationDtdErrors?.["tests/dtd/mydoctype.dtd"];
expect(
libxml.validationDtdErrors?.["tests/dtd/mydoctype.dtd"]
).toHaveLength(5);
expect(
libxml.validationDtdErrors?.["tests/dtd/myBADdoctype.dtd"]
).toHaveLength(5);
expect(
libxml.validationDtdErrors?.["tests/dtd/myBADdoctype2.dtd"]
).toHaveLength(5);
expect(
libxml.validationDtdErrors?.["tests/dtd/mydoctype2.dtd"]
).toHaveLength(5);
libxml.freeXml();
libxml.freeDtds();
});
it("Should return wellformed & valid on a wellformed & valid xml SCHEMA", function () {
let libxml = new Libxml();
let testDefaultWf = libxml.loadXml("tests/data/test-valid-schema.xml");
libxml.loadSchemas(["tests/xsd/MARC21slim.xsd"]);
let testDefaultV = libxml.validateAgainstSchemas();
expect(testDefaultWf).toBeTruthy;
expect(typeof testDefaultV).toBe("string");
libxml.freeXml();
libxml.freeSchemas();
});
it("Should return errors for invalid against schema", function () {
let libxml = new Libxml();
let testDefaultWf = libxml.loadXml("tests/data/test-valid-schema.xml");
libxml.loadSchemas(["tests/xsd/MARC21slim-bad.xsd"]);
let testDefaultV = libxml.validateAgainstSchemas();
expect(testDefaultWf).toBeTruthy;
expect(testDefaultV).toBeFalsy;
expect(libxml).toHaveProperty("validationSchemaErrors");
expect(typeof libxml.validationSchemaErrors).toBe("object");
expect(libxml.validationSchemaErrors?.["tests/xsd/MARC21slim-bad.xsd"])
.toBeTruthy;
expect(
Array.isArray(
libxml.validationSchemaErrors?.["tests/xsd/MARC21slim-bad.xsd"]
)
).toBe(true);
expect(
libxml.validationSchemaErrors?.["tests/xsd/MARC21slim-bad.xsd"][0]
).toHaveProperty("column");
expect(
libxml.validationSchemaErrors?.["tests/xsd/MARC21slim-bad.xsd"][0]
).toHaveProperty("line");
expect(
libxml.validationSchemaErrors?.["tests/xsd/MARC21slim-bad.xsd"][0]
).toHaveProperty("message");
libxml.freeXml();
libxml.freeSchemas();
});
it("Should return expected value and not crash when multiple clean of schemas", function () {
let libxml = new Libxml();
let testDefaultWf = libxml.loadXml("tests/data/test-valid-schema.xml");
libxml.loadSchemas([
"tests/xsd/MARC21slim-bad.xsd",
"tests/xsd/MARC21slim.xsd",
]);
let testDefaultV = libxml.validateAgainstSchemas();
expect(testDefaultWf).toBeTruthy;
expect(typeof testDefaultV).toBe("string");
libxml.freeXml();
libxml.freeSchemas();
});
it("Should not throw error when use clearAll function to clear all memory libxml", function () {
let libxml = new Libxml();
libxml.clearAll();
});
});