expression_spec.js 4.8 KB

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