expression_parser_spec.js 4.1 KB

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