import {XmbMessageParser} from './xmb-message-parser';
import {ParsedMessage} from './parsed-message';
/**
* Created by martin on 17.05.2017.
* Testcases for parsing normalized messages to XMB format and vive versa.
*/
describe('message parse XMB 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 XmbMessageParser();
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 XmbMessageParser();
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: INTERPOLATION');
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('INTERPOLATION_1' +
': a placeholder: INTERPOLATION');
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 <b>' +
'with</b> 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 <i>' +
'with</i> 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>' +
'strange emphasis</strange>');
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('<b><i>' +
'<strange>Placeholder ' +
'INTERPOLATION</strange>' +
'</i></b>');
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 ICU');
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 ' +
'<strange>INTERPOLATION' +
'</strange>');
expect(parsedMessage.asDisplayString()).toBe('Diese Nachricht ist {{0}}');
});
it('should parse embedded tags', () => {
const parsedMessage = parsedMessageFromXML('Diese Nachricht ist ' +
'<b><strange>SEHR WICHTIG' +
'</strange></b>');
expect(parsedMessage.asDisplayString()).toBe('Diese Nachricht ist SEHR WICHTIG');
});
it('should parse complex message with embedded placeholder', () => {
const parsedMessage = parsedMessageFromXML('<a>' +
'link1 with placeholder INTERPOLATION' +
'</a>');
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: ' +
'<b><strange>' +
'FALSCH</b>');
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', () => {
const parsedMessage = parsedMessageFromXML('first: ICU');
expect(parsedMessage.asDisplayString()).toBe('first: ');
});
it('should parse message with 2 embedded ICU message reference', () => {
const parsedMessage = parsedMessageFromXML('first: ICU' +
', second ICU');
expect(parsedMessage.asDisplayString()).toBe('first: , second ');
});
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 line<br>second line');
checkToXmlAndBack(normalizedMessage);
});
});
});