expression_spec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. var expParser = require('src/parsers/expression')
  2. var testCases = [
  3. {
  4. // simple path
  5. exp: 'a.b.d',
  6. scope: {
  7. a: { b: { d: 123 }}
  8. },
  9. expected: 123,
  10. paths: ['a']
  11. },
  12. // complex path
  13. {
  14. exp: 'a["b"].c',
  15. scope: {
  16. a: { b: { c: 234 }}
  17. },
  18. expected: 234,
  19. paths: ['a']
  20. },
  21. {
  22. // string concat
  23. exp: 'a+b',
  24. scope: {
  25. a: 'hello',
  26. b: 'world'
  27. },
  28. expected: 'helloworld',
  29. paths: ['a', 'b']
  30. },
  31. {
  32. // math
  33. exp: 'a - b * 2 + 45',
  34. scope: {
  35. a: 100,
  36. b: 23
  37. },
  38. expected: 100 - 23 * 2 + 45,
  39. paths: ['a', 'b']
  40. },
  41. {
  42. // boolean logic
  43. exp: '(a && b) ? c : d || e',
  44. scope: {
  45. a: true,
  46. b: false,
  47. c: null,
  48. d: false,
  49. e: 'worked'
  50. },
  51. expected: 'worked',
  52. paths: ['a', 'b', 'c', 'd', 'e']
  53. },
  54. {
  55. // inline string with newline
  56. exp: "a + 'hel\nlo'",
  57. scope: {
  58. a: 'inline '
  59. },
  60. expected: 'inline hel\nlo',
  61. paths: ['a']
  62. },
  63. {
  64. // multiline expressions
  65. exp: "{\n a: '35',\n b: c}",
  66. scope: {c: 32},
  67. expected: { a: '35', b: 32 }
  68. },
  69. {
  70. // dollar signs and underscore
  71. exp: "_a + ' ' + $b",
  72. scope: {
  73. _a: 'underscore',
  74. $b: 'dollar'
  75. },
  76. expected: 'underscore dollar',
  77. paths: ['_a', '$b']
  78. },
  79. {
  80. // complex with nested values
  81. exp: "todo.title + ' : ' + (todo['done'] ? 'yep' : 'nope')",
  82. scope: {
  83. todo: {
  84. title: 'write tests',
  85. done: false
  86. }
  87. },
  88. expected: 'write tests : nope',
  89. paths: ['todo']
  90. },
  91. {
  92. // expression with no data variables
  93. exp: "'a' + 'b'",
  94. scope: {},
  95. expected: 'ab',
  96. paths: []
  97. },
  98. {
  99. // values with same variable name inside strings
  100. exp: "'\"test\"' + test + \"'hi'\" + hi",
  101. scope: {
  102. test: 1,
  103. hi: 2
  104. },
  105. expected: '"test"1\'hi\'2',
  106. paths: ['test', 'hi']
  107. },
  108. {
  109. // expressions with inline object literals
  110. exp: "sortRows({ column: 'name', test: haha, durrr: 123 })",
  111. scope: {
  112. sortRows: function (params) {
  113. return params.column + params.test + params.durrr
  114. },
  115. haha: 'hoho'
  116. },
  117. expected: 'namehoho123',
  118. paths: ['sortRows', 'haha']
  119. },
  120. {
  121. // space between path segments
  122. exp: ' a . b . c + d',
  123. scope: {
  124. a: { b: { c: 12 }},
  125. d: 3
  126. },
  127. expected: 15,
  128. paths: ['a', 'd']
  129. },
  130. {
  131. // space in bracket identifiers
  132. exp: ' a[ " a.b.c " ] + b [ \' e \' ]',
  133. scope: {
  134. a: {' a.b.c ': 123},
  135. b: {' e ': 234}
  136. },
  137. expected: 357,
  138. paths: ['a', 'b']
  139. },
  140. {
  141. // number literal
  142. exp: 'a * 1e2 + 1.1',
  143. scope: {
  144. a: 3
  145. },
  146. expected: 301.1,
  147. paths: ['a']
  148. },
  149. {
  150. // keyowrd + keyword literal
  151. exp: 'true && a.true',
  152. scope: {
  153. a: { 'true': false }
  154. },
  155. expected: false,
  156. paths: ['a']
  157. },
  158. {
  159. // super complex
  160. exp: ' $a + b[ " a.b.c " ][\'123\'].$e&&c[ " d " ].e + Math.round(e) ',
  161. scope: {
  162. $a: 1,
  163. b: {
  164. ' a.b.c ': {
  165. '123': { $e: 2 }
  166. }
  167. },
  168. c: { ' d ': {e: 3}},
  169. e: 4.5
  170. },
  171. expected: 8,
  172. paths: ['$a', 'b', 'c', 'e']
  173. },
  174. {
  175. // string with escaped quotes
  176. exp: "'a\\'b' + c",
  177. scope: {
  178. c: '\'c'
  179. },
  180. expected: "a\'b\'c",
  181. paths: ['c']
  182. },
  183. {
  184. // dynamic sub path
  185. exp: "a['b' + i + 'c']",
  186. scope: {
  187. i: 0,
  188. a: {
  189. 'b0c': 123
  190. }
  191. },
  192. expected: 123,
  193. paths: ['a', 'i']
  194. },
  195. {
  196. // Math global, simple path
  197. exp: 'Math.PI',
  198. scope: {},
  199. expected: Math.PI,
  200. paths: []
  201. },
  202. {
  203. // Math global, exp
  204. exp: 'Math.sin(a)',
  205. scope: {
  206. a: 1
  207. },
  208. expected: Math.sin(1),
  209. paths: ['a']
  210. },
  211. {
  212. // boolean literal
  213. exp: 'true',
  214. scope: {
  215. true: false
  216. },
  217. expected: true,
  218. paths: []
  219. },
  220. {
  221. // Date global
  222. exp: 'Date.now() > new Date("2000-01-01")',
  223. scope: {},
  224. expected: true,
  225. paths: []
  226. },
  227. // typeof operator
  228. {
  229. exp: 'typeof test === "string"',
  230. scope: { test: '123' },
  231. expected: true,
  232. paths: ['test']
  233. },
  234. // isNaN
  235. {
  236. exp: 'isNaN(a)',
  237. scope: { a: 2 },
  238. expected: false,
  239. paths: ['a']
  240. },
  241. // parseFloat & parseInt
  242. {
  243. exp: 'parseInt(a, 10) + parseFloat(b)',
  244. scope: { a: 2.33, b: '3.45' },
  245. expected: 5.45,
  246. paths: ['a', 'b']
  247. }
  248. ]
  249. describe('Expression Parser', function () {
  250. testCases.forEach(function (testCase) {
  251. it('parse getter: ' + testCase.exp, function () {
  252. var res = expParser.parseExpression(testCase.exp, true)
  253. expect(res.get(testCase.scope)).toEqual(testCase.expected)
  254. })
  255. })
  256. it('dynamic setter', function () {
  257. // make sure checkSetter works:
  258. // should add setter if a cache hit doesn't have hit function.
  259. expParser.parseExpression('a[b]')
  260. var res = expParser.parseExpression('a[b]', true)
  261. var scope = {
  262. a: { c: 1 },
  263. b: 'c'
  264. }
  265. res.set(scope, 2)
  266. expect(scope.a.c).toBe(2)
  267. })
  268. it('simple path setter', function () {
  269. var res = expParser.parseExpression('a.b.c', true)
  270. var scope = {}
  271. expect(function () {
  272. res.set(scope, 123)
  273. }).not.toThrow()
  274. scope.a = {b: {c: 0}}
  275. res.set(scope, 123)
  276. expect(scope.a.b.c).toBe(123)
  277. })
  278. it('cache', function () {
  279. var res1 = expParser.parseExpression('a + b')
  280. var res2 = expParser.parseExpression('a + b')
  281. expect(res1).toBe(res2)
  282. })
  283. if (canMakeTemplateStringFunction()) {
  284. it('ES2015 template string handling', function () {
  285. var res = expParser.parseExpression('a + `hi ${ b }` + c')
  286. expect(res.get.toString().indexOf('scope.a+`hi ${scope.b}`+scope.c') > -1).toBe(true)
  287. res = expParser.parseExpression('`hi ${ b + `${ d }` }`')
  288. expect(res.get.toString().indexOf('`hi ${scope.b+`${scope.d}`}`') > -1).toBe(true)
  289. res = expParser.parseExpression('{transform:`rotate(${x}deg)`}')
  290. expect(res.get.toString().indexOf('{transform:`rotate(${scope.x}deg)`}') > -1).toBe(true)
  291. })
  292. }
  293. describe('invalid expression', function () {
  294. it('should warn on invalid expression', function () {
  295. expect(getWarnCount()).toBe(0)
  296. expParser.parseExpression('a--b"ffff')
  297. expect('Invalid expression').toHaveBeenWarned()
  298. })
  299. it('should warn on invalid setter expression', function () {
  300. expect(getWarnCount()).toBe(0)
  301. expParser.parseExpression('a+b', true)
  302. expect('Invalid setter expression').toHaveBeenWarned()
  303. })
  304. it('should warn if expression contains improper reserved keywords', function () {
  305. expect(getWarnCount()).toBe(0)
  306. expParser.parseExpression('break + 1')
  307. expect('Avoid using reserved keywords').toHaveBeenWarned()
  308. })
  309. })
  310. })
  311. function canMakeTemplateStringFunction () {
  312. try {
  313. /* eslint-disable no-new-func */
  314. new Function('a', 'return `${a}`')
  315. } catch (e) {
  316. return false
  317. }
  318. return true
  319. }