ngx-i18nsupport-lib
Version:
A Typescript library to work with Angular generated i18n files (xliff, xmb)
82 lines • 4.23 kB
JavaScript
"use strict";
/**
* Created by martin on 04.08.2018.
* Testcases for the XmlSerializer.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var xmldom_1 = require("xmldom");
var xml_serializer_1 = require("./xml-serializer");
var assert_1 = require("assert");
describe('XmlSerializer test spec', function () {
var serializer;
/**
* Helper. Parse an XML string.
* @param xmlstring
*/
function parseXmlString(xmlstring) {
return new xmldom_1.DOMParser().parseFromString(xmlstring);
}
beforeEach(function () {
serializer = new xml_serializer_1.XmlSerializer();
});
it("should serialize a simple document without any changes in output", function () {
var doc1string = "<test><elem>a test</elem></test>";
var doc1 = parseXmlString(doc1string);
var serializedDoc = serializer.serializeToString(doc1);
expect(serializedDoc).toEqual(doc1string);
});
it("should serialize a complex document with attributes etc. without any changes in output", function () {
var doc1string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\">\n <file source-language=\"en\" datatype=\"plaintext\" original=\"ng2.template\">\n </file>\n</xliff>";
var doc1 = parseXmlString(doc1string);
var serializedDoc = serializer.serializeToString(doc1);
expect(serializedDoc).toEqual(doc1string);
});
it("should beautify output using 2 spaces for indentation", function () {
var doc1string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\"><y>a simple pcdata element</y></x>";
var doc1 = parseXmlString(doc1string);
var beautifyOptions = {
beautify: true
};
var serializedDoc = serializer.serializeToString(doc1, beautifyOptions);
var expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\">\n <y>a simple pcdata element</y>\n</x>";
expect(serializedDoc).toEqual(expectedResult);
});
it("should beautify output using e.g. tab for indentation", function () {
var doc1string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\"><y>a simple pcdata element</y></x>";
var doc1 = parseXmlString(doc1string);
var beautifyOptions = {
beautify: true,
indentString: '\t'
};
var serializedDoc = serializer.serializeToString(doc1, beautifyOptions);
var expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\">\n\t<y>a simple pcdata element</y>\n</x>";
expect(serializedDoc).toEqual(expectedResult);
});
it("should throw an error if a non whitespace char is used for indentation", function () {
var doc1string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\"><y>a simple pcdata element</y></x>";
var doc1 = parseXmlString(doc1string);
var beautifyOptions = {
beautify: true,
indentString: '\tx'
};
try {
serializer.serializeToString(doc1, beautifyOptions);
assert_1.fail('oops, error expected here');
}
catch (err) {
expect(err.message).toBe('indentString must not contain non white characters');
}
});
it("should beautify output with mixed content", function () {
var doc1string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\"><y>a <b><it>mixed</it> content</b> element</y></x>";
var doc1 = parseXmlString(doc1string);
var beautifyOptions = {
beautify: true,
mixedContentElements: ['y']
};
var serializedDoc = serializer.serializeToString(doc1, beautifyOptions);
var expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x a=\"1\" b=\"&\">\n <y>a <b><it>mixed</it> content</b> element</y>\n</x>";
expect(serializedDoc).toEqual(expectedResult);
});
});
//# sourceMappingURL=S:/experimente/ngx-i18nsupport-lib/src/impl/xml-serializer.spec.js.map