2017-01-13 09:02:01 +01:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
|
|
|
import { TranslationCollection } from '../../src/utils/translation.collection';
|
|
|
|
import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler';
|
|
|
|
|
|
|
|
describe('NamespacedJsonCompiler', () => {
|
|
|
|
let compiler: NamespacedJsonCompiler;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
compiler = new NamespacedJsonCompiler();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should flatten keys on parse', () => {
|
|
|
|
const contents = `
|
|
|
|
{
|
|
|
|
"NAMESPACE": {
|
|
|
|
"KEY": {
|
|
|
|
"FIRST_KEY": "",
|
|
|
|
"SECOND_KEY": "VALUE"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
const collection: TranslationCollection = compiler.parse(contents);
|
2019-09-18 14:16:47 +02:00
|
|
|
expect(collection.values).to.deep.equal({
|
|
|
|
'NAMESPACE.KEY.FIRST_KEY': '',
|
|
|
|
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
|
|
|
|
});
|
2017-01-13 09:02:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should unflatten keys on compile', () => {
|
|
|
|
const collection = new TranslationCollection({
|
|
|
|
'NAMESPACE.KEY.FIRST_KEY': '',
|
|
|
|
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
|
|
|
|
});
|
|
|
|
const result: string = compiler.compile(collection);
|
|
|
|
expect(result).to.equal('{\n\t"NAMESPACE": {\n\t\t"KEY": {\n\t\t\t"FIRST_KEY": "",\n\t\t\t"SECOND_KEY": "VALUE"\n\t\t}\n\t}\n}');
|
|
|
|
});
|
|
|
|
|
2017-03-29 12:52:14 +02:00
|
|
|
it('should preserve numeric values on compile', () => {
|
|
|
|
const collection = new TranslationCollection({
|
2019-06-12 11:50:23 +02:00
|
|
|
'option.0': '',
|
|
|
|
'option.1': '',
|
|
|
|
'option.2': ''
|
2017-03-29 12:52:14 +02:00
|
|
|
});
|
|
|
|
const result: string = compiler.compile(collection);
|
|
|
|
expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}');
|
|
|
|
});
|
|
|
|
|
2017-03-29 14:20:01 +02:00
|
|
|
it('should use custom indentation chars', () => {
|
|
|
|
const collection = new TranslationCollection({
|
|
|
|
'NAMESPACE.KEY.FIRST_KEY': '',
|
|
|
|
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
|
|
|
|
});
|
|
|
|
const customCompiler = new NamespacedJsonCompiler({
|
|
|
|
indentation: ' '
|
|
|
|
});
|
|
|
|
const result: string = customCompiler.compile(collection);
|
|
|
|
expect(result).to.equal('{\n "NAMESPACE": {\n "KEY": {\n "FIRST_KEY": "",\n "SECOND_KEY": "VALUE"\n }\n }\n}');
|
|
|
|
});
|
|
|
|
|
2019-08-02 13:34:18 +02:00
|
|
|
it('should not reorder keys when compiled', () => {
|
|
|
|
const collection = new TranslationCollection({
|
2019-09-18 14:16:47 +02:00
|
|
|
BROWSE: '',
|
|
|
|
LOGIN: ''
|
2019-08-02 13:34:18 +02:00
|
|
|
});
|
|
|
|
const result: string = compiler.compile(collection);
|
|
|
|
expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}');
|
|
|
|
});
|
2017-01-13 09:02:01 +01:00
|
|
|
});
|