diff --git a/spec/validator_spec.js b/spec/validator_spec.js
index dcf41a17..7a964dc9 100644
--- a/spec/validator_spec.js
+++ b/spec/validator_spec.js
@@ -1,5 +1,7 @@
"use strict";
+const fs = require("fs");
+const path = require("path");
const validator = require("../src/validator");
function validate(xmlData, error, line = 1) {
@@ -10,363 +12,218 @@ function validate(xmlData, error, line = 1) {
msg: Object.values(error)[0],
line
};
+ // console.log(JSON.stringify(result.err,null,4));
expect(result.err).toEqual(expected);
} else {
expect(result).toBe(true);
}
}
+function validateFile(fileName, ...args) {
+ const fileNamePath = path.join(__dirname, "assets/" + fileName);
+ validate(fs.readFileSync(fileNamePath).toString(), ...args);
+}
+
describe("XMLParser", function () {
it("should validate simple xml string", function () {
- let xmlData = "";
-
- let result = validator.validate(xmlData);
- expect(result).toBe(true);
-
- xmlData = ``;
-
- result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate("");
+ validate(``);
});
it("should not validate invalid starting tag", function () {
- const xmlData = "< rootNode>";
- const expected = {
- "code": "InvalidTag",
- "msg": "There is an unnecessary space between tag name and backward slash ' ..'.",
- "line": 1
- };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("< rootNode>", {
+ InvalidTag: "There is an unnecessary space between tag name and backward slash ' ..'."
+ });
});
it("should not validate incomplete xml string", function () {
- const xmlData = "";
- const expected = {
- "code": "InvalidXml",
- "msg": "Invalid '[ \"rootNode\"]' found.",
- "line": 1
- };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("", {
+ InvalidXml: "Invalid '[ \"rootNode\"]' found."
+ });
});
- it("should not validate invalid starting tag for following characters", function() {
- const xmlData = "";
- const expected = {
- "code": "InvalidTag",
- "msg": "Tag 'rootNode#@aa' is an invalid name.",
- "line": 1
- };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ it("should not validate invalid starting tag for following characters", function () {
+ validate("", {
+ InvalidTag: "Tag 'rootNode#@aa' is an invalid name."
+ });
});
- it("should return false for non xml text", function() {
- const xmlData = "rootNode";
- const expected = { code: "InvalidChar", msg: "char 'r' is not expected.", line: 1 };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result, null, 4));
- expect(result).toEqual(expected);
+ it("should return false for non xml text", function () {
+ validate("rootNode", {
+ InvalidChar: "char 'r' is not expected."
+ });
});
- it("should validate self closing tags", function() {
- const xmlData = "texttext";
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ it("should validate self closing tags", function () {
+ validate("texttext");
});
- it("should not consider these as self closing tags", function() {
- let xmlData = "";
- let expected = {code: "InvalidAttr", msg: "boolean attribute 'tag' is not allowed.", line: 1};
-
- let result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
-
-
- xmlData = "";
- expected = {code: "InvalidAttr", msg: "Attribute '/' has no space in starting.", line: 1};
-
- result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ it("should not consider these as self closing tags", function () {
+ validate("", {
+ InvalidAttr: "boolean attribute 'tag' is not allowed."
+ });
+ validate("", {
+ InvalidAttr: "Attribute '/' has no space in starting."
+ });
});
it("should not validate xml string when closing tag is different", function () {
- const xmlData = "";
- const expected = { code: "InvalidTag", msg: "Closing tag 'rootNode' is expected inplace of 'rootnode'.", line: 1 };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("", {
+ InvalidTag: "Closing tag 'rootNode' is expected inplace of 'rootnode'."
+ });
});
it("should not validate xml string when closing tag is invalid", function () {
- let xmlData = "< /rootnode>";
-
- let expected = { code: "InvalidTag", msg: "There is an unnecessary space between tag name and backward slash ' ..'.", line: 1 };
-
- let result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
-
- xmlData = " rootnode>";
- expected = { code: "InvalidTag", msg: "There is an unnecessary space between tag name and backward slash ' ..'.", line: 1 };
- result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
-
- xmlData = "";
- expected = { code: "InvalidTag", msg: "Closing tag 'rootnode' can't have attributes or invalid starting.", line: 1 };
- result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("< /rootnode>", {
+ InvalidTag: "There is an unnecessary space between tag name and backward slash ' ..'."
+ });
+ validate(" rootnode>", {
+ InvalidTag: "There is an unnecessary space between tag name and backward slash ' ..'."
+ });
+ validate("", {
+ InvalidTag: "Closing tag 'rootnode' can't have attributes or invalid starting."
+ });
});
it("should validate simple xml string with namespace", function () {
- const xmlData = "";
-
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate("");
});
it("should not validate xml string with namespace when closing tag is diffrent", function () {
- const xmlData = "";
- const expected = { code: "InvalidTag", msg: "Closing tag 'root:Node' is expected inplace of 'root:node'.", line: 1 };
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("", {
+ InvalidTag: "Closing tag 'root:Node' is expected inplace of 'root:node'."
+ });
});
it("should validate simple xml string with value", function () {
- const xmlData = "some value";
-
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate("some value");
});
it("should not validate simple xml string with value but not matching closing tag", function () {
- const xmlData = "some value";
- const expected = { code: "InvalidTag", msg: "Closing tag 'root:Node' is expected inplace of 'root'.", line: 1 };
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("some value", {
+ InvalidTag: "Closing tag 'root:Node' is expected inplace of 'root'."
+ });
});
it("should not validate simple xml string with value but no closing tag", function () {
- const xmlData = "some value";
- const expected = { code: "InvalidXml", msg: "Invalid '[ \"root:Node\"]' found.", line: 1 };
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("some value", {
+ InvalidXml: "Invalid '[ \"root:Node\"]' found."
+ });
});
it("should validate xml with nested tags", function () {
- const xmlData = "1val";
-
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate("1val");
});
it("should not validate xml with wrongly nested tags", function () {
- const xmlData = "1val";
- const expected = { code: "InvalidTag", msg: "Closing tag 'tag1' is expected inplace of 'tag'.", line: 1 };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("1val", {
+ InvalidTag: "Closing tag 'tag1' is expected inplace of 'tag'."
+ });
});
it("should validate xml with comment", function () {
- const xmlData = "1val";
-
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate("1val");
});
it("should validate xml with comment", function () {
- const xmlData = "1val";
-
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate("1val");
});
- it("should not validate xml with comment in a open tag", function() {
- const xmlData = " -- -->>1val";
- const expected = {code: "InvalidTag", msg: "Tag 'rootNode>1val", {
+ InvalidTag: "Tag 'rootNode >1val";
- const expected = {code: "InvalidAttr", msg: "boolean attribute ' >1val", {
+ InvalidAttr: "boolean attribute '" +
"" +
"" +
"]>" +
- "Hello World.";
-
- const result = validator.validate(xmlData);
- expect(result).toBe(true);
+ "Hello World.");
});
it("should fail for XML with ! which is not a comment, DOCTYPE or CDATA", function () {
- const xmlData = "!bla>";
- const expected = {
- "code": "InvalidTag",
- "msg": "Tag '!bla' is an invalid name.",
- "line": 1
- };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate("!bla>", {
+ InvalidTag: "Tag '!bla' is an invalid name."
+ });
});
it("should not validate XML when prolog doesn't start from 1st char", function () {
- const xmlData = " Hello World.";
- const expected = {
- "code": "InvalidXml",
- "msg": "XML declaration allowed only at the start of the document.",
- "line": 1
- };
-
- const result = validator.validate(xmlData).err;
- //console.log(JSON.stringify(result,null,4));
- expect(result).toEqual(expected);
+ validate(" Hello World.", {
+ InvalidXml: "XML declaration allowed only at the start of the document."
+ });
});
it("should not validate XML with prolog only", function () {
- const xmlData = "" +
- "";
- const expected = {
- "code": "InvalidXml",
- "msg": "Start tag expected.",
- line: 1
-
- };
- const result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ validate("", {
+ InvalidXml: "Start tag expected."
+ });
});
it("should not validate XML with prolog & DOCTYPE but not any other tag", function () {
- const xmlData = "" +
+ validate("" +
"" +
"" +
"" +
"" +
- "]>";
- const expected = {
- "code": "InvalidXml",
- "msg": "Start tag expected.",
- line: 1
- };
- const result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ "]>", {
+ InvalidXml: "Start tag expected."
+ });
});
it("should validate XML PIs", function () {
- var xmlData = ''
- + ''
- + ''
- + '';
-
- var result = validator.validate(xmlData);
- expect(result).toBe(true);
+ validate('' +
+ '' +
+ '' +
+ '');
});
it("should not validate XML PIs with invalid values", function () {
- var xmlData = ''
- + '" ?>'
- + ''
- + '';
-
-
- var expected = { code: 'InvalidChar', msg: 'char \'"\' is not expected.', line: 1 }
-
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ validate('' +
+ '" ?>' +
+ '' +
+ '', {
+ InvalidChar: "char '\"' is not expected."
+ });
});
it('should validate xml with a "length" attribute', function () {
- const xmlData = '';
-
- var result = validator.validate(xmlData);
- expect(result).toEqual(true);
+ validate('');
});
it("should not validate xml with repeated attributes", function () {
- const xmlData = '';
-
- var expected = { code: 'InvalidAttr', msg: "Attribute 'length' is repeated.", line: 1 }
-
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ validate('', {
+ InvalidAttr: "Attribute 'length' is repeated."
+ });
});
it('should validate xml with a tag attribute splitted on more lines', () => {
- const xmlData = `
+ validate(`
- `;
-
- var result = validator.validate(xmlData);
- expect(result).toEqual(true);
+ `);
});
it('should validate xml not properly closed', () => {
- const xmlData = `
+ validate(`
{
- const xmlData = `
- `;
-
- const expected = { code: "InvalidAttr", msg: "boolean attribute 't' is not allowed.", line: 1 };
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ `, {
+ InvalidAttr: "boolean attribute 't' is not allowed."
+ });
});
it('should detect error line when having multiple attributes 2', () => {
- const xmlData = `
- `;
-
- const expected = { code: "InvalidAttr", msg: "boolean attribute 't' is not allowed.", line: 5 };
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ `, {
+ InvalidAttr: "boolean attribute 't' is not allowed."
+ }, 5);
});
it('should validate value with ampersand', function () {
@@ -545,33 +351,26 @@ attribute2="attribute2"
});
describe("should not validate XML documents with multiple root nodes", () => {
-
it('when root nodes are repeated', () => {
- const xmlData = ``;
-
- const expected = { code: "InvalidXml", msg: 'Multiple possible root nodes found.', line: 1};
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ validate(``, {
+ InvalidXml: 'Multiple possible root nodes found.'
+ });
});
it('when root nodes are different', () => {
- const xmlData = '';
-
- const expected = { code: "InvalidXml", msg: 'Multiple possible root nodes found.', line: 1};
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ validate('', {
+ InvalidXml: 'Multiple possible root nodes found.'
+ });
});
it('when root nodes have more nested tags', () => {
- const xmlData = `
+ validate(`
- `;
-
- const expected = { code: "InvalidXml", msg: 'Multiple possible root nodes found.', line: 5};
- var result = validator.validate(xmlData).err;
- expect(result).toEqual(expected);
+ `, {
+ InvalidXml: 'Multiple possible root nodes found.'
+ }, 5);
});
-});
+});
\ No newline at end of file