| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- var expParser = require('../../src/parse/expression')
- function assertExp (testCase) {
- var fn = expParser.parse(testCase.exp)
- expect(fn(testCase.scope)).toEqual(testCase.expected)
- }
- var testCases = [
- {
- // string concat
- exp: 'a+b',
- scope: {
- a: 'hello',
- b: 'world'
- },
- expected: 'helloworld'
- },
- {
- // math
- exp: 'a - b * 2 + 45',
- scope: {
- a: 100,
- b: 23
- },
- expected: 100 - 23 * 2 + 45
- },
- {
- // boolean logic
- exp: '(a && b) ? c : d || e',
- scope: {
- a: true,
- b: false,
- c: null,
- d: false,
- e: 'worked'
- },
- expected: 'worked'
- },
- {
- // inline string with newline
- exp: "a + 'hel\nlo'",
- scope: {
- a: 'inline '
- },
- expected: 'inline hel\nlo'
- },
- {
- // dollar signs and underscore
- exp: "_a + ' ' + $b",
- scope: {
- _a: 'underscore',
- $b: 'dollar'
- },
- expected: 'underscore dollar'
- },
- {
- // complex with nested values
- exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')",
- scope: {
- todo: {
- title: 'write tests',
- done: false
- }
- },
- expected: 'write tests : nope'
- },
- {
- // expression with no data variables
- exp: "'a' + 'b'",
- scope: {},
- expected: 'ab'
- },
- {
- // values with same variable name inside strings
- exp: "'\"test\"' + test + \"'hi'\" + hi",
- scope: {
- test: 1,
- hi: 2
- },
- expected: '"test"1\'hi\'2'
- },
- {
- // expressions with inline object literals
- exp: "sortRows({ column: 'name', test: haha, durrr: 123 })",
- scope: {
- sortRows: function (params) {
- return params.column + params.test + params.durrr
- },
- haha: 'hoho'
- },
- expected: 'namehoho123'
- }
- ]
- describe('Expression Parser', function () {
-
- it('parse getter', function () {
- testCases.forEach(assertExp)
- })
- it('parse setter', function () {
- var setter = expParser.parse('a[b]', true).setter
- var scope = {
- a: { c: 1 },
- b: 'c'
- }
- setter(scope, 2)
- expect(scope.a.c).toBe(2)
- })
- it('cache', function () {
- var fn1 = expParser.parse('a + b')
- var fn2 = expParser.parse('a + b')
- expect(fn1).toBe(fn2)
- })
- })
|