/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {Xtb} from '@angular/compiler/src/i18n/serializers/xtb'; import {escapeRegExp} from '@angular/core/src/facade/lang'; import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; import {HtmlParser} from '../../../src/html_parser/html_parser'; import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/html_parser/interpolation_config'; import {serializeAst} from '../../html_parser/ast_serializer_spec'; export function main(): void { describe('XTB serializer', () => { let serializer: Xtb; function loadAsText(content: string, placeholders: {[id: string]: {[name: string]: string}}): {[id: string]: string} { const asAst = serializer.load(content, 'url', placeholders); let asText: {[id: string]: string} = {}; Object.getOwnPropertyNames(asAst).forEach( id => { asText[id] = serializeAst(asAst[id]).join(''); }); return asText; } beforeEach(() => { serializer = new Xtb(new HtmlParser(), DEFAULT_INTERPOLATION_CONFIG); }); describe('load', () => { it('should load XTB files without placeholders', () => { const XTB = ` bar `; expect(loadAsText(XTB, {})).toEqual({foo: 'bar'}); }); it('should load XTB files with placeholders', () => { const XTB = ` bar `; expect(loadAsText(XTB, {foo: {PLACEHOLDER: '!'}})).toEqual({foo: 'bar!!'}); }); it('should load complex XTB files', () => { const XTB = ` translatable element <b>with placeholders</b> { count, plural, =0 {<p>test</p>}} foo { count, plural, =0 {{ sex, gender, other {<p>deeply nested</p>}} }} `; const PLACEHOLDERS = { a: { START_BOLD_TEXT: '', CLOSE_BOLD_TEXT: '', INTERPOLATION: '{{ a + b }}', }, b: { START_PARAGRAPH: '

', CLOSE_PARAGRAPH: '

', }, d: { START_PARAGRAPH: '

', CLOSE_PARAGRAPH: '

', }, }; expect(loadAsText(XTB, PLACEHOLDERS)).toEqual({ a: 'translatable element with placeholders {{ a + b }}', b: '{ count, plural, =0 {

test

}}', c: 'foo', d: '{ count, plural, =0 {{ sex, gender, other {

deeply nested

}} }}', }); }); }); describe('errors', () => { it('should throw on nested ', () => { const XTB = ''; expect(() => { serializer.load(XTB, 'url', {}); }).toThrowError(/ elements can not be nested/); }); it('should throw on nested ', () => { const XTB = ` `; expect(() => { serializer.load(XTB, 'url', {}); }).toThrowError(/ elements can not be nested/); }); it('should throw when a has no id attribute', () => { const XTB = ` `; expect(() => { serializer.load(XTB, 'url', {}); }).toThrowError(/ misses the "id" attribute/); }); it('should throw when a placeholder has no name attribute', () => { const XTB = ` `; expect(() => { serializer.load(XTB, 'url', {}); }).toThrowError(/ misses the "name" attribute/); }); it('should throw when a placeholder is not present in the source message', () => { const XTB = ` `; expect(() => { serializer.load(XTB, 'url', {}); }).toThrowError(/The placeholder "UNKNOWN" does not exists in the source message/); }); }); it('should throw when the translation results in invalid html', () => { const XTB = ` foobar `; expect(() => { serializer.load(XTB, 'url', {fail: {CLOSE_P: '

'}}); }).toThrowError(/xtb parse errors:\nUnexpected closing tag "p"/); }); it('should throw on unknown tags', () => { const XTB = ``; expect(() => { serializer.load(XTB, 'url', {}); }).toThrowError(new RegExp(escapeRegExp(`Unexpected tag ("[ERROR ->]")`))); }); it('should throw when trying to save an xmb file', () => { expect(() => { serializer.write({}); }).toThrow(); }); }); }