exp-parser.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. describe('UNIT: Expression Parser', function () {
  2. var ExpParser = require('vue/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 data variables
  56. exp: "'a' + 'b'",
  57. vm: {},
  58. expectedValue: 'ab'
  59. },
  60. {
  61. // values with same variable name inside strings
  62. exp: "'\"test\"' + test + \"'hi'\" + hi",
  63. vm: {
  64. test: 1,
  65. hi: 2
  66. },
  67. expectedValue: '"test"1\'hi\'2'
  68. }
  69. ]
  70. testCases.forEach(describeCase)
  71. function describeCase (testCase) {
  72. describe(testCase.exp, function () {
  73. function createBinding (path) {
  74. caughtMissingPaths.push(path)
  75. }
  76. var caughtMissingPaths = [],
  77. compilerMock = {
  78. createBinding: createBinding,
  79. hasKey: function () {},
  80. vm:{
  81. $data: {},
  82. $compiler:{
  83. bindings:{},
  84. createBinding: createBinding
  85. }
  86. }
  87. },
  88. getter = ExpParser.parse(testCase.exp, compilerMock),
  89. vm = testCase.vm,
  90. vars = testCase.paths || Object.keys(vm)
  91. it('should get correct paths', function () {
  92. if (!vars.length) return
  93. assert.strictEqual(caughtMissingPaths.length, vars.length)
  94. for (var i = 0; i < vars.length; i++) {
  95. assert.strictEqual(vars[i], caughtMissingPaths[i])
  96. }
  97. })
  98. it('should generate correct getter function', function () {
  99. var value = getter.call(vm)
  100. assert.strictEqual(value, testCase.expectedValue)
  101. })
  102. })
  103. }
  104. // extra case for invalid expressions
  105. describe('invalid expression', function () {
  106. it('should capture the error and warn', function () {
  107. var utils = require('vue/src/utils'),
  108. oldWarn = utils.warn,
  109. warned = false
  110. utils.warn = function () {
  111. warned = true
  112. }
  113. function noop () {}
  114. ExpParser.parse('a + "fsef', {
  115. createBinding: noop,
  116. hasKey: noop,
  117. vm: {
  118. $compiler: {
  119. bindings: {},
  120. createBinding: noop
  121. },
  122. $data: {}
  123. }
  124. })
  125. assert.ok(warned)
  126. utils.warn = oldWarn
  127. })
  128. })
  129. })