ngx-i18nsupport-lib
Version:
A Typescript library to work with Angular generated i18n files (xliff, xmb)
161 lines • 11.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var xliff_message_parser_1 = require("./xliff-message-parser");
/**
* Created by martin on 17.05.2017.
* Testcases for parsing normalized messages to XLIFF 1.2 and vive versa.
*/
describe('message parse XLIFF 1.2 test spec', function () {
/**
* Helperfunction to create a parsed message from normalized string.
* @param normalizedString
* @param sourceMessage
* @return {ParsedMessage}
*/
function parsedMessageFor(normalizedString, sourceMessage) {
var parser = new xliff_message_parser_1.XliffMessageParser();
return parser.parseNormalizedString(normalizedString, sourceMessage);
}
/**
* Helperfunction to create a parsed message from native xml.
* @param xmlContent
* @param sourceMessage
* @return {ParsedMessage}
*/
function parsedMessageFromXML(xmlContent, sourceMessage) {
var parser = new xliff_message_parser_1.XliffMessageParser();
return parser.createNormalizedMessageFromXMLString(xmlContent, sourceMessage);
}
/**
* create normalized message from string, then create one from generated xml.
* Check that it is the same.
* @param normalizedMessage
*/
function checkToXmlAndBack(normalizedMessage) {
var xml = parsedMessageFor(normalizedMessage).asNativeString();
expect(parsedMessageFromXML('<source>' + xml + '</source>').asDisplayString()).toBe(normalizedMessage);
}
describe('normalized message to xml', function () {
it('should parse plain text', function () {
var normalizedMessage = 'a text without anything special';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe(normalizedMessage);
});
it('should parse text with placeholder', function () {
var normalizedMessage = 'a placeholder: {{0}}';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a placeholder: <x id="INTERPOLATION"/>');
checkToXmlAndBack(normalizedMessage);
});
it('should parse text with 2 placeholders', function () {
var normalizedMessage = '{{1}}: a placeholder: {{0}}';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('<x id="INTERPOLATION_1"/>: a placeholder: <x id="INTERPOLATION"/>');
checkToXmlAndBack(normalizedMessage);
});
it('should parse simple bold tag', function () {
var normalizedMessage = 'a text <b>with</b> a bold text';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a text <x id="START_BOLD_TEXT" ctype="x-b" equiv-text="<b>"/>with<x id="CLOSE_BOLD_TEXT" ctype="x-b"/> a bold text');
checkToXmlAndBack(normalizedMessage);
});
it('should parse simple italic tag', function () {
var normalizedMessage = 'a text <i>with</i> emphasis';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a text <x id="START_ITALIC_TEXT" ctype="x-i" equiv-text="<i>"/>with<x id="CLOSE_ITALIC_TEXT" ctype="x-i"/> emphasis');
checkToXmlAndBack(normalizedMessage);
});
it('should parse unknown tag', function () {
var normalizedMessage = 'a text with <strange>strange emphasis</strange>';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a text with <x id="START_TAG_STRANGE" ctype="x-strange" equiv-text="<strange>"/>strange emphasis<x id="CLOSE_TAG_STRANGE" ctype="x-strange"/>');
checkToXmlAndBack(normalizedMessage);
});
it('should parse embedded tags with placeholder inside', function () {
var normalizedMessage = '<b><i><strange>Placeholder {{0}}</strange></i></b>';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('<x id="START_BOLD_TEXT" ctype="x-b" equiv-text="<b>"/><x id="START_ITALIC_TEXT" ctype="x-i" equiv-text="<i>"/><x id="START_TAG_STRANGE" ctype="x-strange" equiv-text="<strange>"/>Placeholder <x id="INTERPOLATION"/><x id="CLOSE_TAG_STRANGE" ctype="x-strange"/><x id="CLOSE_ITALIC_TEXT" ctype="x-i"/><x id="CLOSE_BOLD_TEXT" ctype="x-b"/>');
checkToXmlAndBack(normalizedMessage);
});
it('should parse empty tag like <br>', function () {
var normalizedMessage = 'one line<br>second line';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('one line<x id="LINE_BREAK" ctype="lb" equiv-text="<br/>"/>second line');
checkToXmlAndBack(normalizedMessage);
});
it('should parse ICU Refs', function () {
var normalizedMessage = 'a text with <ICU-Message-Ref_0/>';
var parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a text with <x id="ICU"/>');
checkToXmlAndBack(normalizedMessage);
});
});
describe('xml to normalized message', function () {
it('should parse simple text content', function () {
var parsedMessage = parsedMessageFromXML('a simple content');
expect(parsedMessage.asDisplayString()).toBe('a simple content');
});
it('should parse strange tag with placeholder content', function () {
var parsedMessage = parsedMessageFromXML('Diese Nachricht ist <x id="START_TAG_STRANGE" ctype="x-strange"/><x id="INTERPOLATION"/><x id="CLOSE_TAG_STRANGE" ctype="x-strange"/>');
expect(parsedMessage.asDisplayString()).toBe('Diese Nachricht ist <strange>{{0}}</strange>');
});
it('should parse embedded tags', function () {
var parsedMessage = parsedMessageFromXML('Diese Nachricht ist <x id="START_BOLD_TEXT" ctype="x-b"/><x id="START_TAG_STRANGE" ctype="x-strange"/>SEHR WICHTIG<x id="CLOSE_TAG_STRANGE" ctype="x-strange"/><x id="CLOSE_BOLD_TEXT" ctype="x-b"/>');
expect(parsedMessage.asDisplayString()).toBe('Diese Nachricht ist <b><strange>SEHR WICHTIG</strange></b>');
});
it('should parse complex message with embedded placeholder', function () {
var parsedMessage = parsedMessageFromXML('<x id="START_LINK" ctype="x-a"/>link1 with placeholder <x id="INTERPOLATION"/><x id="CLOSE_LINK" ctype="x-a"/>');
expect(parsedMessage.asDisplayString()).toBe('<a>link1 with placeholder {{0}}</a>');
});
it('should throw an error due to not well formed elements <b><strange></b>', function () {
try {
var parsedMessage = parsedMessageFromXML('Diese Nachricht ist falsch geschachtelt: <x id="START_BOLD_TEXT" ctype="x-b"/><x id="START_TAG_STRANGE" ctype="x-strange"/>FALSCH<x id="CLOSE_BOLD_TEXT" ctype="x-b"/><x id="CLOSE_TAG_STRANGE" ctype="x-strange"/>');
expect('parsedMessage').toBe('should throw an error');
}
catch (e) {
expect(e.message).toContain('unexpected close tag b');
}
});
it('should parse message with embedded ICU message reference', function () {
var parsedMessage = parsedMessageFromXML('first: <x id="ICU"/>');
expect(parsedMessage.asDisplayString()).toBe('first: <ICU-Message-Ref_0/>');
});
it('should parse message with 2 embedded ICU message reference', function () {
var parsedMessage = parsedMessageFromXML('first: <x id="ICU"/>, second <x id="ICU_1"/>');
expect(parsedMessage.asDisplayString()).toBe('first: <ICU-Message-Ref_0/>, second <ICU-Message-Ref_1/>');
});
it('should set correct placeholer index (issue #84 ngx-i18nsupport) ', function () {
var messageWith2Indexes = 'New <x id="START_TAG_XY" ctype="x-xy" equiv-text="<xy>"/><x id="CLOSE_TAG_XY" ctype="x-xy" equiv-text="</xy>"/> was reported by <x id="START_TAG_XY_1" ctype="x-xy" equiv-text="<xy>"/><x id="CLOSE_TAG_XY" ctype="x-xy" equiv-text="</xy>"/>';
var parsedMessage = parsedMessageFromXML(messageWith2Indexes);
var normalizedMessageString = parsedMessage.asDisplayString();
expect(normalizedMessageString).toBe('New <xy></xy> was reported by <xy id="1"></xy>');
var translatedMessage = parsedMessage.translate('New <xy></xy> was reported by <xy id="1"></xy>');
expect(translatedMessage.asNativeString()).toBe('New <x id="START_TAG_XY" ctype="x-xy" equiv-text="<xy>"/><x id="CLOSE_TAG_XY" ctype="x-xy"/> was reported by <x id="START_TAG_XY_1" ctype="x-xy" equiv-text="<xy>"/><x id="CLOSE_TAG_XY" ctype="x-xy"/>');
});
it('should parse simple plural ICU message', function () {
var parsedMessage = parsedMessageFromXML('{VAR_PLURAL, plural, =0 {just now} =1 {one minute ago} other {a few minutes ago} }');
expect(parsedMessage.asDisplayString()).toBe('<ICU-Message/>');
expect(parsedMessage.getICUMessage()).toBeTruthy();
var icuMessage = parsedMessage.getICUMessage();
expect(icuMessage.getCategories().length).toBe(3);
});
it('should parse plural ICU message with placeholder', function () {
var parsedMessage = parsedMessageFromXML('{VAR_PLURAL, plural, =0 {just now} =1 {one minute ago} other {<x id="INTERPOLATION" equiv-text="{{minutes}}"/> minutes ago} }');
expect(parsedMessage.asDisplayString()).toBe('<ICU-Message/>');
expect(parsedMessage.getICUMessage()).toBeTruthy();
var icuMessage = parsedMessage.getICUMessage();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[2].getMessageNormalized().asDisplayString()).toBe('{{0}} minutes ago');
});
});
});
//# sourceMappingURL=S:/experimente/ngx-i18nsupport-lib/src/impl/xliff-message-parser.spec.js.map