unit test for new exp-parser implementation

This commit is contained in:
Evan You 2013-11-18 15:14:54 -05:00
parent bc0fd377d5
commit b4902ae757
4 changed files with 64 additions and 21 deletions

View File

@ -38,21 +38,16 @@ function getVariables (code) {
}
/**
* Filter
* A given path could potentially exist not on the
* current compiler, but up in the parent chain somewhere.
* This function generates an access relationship string
* that can be used in the getter function by walking up
* the parent chain to check for key existence.
*
* It stops at top parent if no vm in the chain has the
* key. It then creates any missing bindings on the
* final resolved vm.
*/
function filterUnique (vars) {
var hash = utils.hash(),
i = vars.length,
key, res = []
while (i--) {
key = vars[i]
if (hash[key]) continue
hash[key] = 1
res.push(key)
}
return res
}
function getRel (path, compiler) {
var rel = '',
vm = compiler.vm,
@ -108,7 +103,7 @@ module.exports = {
if (!vars.length) {
return makeGetter('return ' + exp, exp)
}
vars = filterUnique(vars)
vars = utils.unique(vars)
var pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g'),
body = 'return ' + exp.replace(pathRE, function (path) {
return 'this.' + getRel(path, compiler) + path

View File

@ -78,6 +78,22 @@ var utils = module.exports = {
}
},
/**
* filter an array with duplicates into uniques
*/
unique: function (arr) {
var hash = utils.hash(),
i = arr.length,
key, res = []
while (i--) {
key = arr[i]
if (hash[key]) continue
hash[key] = 1
res.push(key)
}
return res
},
/**
* Convert a string template to a dom fragment
*/

View File

@ -66,7 +66,18 @@ describe('UNIT: Expression Parser', function () {
function describeCase (testCase) {
describe(testCase.exp, function () {
var result = ExpParser.parse(testCase.exp),
var caughtMissingPaths = [],
compilerMock = {
vm:{
$compiler:{
bindings:{},
createBinding: function (path) {
caughtMissingPaths.push(path)
}
}
}
},
getter = ExpParser.parse(testCase.exp, compilerMock),
vm = testCase.vm,
vars = testCase.paths || Object.keys(vm)
@ -74,16 +85,16 @@ describe('UNIT: Expression Parser', function () {
// the real $get() will be tested in integration tests.
vm.$get = function (key) { return this[key] }
it('should get correct args', function () {
it('should get correct paths', function () {
if (!vars.length) return
assert.strictEqual(result.paths.length, vars.length)
assert.strictEqual(caughtMissingPaths.length, vars.length)
for (var i = 0; i < vars.length; i++) {
assert.strictEqual(vars[i], result.paths[i])
assert.strictEqual(vars[i], caughtMissingPaths[i])
}
})
it('should generate correct getter function', function () {
var value = result.getter.call(vm)
var value = getter.call(vm)
assert.strictEqual(value, testCase.expectedValue)
})
@ -100,7 +111,14 @@ describe('UNIT: Expression Parser', function () {
utils.warn = function () {
warned = true
}
ExpParser.parse('a + "fsef')
ExpParser.parse('a + "fsef', {
vm: {
$compiler: {
bindings: {},
createBinding: function () {}
}
}
})
assert.ok(warned)
utils.warn = oldWarn
})

View File

@ -129,6 +129,20 @@ describe('UNIT: Utils', function () {
})
describe('unique', function () {
it('should filter an array with duplicates into unqiue ones', function () {
var arr = [1, 2, 3, 1, 2, 3, 4, 5],
res = utils.unique(arr),
l = res.length
assert.strictEqual(l, 5)
while (l--) {
assert.strictEqual(res[l], 5 - l)
}
})
})
describe('toFragment', function () {
it('should convert a string tempalte to a documentFragment', function () {