Преглед изворни кода

unit test for Expression Parser

Evan You пре 12 година
родитељ
комит
d80b5ffef6
6 измењених фајлова са 124 додато и 6 уклоњено
  1. 1 1
      Gruntfile.js
  2. 5 1
      src/deps-parser.js
  3. 4 4
      src/exp-parser.js
  4. 0 0
      test/integration/basic.html
  5. 28 0
      test/unit/deps-parser.js
  6. 86 0
      test/unit/exp-parser.js

+ 1 - 1
Gruntfile.js

@@ -33,7 +33,7 @@ module.exports = function( grunt ) {
 
         mocha: {
             build: {
-                src: ['test/e2e/*.html'],
+                src: ['test/integration/*.html'],
                 options: {
                     reporter: 'Spec',
                     run: true

+ 5 - 1
src/deps-parser.js

@@ -102,5 +102,9 @@ module.exports = {
         bindings.forEach(catchDeps)
         observer.isObserving = false
         utils.log('\ndone.')
-    }
+    },
+
+    // for testing only
+    cdvm: createDummyVM,
+    pcd: parseContextDependency
 }

+ 4 - 4
src/exp-parser.js

@@ -40,13 +40,13 @@ module.exports = {
         var vars = getVariables(exp)
         if (!vars.length) return null
         var args = [],
-            v, i = vars.length,
+            v, i, l = vars.length,
             hash = {}
-        while (i--) {
+        for (i = 0; i < l; i++) {
             v = vars[i]
             // avoid duplicate keys
             if (hash[v]) continue
-            hash[v] = 1
+            hash[v] = v
             // push assignment
             args.push(v + '=this.$get("' + v + '")')
         }
@@ -54,7 +54,7 @@ module.exports = {
         /* jshint evil: true */
         return {
             getter: new Function(args),
-            vars: vars
+            vars: Object.keys(hash)
         }
     }
 }

+ 0 - 0
test/e2e/basic.html → test/integration/basic.html


+ 28 - 0
test/unit/deps-parser.js

@@ -0,0 +1,28 @@
+/*
+ *  NOTE
+ *
+ *  this suite only tests two utility methods used in the
+ *  Dependency Parser, but does not test the main .parse()
+ *  method. .parse() is covered in integration tests because
+ *  it has to work with multiple compilers.
+ */
+
+// shiv the document to provide dummy object
+global.document = {
+    createElement: function () { return {} }
+}
+
+var DepsParser = require('../../src/deps-parser'),
+    assert     = require('assert')
+
+describe('UNIT: Dependency Parser', function () {
+    
+    describe('.createDummyVM()', function () {
+        var createDummyVM = DepsParser.cdvm
+    })
+
+    describe('.parseContextDependency()', function () {
+        var parseContextDependency = DepsParser.pcd
+    })
+
+})

+ 86 - 0
test/unit/exp-parser.js

@@ -0,0 +1,86 @@
+var ExpParser = require('../../src/exp-parser'),
+    assert    = require('assert')
+
+describe('UNIT: Expression Parser', function () {
+    
+    var testCases = [
+        {
+            // string concat
+            exp: 'a + b',
+            vm: {
+                a: 'hello',
+                b: 'world'
+            },
+            expectedValue: 'helloworld'
+        },
+        {
+            // math
+            exp: 'a - b * 2 + 45',
+            vm: {
+                a: 100,
+                b: 23
+            },
+            expectedValue: 100 - 23 * 2 + 45
+        },
+        {
+            // boolean logic
+            exp: '(a && b) ? c : d || e',
+            vm: {
+                a: true,
+                b: false,
+                c: null,
+                d: false,
+                e: 'worked'
+            },
+            expectedValue: 'worked'
+        },
+        {
+            // inline string
+            exp: "a + 'hello'",
+            vm: {
+                a: 'inline '
+            },
+            expectedValue: 'inline hello'
+        },
+        {
+            // complex with nested values
+            exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')",
+            vm: {
+                todo: {
+                    title: 'write tests',
+                    done: false
+                }
+            },
+            expectedValue: 'write tests : nope'
+        }
+    ]
+
+    testCases.forEach(describeCase)
+
+    function describeCase (testCase) {
+        describe(testCase.exp, function () {
+
+            var result = ExpParser.parse(testCase.exp),
+                vm     = testCase.vm,
+                vars   = Object.keys(vm)
+
+            // mock the $get().
+            // the real $get() will be tested in integration tests.
+            vm.$get = function (key) { return this[key] }
+
+            it('should get correct args', function () {
+                assert.strictEqual(result.vars.length, vars.length)
+                for (var i = 0; i < vars.length; i++) {
+                    assert.strictEqual(vars[i], result.vars[i])
+                }
+            })
+
+            it('should generate correct getter function', function () {
+                var value = result.getter.call(vm)
+                assert.strictEqual(value, testCase.expectedValue)
+            })
+
+        })
+    }
+
+})