2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {HtmlAst} from '@angular/compiler/src/html_ast';
|
|
|
|
import {Message, id} from '@angular/compiler/src/i18n/message';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {deserializeXmb, serializeXmb} from '@angular/compiler/src/i18n/xmb_serializer';
|
|
|
|
import {ParseError, ParseSourceSpan} from '@angular/compiler/src/parse_util';
|
|
|
|
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
2016-03-15 18:00:51 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('Xmb', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
describe('Xmb Serialization', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return an empty message bundle for an empty list of messages',
|
|
|
|
() => { expect(serializeXmb([])).toEqual('<message-bundle></message-bundle>'); });
|
2016-03-27 21:30:32 -04:00
|
|
|
|
2016-06-20 16:18:49 -04:00
|
|
|
it('should serialize messages without desc nor meaning', () => {
|
|
|
|
let m = new Message('content', null, null);
|
2016-03-27 21:30:32 -04:00
|
|
|
let expected = `<message-bundle><msg id='${id(m)}'>content</msg></message-bundle>`;
|
|
|
|
expect(serializeXmb([m])).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2016-06-20 16:18:49 -04:00
|
|
|
it('should serialize messages with desc and meaning', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
let m = new Message('content', 'meaning', 'description');
|
2016-03-27 21:30:32 -04:00
|
|
|
let expected =
|
2016-06-20 16:18:49 -04:00
|
|
|
`<message-bundle><msg id='${id(m)}' desc='description' meaning='meaning'>content</msg></message-bundle>`;
|
|
|
|
expect(serializeXmb([m])).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should escape the desc and meaning', () => {
|
|
|
|
let m = new Message('content', '"\'&<>"\'&<>', '"\'&<>"\'&<>');
|
|
|
|
let expected =
|
|
|
|
`<message-bundle><msg id='${id(m)}' desc='"'&<>"'&<>' meaning='"'&<>"'&<>'>content</msg></message-bundle>`;
|
2016-03-27 21:30:32 -04:00
|
|
|
expect(serializeXmb([m])).toEqual(expected);
|
|
|
|
});
|
2016-03-15 18:00:51 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('Xmb Deserialization', () => {
|
|
|
|
it('should parse an empty bundle', () => {
|
|
|
|
let mb = '<message-bundle></message-bundle>';
|
|
|
|
expect(deserializeXmb(mb, 'url').messages).toEqual({});
|
2016-03-27 21:30:32 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should parse an non-empty bundle', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = `
|
|
|
|
<message-bundle>
|
|
|
|
<msg id="id1" desc="description1">content1</msg>
|
|
|
|
<msg id="id2">content2</msg>
|
|
|
|
</message-bundle>
|
|
|
|
`;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
let parsed = deserializeXmb(mb, 'url').messages;
|
|
|
|
expect(_serialize(parsed['id1'])).toEqual('content1');
|
|
|
|
expect(_serialize(parsed['id2'])).toEqual('content2');
|
2016-03-27 21:30:32 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error when cannot parse the content', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = `
|
|
|
|
<message-bundle>
|
|
|
|
<msg id="id1" desc="description1">content
|
|
|
|
</message-bundle>
|
|
|
|
`;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
let res = deserializeXmb(mb, 'url');
|
2016-03-27 21:30:32 -04:00
|
|
|
expect(_serializeErrors(res.errors)).toEqual(['Unexpected closing tag "message-bundle"']);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error when cannot find the id attribute', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = `
|
|
|
|
<message-bundle>
|
|
|
|
<msg>content</msg>
|
|
|
|
</message-bundle>
|
|
|
|
`;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
let res = deserializeXmb(mb, 'url');
|
2016-03-27 21:30:32 -04:00
|
|
|
expect(_serializeErrors(res.errors)).toEqual(['"id" attribute is missing']);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error on empty content', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = ``;
|
2016-06-08 19:38:52 -04:00
|
|
|
let res = deserializeXmb(mb, 'url');
|
2016-03-27 21:30:32 -04:00
|
|
|
expect(_serializeErrors(res.errors)).toEqual(['Missing element "message-bundle"']);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error on an invalid element', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = `
|
|
|
|
<message-bundle>
|
|
|
|
<invalid>content</invalid>
|
|
|
|
</message-bundle>
|
|
|
|
`;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
let res = deserializeXmb(mb, 'url');
|
2016-03-27 21:30:32 -04:00
|
|
|
expect(_serializeErrors(res.errors)).toEqual(['Unexpected element "invalid"']);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should expand \'ph\' elements', () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = `
|
|
|
|
<message-bundle>
|
|
|
|
<msg id="id1">a<ph name="i0"/></msg>
|
|
|
|
</message-bundle>
|
|
|
|
`;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
let res = deserializeXmb(mb, 'url').messages['id1'];
|
|
|
|
expect((<any>res[1]).name).toEqual('ph');
|
2016-03-27 21:30:32 -04:00
|
|
|
});
|
2016-03-15 18:00:51 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-03-27 21:30:32 -04:00
|
|
|
|
|
|
|
function _serialize(nodes: HtmlAst[]): string {
|
|
|
|
return (<any>nodes[0]).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _serializeErrors(errors: ParseError[]): string[] {
|
|
|
|
return errors.map(e => e.msg);
|
2016-04-28 20:50:03 -04:00
|
|
|
}
|