Skip to content

Commit b4902ae

Browse files
committed
unit test for new exp-parser implementation
1 parent bc0fd37 commit b4902ae

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

src/exp-parser.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,16 @@ function getVariables (code) {
3838
}
3939

4040
/**
41-
* Filter
41+
* A given path could potentially exist not on the
42+
* current compiler, but up in the parent chain somewhere.
43+
* This function generates an access relationship string
44+
* that can be used in the getter function by walking up
45+
* the parent chain to check for key existence.
46+
*
47+
* It stops at top parent if no vm in the chain has the
48+
* key. It then creates any missing bindings on the
49+
* final resolved vm.
4250
*/
43-
function filterUnique (vars) {
44-
var hash = utils.hash(),
45-
i = vars.length,
46-
key, res = []
47-
while (i--) {
48-
key = vars[i]
49-
if (hash[key]) continue
50-
hash[key] = 1
51-
res.push(key)
52-
}
53-
return res
54-
}
55-
5651
function getRel (path, compiler) {
5752
var rel = '',
5853
vm = compiler.vm,
@@ -108,7 +103,7 @@ module.exports = {
108103
if (!vars.length) {
109104
return makeGetter('return ' + exp, exp)
110105
}
111-
vars = filterUnique(vars)
106+
vars = utils.unique(vars)
112107
var pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g'),
113108
body = 'return ' + exp.replace(pathRE, function (path) {
114109
return 'this.' + getRel(path, compiler) + path

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ var utils = module.exports = {
7878
}
7979
},
8080

81+
/**
82+
* filter an array with duplicates into uniques
83+
*/
84+
unique: function (arr) {
85+
var hash = utils.hash(),
86+
i = arr.length,
87+
key, res = []
88+
while (i--) {
89+
key = arr[i]
90+
if (hash[key]) continue
91+
hash[key] = 1
92+
res.push(key)
93+
}
94+
return res
95+
},
96+
8197
/**
8298
* Convert a string template to a dom fragment
8399
*/

test/unit/specs/exp-parser.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,35 @@ describe('UNIT: Expression Parser', function () {
6666
function describeCase (testCase) {
6767
describe(testCase.exp, function () {
6868

69-
var result = ExpParser.parse(testCase.exp),
69+
var caughtMissingPaths = [],
70+
compilerMock = {
71+
vm:{
72+
$compiler:{
73+
bindings:{},
74+
createBinding: function (path) {
75+
caughtMissingPaths.push(path)
76+
}
77+
}
78+
}
79+
},
80+
getter = ExpParser.parse(testCase.exp, compilerMock),
7081
vm = testCase.vm,
7182
vars = testCase.paths || Object.keys(vm)
7283

7384
// mock the $get().
7485
// the real $get() will be tested in integration tests.
7586
vm.$get = function (key) { return this[key] }
7687

77-
it('should get correct args', function () {
88+
it('should get correct paths', function () {
7889
if (!vars.length) return
79-
assert.strictEqual(result.paths.length, vars.length)
90+
assert.strictEqual(caughtMissingPaths.length, vars.length)
8091
for (var i = 0; i < vars.length; i++) {
81-
assert.strictEqual(vars[i], result.paths[i])
92+
assert.strictEqual(vars[i], caughtMissingPaths[i])
8293
}
8394
})
8495

8596
it('should generate correct getter function', function () {
86-
var value = result.getter.call(vm)
97+
var value = getter.call(vm)
8798
assert.strictEqual(value, testCase.expectedValue)
8899
})
89100

@@ -100,7 +111,14 @@ describe('UNIT: Expression Parser', function () {
100111
utils.warn = function () {
101112
warned = true
102113
}
103-
ExpParser.parse('a + "fsef')
114+
ExpParser.parse('a + "fsef', {
115+
vm: {
116+
$compiler: {
117+
bindings: {},
118+
createBinding: function () {}
119+
}
120+
}
121+
})
104122
assert.ok(warned)
105123
utils.warn = oldWarn
106124
})

test/unit/specs/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ describe('UNIT: Utils', function () {
129129

130130
})
131131

132+
describe('unique', function () {
133+
134+
it('should filter an array with duplicates into unqiue ones', function () {
135+
var arr = [1, 2, 3, 1, 2, 3, 4, 5],
136+
res = utils.unique(arr),
137+
l = res.length
138+
assert.strictEqual(l, 5)
139+
while (l--) {
140+
assert.strictEqual(res[l], 5 - l)
141+
}
142+
})
143+
144+
})
145+
132146
describe('toFragment', function () {
133147

134148
it('should convert a string tempalte to a documentFragment', function () {

0 commit comments

Comments
 (0)