import {XliffMessageParser} from './xliff-message-parser';
import {ParsedMessage} from './parsed-message';
import {DOMParser} from 'xmldom';
import {INormalizedMessage} from '../api/index';
/**
* 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', () => {
/**
* Helperfunction to create a parsed message from normalized string.
* @param normalizedString normalizedString
* @param sourceMessage sourceMessage
* @return ParsedMessage
*/
function parsedMessageFor(normalizedString: string, sourceMessage?: ParsedMessage): ParsedMessage {
const parser = new XliffMessageParser();
return parser.parseNormalizedString(normalizedString, sourceMessage);
}
/**
* Helperfunction to create a parsed message from native xml.
* @param xmlContent xmlContent
* @param sourceMessage sourceMessage
* @return ParsedMessage
*/
function parsedMessageFromXML(xmlContent: string, sourceMessage?: ParsedMessage): ParsedMessage {
const parser = new 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 normalizedMessage
*/
function checkToXmlAndBack(normalizedMessage: string) {
const xml = parsedMessageFor(normalizedMessage).asNativeString();
expect(parsedMessageFromXML('' + xml + '').asDisplayString()).toBe(normalizedMessage);
}
describe('normalized message to xml', () => {
it('should parse plain text', () => {
const normalizedMessage = 'a text without anything special';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe(normalizedMessage);
});
it('should parse text with placeholder', () => {
const normalizedMessage = 'a placeholder: {{0}}';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a placeholder: ');
checkToXmlAndBack(normalizedMessage);
});
it('should parse text with 2 placeholders', () => {
const normalizedMessage = '{{1}}: a placeholder: {{0}}';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe(': a placeholder: ');
checkToXmlAndBack(normalizedMessage);
});
it('should parse simple bold tag', () => {
const normalizedMessage = 'a text with a bold text';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString())
.toBe('a text ' +
'with a bold text');
checkToXmlAndBack(normalizedMessage);
});
it('should parse simple italic tag', () => {
const normalizedMessage = 'a text with emphasis';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString())
.toBe('a text ' +
'with emphasis');
checkToXmlAndBack(normalizedMessage);
});
it('should parse unknown tag', () => {
const normalizedMessage = 'a text with strange emphasis';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString())
.toBe('a text with ' +
'strange emphasis');
checkToXmlAndBack(normalizedMessage);
});
it('should parse embedded tags with placeholder inside', () => {
const normalizedMessage = 'Placeholder {{0}}';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString())
.toBe('' +
'Placeholder ');
checkToXmlAndBack(normalizedMessage);
});
it('should parse empty tag like
', () => {
const normalizedMessage = 'one line
second line';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('one linesecond line');
checkToXmlAndBack(normalizedMessage);
});
it('should parse ICU Refs', () => {
const normalizedMessage = 'a text with ';
const parsedMessage = parsedMessageFor(normalizedMessage);
expect(parsedMessage.asDisplayString()).toBe(normalizedMessage);
expect(parsedMessage.asNativeString()).toBe('a text with ');
checkToXmlAndBack(normalizedMessage);
});
});
describe('xml to normalized message', () => {
it('should parse simple text content', () => {
const parsedMessage = parsedMessageFromXML('a simple content');
expect(parsedMessage.asDisplayString()).toBe('a simple content');
});
it('should parse strange tag with placeholder content', () => {
const parsedMessage = parsedMessageFromXML('Diese Nachricht ist ' +
'');
expect(parsedMessage.asDisplayString()).toBe('Diese Nachricht ist {{0}}');
});
it('should parse embedded tags', () => {
const parsedMessage = parsedMessageFromXML('Diese Nachricht ist ' +
'SEHR WICHTIG');
expect(parsedMessage.asDisplayString()).toBe('Diese Nachricht ist SEHR WICHTIG');
});
it('should parse complex message with embedded placeholder', () => {
const parsedMessage = parsedMessageFromXML('link1 with placeholder ' +
'');
expect(parsedMessage.asDisplayString()).toBe('link1 with placeholder {{0}}');
});
it('should throw an error due to not well formed elements ', () => {
try {
const parsedMessage = parsedMessageFromXML('Diese Nachricht ist falsch geschachtelt: ' +
'' +
'FALSCH');
expect(parsedMessage.toString()).toBe('should throw an error');
} catch (e) {
expect(e.message).toContain('unexpected close tag b');
}
});
it('should parse message with embedded ICU message reference', () => {
const parsedMessage = parsedMessageFromXML('first: ');
expect(parsedMessage.asDisplayString()).toBe('first: ');
});
it('should parse message with 2 embedded ICU message reference', () => {
const parsedMessage = parsedMessageFromXML('first: , second ');
expect(parsedMessage.asDisplayString()).toBe('first: , second ');
});
it('should set correct placeholer index (issue #84 ngx-i18nsupport) ', () => {
const messageWith2Indexes = 'New ' +
' was reported by ';
const parsedMessage = parsedMessageFromXML(messageWith2Indexes);
const normalizedMessageString = parsedMessage.asDisplayString();
expect(normalizedMessageString).toBe('New was reported by ');
const translatedMessage: INormalizedMessage = parsedMessage.translate('New was reported by ');
expect(translatedMessage.asNativeString()).toBe('New ' +
' was reported by ');
});
it('should parse simple plural ICU message', () => {
const parsedMessage =
parsedMessageFromXML('{VAR_PLURAL, plural, =0 {just now} =1 {one minute ago} other {a few minutes ago} }');
expect(parsedMessage.asDisplayString()).toBe('');
expect(parsedMessage.getICUMessage()).toBeTruthy();
const icuMessage = parsedMessage.getICUMessage();
expect(icuMessage.getCategories().length).toBe(3);
});
it('should parse plural ICU message with placeholder', () => {
const parsedMessage = parsedMessageFromXML('{VAR_PLURAL, plural, =0 {just now} =1 {one minute ago}' +
' other { minutes ago} }');
expect(parsedMessage.asDisplayString()).toBe('');
expect(parsedMessage.getICUMessage()).toBeTruthy();
const icuMessage = parsedMessage.getICUMessage();
expect(icuMessage.getCategories().length).toBe(3);
expect(icuMessage.getCategories()[2].getMessageNormalized().asDisplayString()).toBe('{{0}} minutes ago');
});
});
});