2017-03-09 06:23:23 +00:00
|
|
|
import * as utils from '../utils';
|
2017-07-20 03:36:44 +00:00
|
|
|
import {globalBeforeEach} from '../../__jest__/before-each';
|
2017-03-09 06:23:23 +00:00
|
|
|
|
|
|
|
describe('getKeys()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2017-03-09 06:23:23 +00:00
|
|
|
it('flattens complex object', () => {
|
|
|
|
const obj = {
|
|
|
|
foo: 'bar',
|
|
|
|
nested: {a: {b: {}}},
|
2017-07-14 19:32:44 +00:00
|
|
|
null: null,
|
|
|
|
undefined: undefined,
|
|
|
|
false: false,
|
2017-03-09 06:23:23 +00:00
|
|
|
array: [
|
|
|
|
'hello',
|
|
|
|
{hi: 'there'},
|
|
|
|
true,
|
|
|
|
['x', 'y', 'z']
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2017-03-12 05:38:44 +00:00
|
|
|
const keys = utils
|
|
|
|
.getKeys(obj)
|
|
|
|
.sort((a, b) => a.name > b.name ? 1 : -1);
|
|
|
|
|
|
|
|
expect(keys).toEqual([
|
|
|
|
{name: 'array[0]', value: obj.array[0]},
|
|
|
|
{name: 'array[1].hi', value: obj.array[1].hi},
|
|
|
|
{name: 'array[2]', value: obj.array[2]},
|
|
|
|
{name: 'array[3][0]', value: obj.array[3][0]},
|
|
|
|
{name: 'array[3][1]', value: obj.array[3][1]},
|
|
|
|
{name: 'array[3][2]', value: obj.array[3][2]},
|
2017-07-14 19:32:44 +00:00
|
|
|
{name: 'false', value: obj.false},
|
|
|
|
{name: 'foo', value: obj.foo},
|
|
|
|
{name: 'null', value: obj.null},
|
|
|
|
{name: 'undefined', value: obj.undefined}
|
2017-03-12 05:38:44 +00:00
|
|
|
]);
|
2017-03-12 00:31:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('ignores functions', () => {
|
|
|
|
const obj = {
|
|
|
|
foo: 'bar',
|
|
|
|
toString: function () {
|
|
|
|
// Nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const keys = utils.getKeys(obj);
|
2017-05-23 22:05:31 +00:00
|
|
|
expect(keys).toEqual([{name: 'foo', value: 'bar'}]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('tokenizeTag()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2017-05-23 22:05:31 +00:00
|
|
|
it('tokenizes complex tag', () => {
|
|
|
|
const actual = utils.tokenizeTag(
|
|
|
|
`{% name bar, "baz \\"qux\\"" , 1 + 5 | default("foo") %}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [
|
|
|
|
{type: 'variable', value: 'bar'},
|
|
|
|
{type: 'string', value: 'baz "qux"', quotedBy: '"'},
|
|
|
|
{type: 'expression', value: '1 + 5 | default("foo")'}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles whitespace', () => {
|
|
|
|
const minimal = utils.tokenizeTag(`{%name'foo',bar%}`);
|
|
|
|
const generous = utils.tokenizeTag(`{%name \t'foo' , bar\t\n%}`);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [
|
|
|
|
{type: 'string', value: 'foo', quotedBy: "'"},
|
|
|
|
{type: 'variable', value: 'bar'}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(minimal).toEqual(expected);
|
|
|
|
expect(generous).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles type string', () => {
|
|
|
|
const actual = utils.tokenizeTag(`{% name 'foo' %}`);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [{type: 'string', value: 'foo', quotedBy: "'"}]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles type number', () => {
|
2017-06-01 02:04:27 +00:00
|
|
|
const actual = utils.tokenizeTag(`{% name 9.324, 8, 7 %}`);
|
2017-05-23 22:05:31 +00:00
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [
|
2017-06-01 02:04:27 +00:00
|
|
|
{type: 'number', value: '9.324'},
|
|
|
|
{type: 'number', value: '8'},
|
|
|
|
{type: 'number', value: '7'}
|
2017-05-23 22:05:31 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles type boolean', () => {
|
|
|
|
const actual = utils.tokenizeTag(`{% name true, false %}`);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [
|
|
|
|
{type: 'boolean', value: 'true'},
|
|
|
|
{type: 'boolean', value: 'false'}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles type expression', () => {
|
|
|
|
const actual = utils.tokenizeTag(`{% name 5 * 10 + 'hello' | default(2 - 3) %}`);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [{type: 'expression', value: `5 * 10 + 'hello' | default(2 - 3)`}]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: This is actually invalid Nunjucks but we handle it anyway
|
|
|
|
* because it's better (and easier to implement) than failing.
|
|
|
|
*/
|
|
|
|
it('handles no commas', () => {
|
|
|
|
const actual = utils.tokenizeTag(`{% name foo bar baz %}`);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
name: 'name',
|
|
|
|
args: [{type: 'expression', value: 'foo bar baz'}]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('unTokenizeTag()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2017-05-23 22:05:31 +00:00
|
|
|
it('untokenizes a tag', () => {
|
|
|
|
const tagStr = `{% name bar, "baz \\"qux\\"" , 1 + 5, 'hi' %}`;
|
|
|
|
|
|
|
|
const tagData = utils.tokenizeTag(tagStr);
|
|
|
|
const result = utils.unTokenizeTag(tagData);
|
|
|
|
|
|
|
|
expect(result).toEqual(`{% name bar, "baz \\"qux\\"", 1 + 5, 'hi' %}`);
|
2017-03-09 06:23:23 +00:00
|
|
|
});
|
|
|
|
});
|