import {Xliff2MessageParser} from './xliff2-message-parser'; import {ParsedMessage} from './parsed-message'; /** * Created by martin on 14.05.2017. * Testcases for parsing normalized messages to XLIFF 2.0 and vive versa. */ describe('message parse XLIFF 2.0 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 Xliff2MessageParser(); 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 Xliff2MessageParser(); 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'); }); 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 ICU Refs', () => { const normalizedMessage = 'a text with '; const parsedMessage = parsedMessageFor(normalizedMessage); expect(parsedMessage.asDisplayString()).toBe(normalizedMessage); // old syntax before angular #17344 // expect(parsedMessage.asNativeString()).toBe('a text with '); // new syntax after angular #17344 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 report an error when xml string is not correct (TODO, does not work)', () => { const parsedMessage = parsedMessageFromXML(''); expect(parsedMessage.asDisplayString()).toBe(''); // TODO xmldoc does not report any error }); it('should parse message with embedded ICU message reference', () => { const parsedMessage = parsedMessageFromXML('first: '); expect(parsedMessage.asDisplayString()).toBe('first: '); }); it('should parse message with embedded ICU message reference (new syntax after angular #17344)', () => { 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 parse message with 2 embedded ICU message reference (new syntax after angular #17344)', () => { const parsedMessage = parsedMessageFromXML('first: , second '); 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 linesecond line'); checkToXmlAndBack(normalizedMessage); }); }); });