expression_spec.js 6.0 KB

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