expression-language
Version:
Javascript implementation of symfony/expression-language
61 lines (60 loc) • 3.29 kB
JavaScript
"use strict";
var _ExpressionLanguage = _interopRequireDefault(require("../../ExpressionLanguage"));
var _ArrayProvider = _interopRequireDefault(require("../ArrayProvider"));
var _CompileRuntime = _interopRequireDefault(require("../../CompileRuntime"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
test('implode evaluate', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.evaluate('implode(". ", ["check", "this", "out"])');
expect(result).toBe("check. this. out");
});
test('implode compile', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.compile('implode(". ", ["check", "this", "out"])');
expect(result).toBe('__runtime.implode(". ", ["check", "this", "out"])');
let fn = new Function('__runtime', 'return ' + result + ';');
expect(fn(_CompileRuntime.default)).toBe("check. this. out");
});
test('count evaluate', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.evaluate('count(["1", "2", "3"])');
expect(result).toBe(3);
let result2 = el.evaluate('count(["1", "2", "3", ["4", "5"]], "COUNT_RECURSIVE")');
expect(result2).toBe(6); // Counts array as one, then contents individually
});
test('count compile', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.compile('count(["1", "2", "3"])');
expect(result).toBe('__runtime.count(["1", "2", "3"])');
let result2 = el.compile('count(["1", "2", "3"], "COUNT_RECURSIVE")');
expect(result2).toBe('__runtime.count(["1", "2", "3"], "COUNT_RECURSIVE")');
let fn = new Function('__runtime', 'return ' + result + ';');
expect(fn(_CompileRuntime.default)).toBe(3);
});
test('array_intersect evaluate', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.evaluate('array_intersect(["1", "2", "3"], ["1", "2", "3"], ["2", "3"])');
expect(result).toMatchObject(["2", "3"]);
});
test('array_intersect compile', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.compile('array_intersect(["1", "2", "3"], ["1", "2", "3"], ["2", "3"])');
expect(result).toBe('__runtime.array_intersect(["1", "2", "3"], ["1", "2", "3"], ["2", "3"])');
let fn = new Function('__runtime', 'return ' + result + ';');
expect(fn(_CompileRuntime.default)).toMatchObject(["2", "3"]);
});
test('array_intersect compile with a single array argument (no additional args)', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.compile('array_intersect(["1", "2", "3"])');
expect(result).toBe('__runtime.array_intersect(["1", "2", "3"])');
let fn = new Function('__runtime', 'return ' + result + ';');
expect(fn(_CompileRuntime.default)).toMatchObject([]);
});
test('array_intersect evaluate with associative-object (non-array) arguments preserves keys', () => {
let el = new _ExpressionLanguage.default(null, [new _ArrayProvider.default()]);
let result = el.evaluate('array_intersect({a: "1", b: "2", c: "3"}, {x: "2", y: "3"})');
expect(result).toEqual({
b: "2",
c: "3"
});
});