expression_spec.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // Object with string values with back-quotes
  71. exp: '[{"a":"he`llo"},{"b":"world"},{"c":55}]',
  72. scope: {},
  73. expected: [{ 'a': 'he`llo'}, { 'b': 'world'}, { 'c': 55}]
  74. },
  75. {
  76. // Object with string values and back quotes (single quoted string)
  77. exp: '[{\'a\':\'he`llo\'},{\'b\':\'world\'},{\'c\':55}]',
  78. scope: {},
  79. expected: [{ 'a': 'he`llo'}, { 'b': 'world'}, { 'c': 55}]
  80. },
  81. {
  82. // dollar signs and underscore
  83. exp: "_a + ' ' + $b",
  84. scope: {
  85. _a: 'underscore',
  86. $b: 'dollar'
  87. },
  88. expected: 'underscore dollar',
  89. paths: ['_a', '$b']
  90. },
  91. {
  92. // complex with nested values
  93. exp: "todo.title + ' : ' + (todo['done'] ? 'yep' : 'nope')",
  94. scope: {
  95. todo: {
  96. title: 'write tests',
  97. done: false
  98. }
  99. },
  100. expected: 'write tests : nope',
  101. paths: ['todo']
  102. },
  103. {
  104. // expression with no data variables
  105. exp: "'a' + 'b'",
  106. scope: {},
  107. expected: 'ab',
  108. paths: []
  109. },
  110. {
  111. // values with same variable name inside strings
  112. exp: "'\"test\"' + test + \"'hi'\" + hi",
  113. scope: {
  114. test: 1,
  115. hi: 2
  116. },
  117. expected: '"test"1\'hi\'2',
  118. paths: ['test', 'hi']
  119. },
  120. {
  121. // expressions with inline object literals
  122. exp: "sortRows({ column: 'name', test: foo, durrr: 123 })",
  123. scope: {
  124. sortRows: function (params) {
  125. return params.column + params.test + params.durrr
  126. },
  127. foo: 'bar'
  128. },
  129. expected: 'namebar123',
  130. paths: ['sortRows', 'bar']
  131. },
  132. {
  133. // space between path segments
  134. exp: ' a . b . c + d',
  135. scope: {
  136. a: { b: { c: 12 }},
  137. d: 3
  138. },
  139. expected: 15,
  140. paths: ['a', 'd']
  141. },
  142. {
  143. // space in bracket identifiers
  144. exp: ' a[ " a.b.c " ] + b [ \' e \' ]',
  145. scope: {
  146. a: {' a.b.c ': 123},
  147. b: {' e ': 234}
  148. },
  149. expected: 357,
  150. paths: ['a', 'b']
  151. },
  152. {
  153. // number literal
  154. exp: 'a * 1e2 + 1.1',
  155. scope: {
  156. a: 3
  157. },
  158. expected: 301.1,
  159. paths: ['a']
  160. },
  161. {
  162. // keyowrd + keyword literal
  163. exp: 'true && a.true',
  164. scope: {
  165. a: { 'true': false }
  166. },
  167. expected: false,
  168. paths: ['a']
  169. },
  170. {
  171. // super complex
  172. exp: ' $a + b[ " a.b.c " ][\'123\'].$e&&c[ " d " ].e + Math.round(e) ',
  173. scope: {
  174. $a: 1,
  175. b: {
  176. ' a.b.c ': {
  177. '123': { $e: 2 }
  178. }
  179. },
  180. c: { ' d ': {e: 3}},
  181. e: 4.5
  182. },
  183. expected: 8,
  184. paths: ['$a', 'b', 'c', 'e']
  185. },
  186. {
  187. // string with escaped quotes
  188. exp: "'a\\'b' + c",
  189. scope: {
  190. c: '\'c'
  191. },
  192. expected: "a'b'c",
  193. paths: ['c']
  194. },
  195. {
  196. // dynamic sub path
  197. exp: "a['b' + i + 'c']",
  198. scope: {
  199. i: 0,
  200. a: {
  201. 'b0c': 123
  202. }
  203. },
  204. expected: 123,
  205. paths: ['a', 'i']
  206. },
  207. {
  208. // Math global, simple path
  209. exp: 'Math.PI',
  210. scope: {},
  211. expected: Math.PI,
  212. paths: []
  213. },
  214. {
  215. // Math global, exp
  216. exp: 'Math.sin(a)',
  217. scope: {
  218. a: 1
  219. },
  220. expected: Math.sin(1),
  221. paths: ['a']
  222. },
  223. {
  224. // boolean literal
  225. exp: 'true',
  226. scope: {
  227. true: false
  228. },
  229. expected: true,
  230. paths: []
  231. },
  232. {
  233. exp: 'null',
  234. scope: {},
  235. expected: null,
  236. paths: []
  237. },
  238. {
  239. exp: 'undefined',
  240. scope: { undefined: 1 },
  241. expected: undefined,
  242. paths: []
  243. },
  244. {
  245. // Date global
  246. exp: 'Date.now() > new Date("2000-01-01")',
  247. scope: {},
  248. expected: true,
  249. paths: []
  250. },
  251. // typeof operator
  252. {
  253. exp: 'typeof test === "string"',
  254. scope: { test: '123' },
  255. expected: true,
  256. paths: ['test']
  257. },
  258. // isNaN
  259. {
  260. exp: 'isNaN(a)',
  261. scope: { a: 2 },
  262. expected: false,
  263. paths: ['a']
  264. },
  265. // parseFloat & parseInt
  266. {
  267. exp: 'parseInt(a, 10) + parseFloat(b)',
  268. scope: { a: 2.33, b: '3.45' },
  269. expected: 5.45,
  270. paths: ['a', 'b']
  271. }
  272. ]
  273. describe('Expression Parser', function () {
  274. testCases.forEach(function (testCase) {
  275. it('parse getter: ' + testCase.exp, function () {
  276. var res = expParser.parseExpression(testCase.exp, true)
  277. expect(res.get(testCase.scope)).toEqual(testCase.expected)
  278. })
  279. })
  280. it('dynamic setter', function () {
  281. // make sure checkSetter works:
  282. // should add setter if a cache hit doesn't have hit function.
  283. expParser.parseExpression('a[b]')
  284. var res = expParser.parseExpression('a[b]', true)
  285. var scope = {
  286. a: { c: 1 },
  287. b: 'c'
  288. }
  289. res.set(scope, 2)
  290. expect(scope.a.c).toBe(2)
  291. })
  292. it('simple path setter', function () {
  293. var res = expParser.parseExpression('a.b.c', true)
  294. var scope = {}
  295. expect(function () {
  296. res.set(scope, 123)
  297. }).not.toThrow()
  298. scope.a = {b: {c: 0}}
  299. res.set(scope, 123)
  300. expect(scope.a.b.c).toBe(123)
  301. })
  302. it('cache', function () {
  303. var res1 = expParser.parseExpression('a + b')
  304. var res2 = expParser.parseExpression('a + b')
  305. expect(res1).toBe(res2)
  306. })
  307. if (canMakeTemplateStringFunction()) {
  308. it('ES2015 template string handling', function () {
  309. var res = expParser.parseExpression('a + `hi ${ b }` + c')
  310. expect(res.get.toString().indexOf('scope.a+`hi ${scope.b}`+scope.c') > -1).toBe(true)
  311. res = expParser.parseExpression('`hi ${ b + `${ d }` }`')
  312. expect(res.get.toString().indexOf('`hi ${scope.b+`${scope.d}`}`') > -1).toBe(true)
  313. res = expParser.parseExpression('{transform:`rotate(${x}deg)`}')
  314. expect(res.get.toString().indexOf('{transform:`rotate(${scope.x}deg)`}') > -1).toBe(true)
  315. })
  316. }
  317. describe('invalid expression', function () {
  318. it('should warn on invalid expression', function () {
  319. expect(getWarnCount()).toBe(0)
  320. expParser.parseExpression('a--b"ffff')
  321. expect('Invalid expression').toHaveBeenWarned()
  322. })
  323. it('should warn on invalid setter expression', function () {
  324. expect(getWarnCount()).toBe(0)
  325. expParser.parseExpression('a+b', true)
  326. expect('Invalid setter expression').toHaveBeenWarned()
  327. })
  328. it('should warn if expression contains improper reserved keywords', function () {
  329. expect(getWarnCount()).toBe(0)
  330. expParser.parseExpression('break + 1')
  331. expect('Avoid using reserved keywords').toHaveBeenWarned()
  332. })
  333. })
  334. })
  335. function canMakeTemplateStringFunction () {
  336. try {
  337. /* eslint-disable no-new-func */
  338. new Function('a', 'return `${a}`')
  339. } catch (e) {
  340. return false
  341. }
  342. return true
  343. }