@@ -87,31 +87,6 @@ describe('Expression Parser', function () {
8787
8888 testCases . forEach ( describeCase )
8989
90- // extra case for invalid expressions
91- describe ( 'invalid expression' , function ( ) {
92-
93- before ( warnSpy . swapWarn )
94-
95- it ( 'should capture the error and warn' , function ( ) {
96- function noop ( ) { }
97- ExpParser . parse ( 'a + "fsef' , {
98- createBinding : noop ,
99- hasKey : noop ,
100- vm : {
101- $compiler : {
102- bindings : { } ,
103- createBinding : noop
104- } ,
105- $data : { }
106- }
107- } )
108- assert . ok ( warnSpy . warned )
109- } )
110-
111- after ( warnSpy . resetWarn )
112-
113- } )
114-
11590 describe ( 'XSS protection' , function ( ) {
11691
11792 var cases = [
@@ -141,6 +116,98 @@ describe('Expression Parser', function () {
141116
142117 } )
143118
119+ describe ( 'scope trace' , function ( ) {
120+
121+ it ( 'should determine the correct scope for variables' , function ( ) {
122+
123+ var bindingsCreated = { }
124+
125+ var getter = ExpParser . parse ( 'a + b' , mockCompiler ( {
126+ parent : {
127+ bindings : { } ,
128+ createBinding : function ( key ) {
129+ assert . strictEqual ( key , 'a' )
130+ bindingsCreated [ key ] = true
131+ } ,
132+ hasKey : function ( key ) {
133+ return key === 'a'
134+ } ,
135+ parent : {
136+ bindings : { } ,
137+ createBinding : function ( key ) {
138+ assert . strictEqual ( key , 'b' )
139+ bindingsCreated [ key ] = true
140+ } ,
141+ hasKey : function ( key ) {
142+ return key === 'b'
143+ }
144+ }
145+ }
146+ } ) )
147+ var getterString = getter . toString ( )
148+ assert . ok ( getterString . indexOf ( 'this.$parent.a' ) > - 1 )
149+ assert . ok ( getterString . indexOf ( 'this.$parent.$parent.b' ) > - 1 )
150+ } )
151+
152+ } )
153+
154+ // extra case for invalid expressions
155+ describe ( 'invalid expression' , function ( ) {
156+
157+ before ( warnSpy . swapWarn )
158+
159+ it ( 'should capture the error and warn' , function ( ) {
160+ ExpParser . parse ( 'a + "fsef' , mockCompiler ( ) )
161+ assert . ok ( warnSpy . warned )
162+ } )
163+
164+ after ( warnSpy . resetWarn )
165+
166+ } )
167+
168+ describe ( '.eval() with extra data' , function ( ) {
169+
170+ it ( 'should be able to eval an epxression with temporary additional data' , function ( ) {
171+ var res = ExpParser . eval ( 'a + b' , mockCompiler ( ) , { a : 1 , b : 2 } )
172+ assert . strictEqual ( res , 3 )
173+ } )
174+
175+ } )
176+
177+ describe ( 'computed filters' , function ( ) {
178+
179+ it ( 'should wrap expression with computed filters' , function ( ) {
180+
181+ var filters = [
182+ { name : 'test' , args : [ 'a' , 'b' ] } ,
183+ { name : 'wrap' , args : [ 'c' , 'd' ] }
184+ ] ,
185+ filterFns = {
186+ test : function ( v , a , b ) {
187+ return v + a + b
188+ } ,
189+ wrap : function ( v , c , d ) {
190+ return v + c + d
191+ }
192+ }
193+
194+ var compiler = mockCompiler ( {
195+ getOption : function ( type , id ) {
196+ return filterFns [ id ]
197+ }
198+ } )
199+
200+ var getter = ExpParser . parse ( 'a + b' , compiler , null , filters )
201+ var res = getter . call ( {
202+ $compiler : compiler ,
203+ a : 1 ,
204+ b : 2
205+ } )
206+ assert . strictEqual ( res , '3abcd' )
207+ } )
208+
209+ } )
210+
144211 function describeCase ( testCase ) {
145212 describe ( testCase . exp , function ( ) {
146213
@@ -196,4 +263,24 @@ describe('Expression Parser', function () {
196263 } )
197264 }
198265
266+ function noop ( ) { }
267+
268+ function mockCompiler ( opts ) {
269+ var mock = {
270+ createBinding : noop ,
271+ hasKey : noop ,
272+ vm : {
273+ $compiler : {
274+ bindings : { } ,
275+ createBinding : noop
276+ } ,
277+ $data : { }
278+ }
279+ }
280+ for ( var key in opts ) {
281+ mock [ key ] = opts [ key ]
282+ }
283+ return mock
284+ }
285+
199286} )
0 commit comments