2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xdescribe,
|
|
|
|
xit
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
2016-03-15 18:00:51 -04:00
|
|
|
|
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';
|
|
|
|
import {serializeXmb, deserializeXmb} from '@angular/compiler/src/i18n/xmb_serializer';
|
|
|
|
import {ParseSourceSpan, ParseError} from '@angular/compiler/src/parse_util';
|
2016-03-15 18:00:51 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("Xmb", () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
describe('Xmb Serialization', () => {
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
it("should serializeXmb messages without desc", () => {
|
|
|
|
let m = new Message("content", "meaning", 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-04-12 12:40:37 -04:00
|
|
|
it("should serializeXmb messages with desc", () => {
|
|
|
|
let m = new Message("content", "meaning", "description");
|
2016-03-27 21:30:32 -04:00
|
|
|
let expected =
|
|
|
|
`<message-bundle><msg id='${id(m)}' desc='description'>content</msg></message-bundle>`;
|
|
|
|
expect(serializeXmb([m])).toEqual(expected);
|
|
|
|
});
|
2016-03-15 18:00:51 -04:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
it("should error on empty content", () => {
|
2016-03-27 21:30:32 -04:00
|
|
|
let mb = ``;
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
let res = deserializeXmb(mb, "url");
|
2016-03-27 21:30:32 -04:00
|
|
|
expect(_serializeErrors(res.errors)).toEqual(['Unexpected element "invalid"']);
|
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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
|
|
|
}
|