angular-cn/modules/@angular/compiler/test/i18n/message_bundle_spec.ts

83 lines
2.9 KiB
TypeScript
Raw Normal View History

2016-07-15 12:42:33 -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-07-21 16:56:58 -04:00
import * as i18n from '@angular/compiler/src/i18n/i18n_ast';
import {Serializer} from '@angular/compiler/src/i18n/serializers/serializer';
2016-07-15 12:42:33 -04:00
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
2016-07-21 14:41:25 -04:00
import {HtmlParser} from '../../src/html_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../src/html_parser/interpolation_config';
2016-07-21 16:56:58 -04:00
import {MessageBundle, serializeAst, strHash} from '../../src/i18n/message_bundle';
2016-07-15 12:42:33 -04:00
export function main(): void {
2016-07-21 16:56:58 -04:00
describe('MessageBundle', () => {
describe('Messages', () => {
let messages: MessageBundle;
2016-07-15 12:42:33 -04:00
2016-07-21 16:56:58 -04:00
beforeEach(() => { messages = new MessageBundle(new HtmlParser, [], {}); });
2016-07-15 12:42:33 -04:00
it('should extract the message to the catalog', () => {
2016-07-21 16:56:58 -04:00
messages.updateFromTemplate(
2016-07-15 12:42:33 -04:00
'<p i18n="m|d">Translate Me</p>', 'url', DEFAULT_INTERPOLATION_CONFIG);
2016-07-21 16:56:58 -04:00
expect(humanizeMessages(messages)).toEqual([
2016-07-15 12:42:33 -04:00
'a486901=Translate Me',
]);
});
it('should extract the same message with different meaning in different entries', () => {
2016-07-21 16:56:58 -04:00
messages.updateFromTemplate(
2016-07-15 12:42:33 -04:00
'<p i18n="m|d">Translate Me</p><p i18n>Translate Me</p>', 'url',
DEFAULT_INTERPOLATION_CONFIG);
2016-07-21 16:56:58 -04:00
expect(humanizeMessages(messages)).toEqual([
2016-07-15 12:42:33 -04:00
'a486901=Translate Me',
'8475f2cc=Translate Me',
]);
});
});
describe('strHash', () => {
it('should return a hash value', () => {
// https://github.com/google/closure-library/blob/1fb19a857b96b74e6523f3e9d33080baf25be046/closure/goog/string/string_test.js#L1115
expectHash('', 0);
expectHash('foo', 101574);
expectHash('\uAAAAfoo', 1301670364);
expectHash('a', 92567585, 5);
expectHash('a', 2869595232, 6);
expectHash('a', 3058106369, 7);
expectHash('a', 312017024, 8);
expectHash('a', 2929737728, 1024);
});
});
});
}
class _TestSerializer implements Serializer {
2016-07-21 16:56:58 -04:00
write(messageMap: {[id: string]: i18n.Message}): string {
2016-07-15 12:42:33 -04:00
return Object.keys(messageMap)
.map(id => `${id}=${serializeAst(messageMap[id].nodes)}`)
.join('//');
}
2016-07-21 16:56:58 -04:00
load(content: string, url: string, placeholders: {}): {} { return null; }
2016-07-15 12:42:33 -04:00
}
2016-07-21 16:56:58 -04:00
function humanizeMessages(catalog: MessageBundle): string[] {
2016-07-15 12:42:33 -04:00
return catalog.write(new _TestSerializer()).split('//');
}
function expectHash(text: string, decimal: number, repeat: number = 1) {
let acc = text;
for (let i = 1; i < repeat; i++) {
acc += text;
}
const hash = strHash(acc);
expect(typeof(hash)).toEqual('string');
expect(hash.length > 0).toBe(true);
expect(parseInt(hash, 16)).toEqual(decimal);
}