exp-parser.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. describe('UNIT: Expression Parser', function () {
  2. var ExpParser = require('seed/src/exp-parser')
  3. var testCases = [
  4. {
  5. // string concat
  6. exp: 'a + b',
  7. vm: {
  8. a: 'hello',
  9. b: 'world'
  10. },
  11. expectedValue: 'helloworld'
  12. },
  13. {
  14. // math
  15. exp: 'a - b * 2 + 45',
  16. vm: {
  17. a: 100,
  18. b: 23
  19. },
  20. expectedValue: 100 - 23 * 2 + 45
  21. },
  22. {
  23. // boolean logic
  24. exp: '(a && b) ? c : d || e',
  25. vm: {
  26. a: true,
  27. b: false,
  28. c: null,
  29. d: false,
  30. e: 'worked'
  31. },
  32. expectedValue: 'worked'
  33. },
  34. {
  35. // inline string
  36. exp: "a + 'hello'",
  37. vm: {
  38. a: 'inline '
  39. },
  40. expectedValue: 'inline hello'
  41. },
  42. {
  43. // complex with nested values
  44. exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')",
  45. paths: ['todo.title', 'todo.done'],
  46. vm: {
  47. todo: {
  48. title: 'write tests',
  49. done: false
  50. }
  51. },
  52. expectedValue: 'write tests : nope'
  53. },
  54. {
  55. // expression with no scope variables
  56. exp: "'a' + 'b'",
  57. vm: {},
  58. expectedValue: 'ab'
  59. }
  60. ]
  61. testCases.forEach(describeCase)
  62. function describeCase (testCase) {
  63. describe(testCase.exp, function () {
  64. var caughtMissingPaths = [],
  65. compilerMock = {
  66. vm:{
  67. $compiler:{
  68. bindings:{},
  69. createBinding: function (path) {
  70. caughtMissingPaths.push(path)
  71. }
  72. }
  73. }
  74. },
  75. getter = ExpParser.parse(testCase.exp, compilerMock),
  76. vm = testCase.vm,
  77. vars = testCase.paths || Object.keys(vm)
  78. // mock the $get().
  79. // the real $get() will be tested in integration tests.
  80. vm.$get = function (key) { return this[key] }
  81. it('should get correct paths', function () {
  82. if (!vars.length) return
  83. assert.strictEqual(caughtMissingPaths.length, vars.length)
  84. for (var i = 0; i < vars.length; i++) {
  85. assert.strictEqual(vars[i], caughtMissingPaths[i])
  86. }
  87. })
  88. it('should generate correct getter function', function () {
  89. var value = getter.call(vm)
  90. assert.strictEqual(value, testCase.expectedValue)
  91. })
  92. })
  93. }
  94. // extra case for invalid expressions
  95. describe('invalid expression', function () {
  96. it('should capture the error and warn', function () {
  97. var utils = require('seed/src/utils'),
  98. oldWarn = utils.warn,
  99. warned = false
  100. utils.warn = function () {
  101. warned = true
  102. }
  103. ExpParser.parse('a + "fsef', {
  104. vm: {
  105. $compiler: {
  106. bindings: {},
  107. createBinding: function () {}
  108. }
  109. }
  110. })
  111. assert.ok(warned)
  112. utils.warn = oldWarn
  113. })
  114. })
  115. })