11import { ddescribe , describe , it , xit , iit , expect , beforeEach } from 'test_lib/test_lib' ;
2- import { BaseException , isBlank } from 'facade/lang' ;
3- import { MapWrapper } from 'facade/collection' ;
2+ import { BaseException , isBlank , isPresent } from 'facade/lang' ;
3+ import { MapWrapper , ListWrapper } from 'facade/collection' ;
44import { Parser } from 'change_detection/parser/parser' ;
55import { Lexer } from 'change_detection/parser/lexer' ;
66import { Formatter , LiteralPrimitive } from 'change_detection/parser/ast' ;
@@ -32,11 +32,15 @@ export function main() {
3232 }
3333
3434 function parseAction ( text ) {
35- return createParser ( ) . parseAction ( text ) ;
35+ return createParser ( ) . parseAction ( text ) . ast ;
3636 }
3737
3838 function parseBinding ( text ) {
39- return createParser ( ) . parseBinding ( text ) ;
39+ return createParser ( ) . parseBinding ( text ) . ast ;
40+ }
41+
42+ function parseTemplateBindings ( text ) {
43+ return createParser ( ) . parseTemplateBindings ( text ) ;
4044 }
4145
4246 function expectEval ( text , passedInContext = null ) {
@@ -48,6 +52,15 @@ export function main() {
4852 return expect ( ( ) => parseAction ( text ) . eval ( td ( ) ) ) ;
4953 }
5054
55+ function evalAsts ( asts , passedInContext = null ) {
56+ var c = isBlank ( passedInContext ) ? td ( ) : passedInContext ;
57+ var res = [ ] ;
58+ for ( var i = 0 ; i < asts . length ; i ++ ) {
59+ ListWrapper . push ( res , asts [ i ] . eval ( c ) ) ;
60+ }
61+ return res ;
62+ }
63+
5164 describe ( "parser" , ( ) => {
5265 describe ( "parseAction" , ( ) => {
5366 describe ( "basic expressions" , ( ) => {
@@ -248,7 +261,7 @@ export function main() {
248261 expectEval ( 'a["key"] = 200' , context ) . toEqual ( 200 ) ;
249262 expect ( MapWrapper . get ( context . a , "key" ) ) . toEqual ( 200 ) ;
250263 } ) ;
251-
264+
252265 it ( "should support array/map updates" , ( ) => {
253266 var context = td ( [ MapWrapper . createFromPairs ( [ [ "key" , 100 ] ] ) ] ) ;
254267 expectEval ( 'a[0]["key"] = 200' , context ) . toEqual ( 200 ) ;
@@ -287,7 +300,7 @@ export function main() {
287300
288301 it ( 'should pass exceptions' , ( ) => {
289302 expect ( ( ) => {
290- createParser ( ) . parseAction ( 'a()' ) . eval ( td ( ( ) => { throw new BaseException ( "boo to you" ) } ) ) ;
303+ createParser ( ) . parseAction ( 'a()' ) . ast . eval ( td ( ( ) => { throw new BaseException ( "boo to you" ) } ) ) ;
291304 } ) . toThrowError ( 'boo to you' ) ;
292305 } ) ;
293306
@@ -297,6 +310,10 @@ export function main() {
297310 expectEval ( "1;;" ) . toEqual ( 1 ) ;
298311 } ) ;
299312 } ) ;
313+
314+ it ( 'should store the source in the result' , ( ) => {
315+ expect ( createParser ( ) . parseAction ( 'someExpr' ) . source ) . toBe ( 'someExpr' ) ;
316+ } ) ;
300317 } ) ;
301318
302319 describe ( "parseBinding" , ( ) => {
@@ -319,6 +336,11 @@ export function main() {
319336 expect ( ( ) => parseBinding ( '"Foo"|1234' ) ) . toThrowError ( new RegExp ( 'identifier or keyword' ) ) ;
320337 expect ( ( ) => parseBinding ( '"Foo"|"uppercase"' ) ) . toThrowError ( new RegExp ( 'identifier or keyword' ) ) ;
321338 } ) ;
339+
340+ } ) ;
341+
342+ it ( 'should store the source in the result' , ( ) => {
343+ expect ( createParser ( ) . parseBinding ( 'someExpr' ) . source ) . toBe ( 'someExpr' ) ;
322344 } ) ;
323345
324346 it ( 'should throw on chain expressions' , ( ) => {
@@ -329,6 +351,90 @@ export function main() {
329351 expect ( ( ) => parseBinding ( "1;2" ) ) . toThrowError ( new RegExp ( "contain chained expression" ) ) ;
330352 } ) ;
331353 } ) ;
354+
355+ describe ( 'parseTemplateBindings' , ( ) => {
356+
357+ function keys ( templateBindings ) {
358+ return ListWrapper . map ( templateBindings , ( binding ) => binding . key ) ;
359+ }
360+
361+ function names ( templateBindings ) {
362+ return ListWrapper . map ( templateBindings , ( binding ) => binding . name ) ;
363+ }
364+
365+ function exprSources ( templateBindings ) {
366+ return ListWrapper . map ( templateBindings ,
367+ ( binding ) => isPresent ( binding . expression ) ? binding . expression . source : null ) ;
368+ }
369+
370+ function exprAsts ( templateBindings ) {
371+ return ListWrapper . map ( templateBindings ,
372+ ( binding ) => isPresent ( binding . expression ) ? binding . expression . ast : null ) ;
373+ }
374+
375+ it ( 'should parse an empty string' , ( ) => {
376+ var bindings = parseTemplateBindings ( "" ) ;
377+ expect ( bindings ) . toEqual ( [ ] ) ;
378+ } ) ;
379+
380+ it ( 'should only allow identifier, string, or keyword as keys' , ( ) => {
381+ var bindings = parseTemplateBindings ( "a:'b'" ) ;
382+ expect ( keys ( bindings ) ) . toEqual ( [ 'a' ] ) ;
383+
384+ bindings = parseTemplateBindings ( "'a':'b'" ) ;
385+ expect ( keys ( bindings ) ) . toEqual ( [ 'a' ] ) ;
386+
387+ bindings = parseTemplateBindings ( "\"a\":'b'" ) ;
388+ expect ( keys ( bindings ) ) . toEqual ( [ 'a' ] ) ;
389+
390+ expect ( ( ) => {
391+ parseTemplateBindings ( '(:0' ) ;
392+ } ) . toThrowError ( new RegExp ( 'expected identifier, keyword, or string' ) ) ;
393+
394+ expect ( ( ) => {
395+ parseTemplateBindings ( '1234:0' ) ;
396+ } ) . toThrowError ( new RegExp ( 'expected identifier, keyword, or string' ) ) ;
397+ } ) ;
398+
399+ it ( 'should detect expressions as value' , ( ) => {
400+ var bindings = parseTemplateBindings ( "a:b" ) ;
401+ expect ( exprSources ( bindings ) ) . toEqual ( [ 'b' ] ) ;
402+ expect ( evalAsts ( exprAsts ( bindings ) , td ( 0 , 23 ) ) ) . toEqual ( [ 23 ] ) ;
403+
404+ bindings = parseTemplateBindings ( "a:1+1" ) ;
405+ expect ( exprSources ( bindings ) ) . toEqual ( [ '1+1' ] ) ;
406+ expect ( evalAsts ( exprAsts ( bindings ) ) ) . toEqual ( [ 2 ] ) ;
407+ } ) ;
408+
409+ it ( 'should detect names as value' , ( ) => {
410+ var bindings = parseTemplateBindings ( "a:#b" ) ;
411+ expect ( names ( bindings ) ) . toEqual ( [ 'b' ] ) ;
412+ expect ( exprSources ( bindings ) ) . toEqual ( [ null ] ) ;
413+ expect ( exprAsts ( bindings ) ) . toEqual ( [ null ] ) ;
414+ } ) ;
415+
416+ it ( 'should allow space and colon as separators' , ( ) => {
417+ var bindings = parseTemplateBindings ( "a:b" ) ;
418+ expect ( keys ( bindings ) ) . toEqual ( [ 'a' ] ) ;
419+ expect ( exprSources ( bindings ) ) . toEqual ( [ 'b' ] ) ;
420+
421+ bindings = parseTemplateBindings ( "a b" ) ;
422+ expect ( keys ( bindings ) ) . toEqual ( [ 'a' ] ) ;
423+ expect ( exprSources ( bindings ) ) . toEqual ( [ 'b' ] ) ;
424+ } ) ;
425+
426+ it ( 'should allow multiple pairs' , ( ) => {
427+ var bindings = parseTemplateBindings ( "a 1 b 2" ) ;
428+ expect ( keys ( bindings ) ) . toEqual ( [ 'a' , 'b' ] ) ;
429+ expect ( exprSources ( bindings ) ) . toEqual ( [ '1 ' , '2' ] ) ;
430+ } ) ;
431+
432+ it ( 'should store the sources in the result' , ( ) => {
433+ var bindings = parseTemplateBindings ( "a 1,b 2" ) ;
434+ expect ( bindings [ 0 ] . expression . source ) . toEqual ( '1' ) ;
435+ expect ( bindings [ 1 ] . expression . source ) . toEqual ( '2' ) ;
436+ } ) ;
437+ } ) ;
332438 } ) ;
333439}
334440
0 commit comments