expression_spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. var expParser = require('../../../../src/parse/expression')
  2. var _ = require('../../../../src/util')
  3. function assertExp (testCase) {
  4. var res = expParser.parse(testCase.exp)
  5. expect(res.get(testCase.scope)).toEqual(testCase.expected)
  6. expect(res.paths.length).toBe(testCase.paths.length)
  7. res.paths.forEach(function (p, i) {
  8. expect(p).toBe(testCase.paths[i])
  9. })
  10. }
  11. var testCases = [
  12. {
  13. // simple path that doesn't exist
  14. exp: 'a.b.c',
  15. scope: {
  16. a: {}
  17. },
  18. expected: undefined,
  19. paths: ['a']
  20. },
  21. {
  22. // simple path that exists
  23. exp: 'a.b.d',
  24. scope: {
  25. a:{b:{d:123}}
  26. },
  27. expected: 123,
  28. paths: ['a']
  29. },
  30. // complex path
  31. {
  32. exp: 'a["b"].c',
  33. scope: {
  34. a:{b:{c:234}}
  35. },
  36. expected: 234,
  37. paths: ['a']
  38. },
  39. {
  40. // string concat
  41. exp: 'a+b',
  42. scope: {
  43. a: 'hello',
  44. b: 'world'
  45. },
  46. expected: 'helloworld',
  47. paths: ['a', 'b']
  48. },
  49. {
  50. // math
  51. exp: 'a - b * 2 + 45',
  52. scope: {
  53. a: 100,
  54. b: 23
  55. },
  56. expected: 100 - 23 * 2 + 45,
  57. paths: ['a', 'b']
  58. },
  59. {
  60. // boolean logic
  61. exp: '(a && b) ? c : d || e',
  62. scope: {
  63. a: true,
  64. b: false,
  65. c: null,
  66. d: false,
  67. e: 'worked'
  68. },
  69. expected: 'worked',
  70. paths: ['a', 'b', 'c', 'd', 'e']
  71. },
  72. {
  73. // inline string with newline
  74. exp: "a + 'hel\nlo'",
  75. scope: {
  76. a: 'inline '
  77. },
  78. expected: 'inline hel\nlo',
  79. paths: ['a']
  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: haha, durrr: 123 })",
  123. scope: {
  124. sortRows: function (params) {
  125. return params.column + params.test + params.durrr
  126. },
  127. haha: 'hoho'
  128. },
  129. expected: 'namehoho123',
  130. paths: ['sortRows', 'haha']
  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. describe('Expression Parser', function () {
  188. it('parse getter', function () {
  189. testCases.forEach(assertExp)
  190. })
  191. it('dynamic setter', function () {
  192. // make sure checkSetter works:
  193. // should add setter if a cache hit doesn't have hit function.
  194. expParser.parse('a[b]')
  195. var res = expParser.parse('a[b]', true)
  196. var scope = {
  197. a: { c: 1 },
  198. b: 'c'
  199. }
  200. res.set(scope, 2)
  201. expect(scope.a.c).toBe(2)
  202. })
  203. it('simple path setter', function () {
  204. var res = expParser.parse('a.b.c', true)
  205. var scope = {}
  206. expect(function () {
  207. res.set(scope, 123)
  208. }).not.toThrow()
  209. scope.a = {b:{c:0}}
  210. res.set(scope, 123)
  211. expect(scope.a.b.c).toBe(123)
  212. })
  213. it('cache', function () {
  214. var res1 = expParser.parse('a + b')
  215. var res2 = expParser.parse('a + b')
  216. expect(res1).toBe(res2)
  217. })
  218. describe('invalid expression', function () {
  219. beforeEach(function () {
  220. spyOn(_, 'warn')
  221. })
  222. it('should warn on invalid expression', function () {
  223. var res = expParser.parse('a--b"ffff')
  224. expect(_.warn).toHaveBeenCalled()
  225. })
  226. if (leftHandThrows()) {
  227. it('should warn on invalid left hand expression for setter', function () {
  228. var res = expParser.parse('a+b', true)
  229. expect(_.warn).toHaveBeenCalled()
  230. })
  231. }
  232. })
  233. })
  234. /**
  235. * check if creating a new Function with invalid left-hand
  236. * assignment would throw
  237. */
  238. function leftHandThrows () {
  239. try {
  240. var fn = new Function('a + b = 1')
  241. } catch (e) {
  242. return true
  243. }
  244. }