@@ -473,6 +473,112 @@ describe('parser', function() {
473
473
} ) ;
474
474
475
475
476
+ it ( 'should call the function from the received instance and not from a new one' , function ( ) {
477
+ var n = 0 ;
478
+ scope . fn = function ( ) {
479
+ var c = n ++ ;
480
+ return { c : c , anotherFn : function ( ) { return this . c == c ; } } ;
481
+ } ;
482
+ expect ( scope . $eval ( 'fn().anotherFn()' ) ) . toBe ( true ) ;
483
+ } ) ;
484
+
485
+
486
+ it ( 'should call the function once when it is part of the context' , function ( ) {
487
+ var count = 0 ;
488
+ scope . fn = function ( ) {
489
+ count ++ ;
490
+ return { anotherFn : function ( ) { return "lucas" ; } } ;
491
+ } ;
492
+ expect ( scope . $eval ( 'fn().anotherFn()' ) ) . toBe ( 'lucas' ) ;
493
+ expect ( count ) . toBe ( 1 ) ;
494
+ } ) ;
495
+
496
+
497
+ it ( 'should call the function once when it is not part of the context' , function ( ) {
498
+ var count = 0 ;
499
+ scope . fn = function ( ) {
500
+ count ++ ;
501
+ return function ( ) { return 'lucas' ; } ;
502
+ } ;
503
+ expect ( scope . $eval ( 'fn()()' ) ) . toBe ( 'lucas' ) ;
504
+ expect ( count ) . toBe ( 1 ) ;
505
+ } ) ;
506
+
507
+
508
+ it ( 'should call the function once when it is not part of the context' , function ( ) {
509
+ var count = 0 ;
510
+ scope . fn = function ( ) {
511
+ count ++ ;
512
+ return function ( ) { return 'lucas' ; } ;
513
+ } ;
514
+ expect ( scope . $eval ( 'fn()()' ) ) . toBe ( 'lucas' ) ;
515
+ expect ( count ) . toBe ( 1 ) ;
516
+ } ) ;
517
+
518
+
519
+ it ( 'should call the function once when it is part of the context on assignments' , function ( ) {
520
+ var count = 0 ;
521
+ var element = { } ;
522
+ scope . fn = function ( ) {
523
+ count ++ ;
524
+ return element ;
525
+ } ;
526
+ expect ( scope . $eval ( 'fn().name = "lucas"' ) ) . toBe ( 'lucas' ) ;
527
+ expect ( element . name ) . toBe ( 'lucas' ) ;
528
+ expect ( count ) . toBe ( 1 ) ;
529
+ } ) ;
530
+
531
+
532
+ it ( 'should call the function once when it is part of the context on array lookups' , function ( ) {
533
+ var count = 0 ;
534
+ var element = [ ] ;
535
+ scope . fn = function ( ) {
536
+ count ++ ;
537
+ return element ;
538
+ } ;
539
+ expect ( scope . $eval ( 'fn()[0] = "lucas"' ) ) . toBe ( 'lucas' ) ;
540
+ expect ( element [ 0 ] ) . toBe ( 'lucas' ) ;
541
+ expect ( count ) . toBe ( 1 ) ;
542
+ } ) ;
543
+
544
+
545
+ it ( 'should call the function once when it is part of the context on array lookup function' , function ( ) {
546
+ var count = 0 ;
547
+ var element = [ { anotherFn : function ( ) { return 'lucas' ; } } ] ;
548
+ scope . fn = function ( ) {
549
+ count ++ ;
550
+ return element ;
551
+ } ;
552
+ expect ( scope . $eval ( 'fn()[0].anotherFn()' ) ) . toBe ( 'lucas' ) ;
553
+ expect ( count ) . toBe ( 1 ) ;
554
+ } ) ;
555
+
556
+
557
+ it ( 'should call the function once when it is part of the context on array lookup function' , function ( ) {
558
+ var count = 0 ;
559
+ var element = { name : { anotherFn : function ( ) { return 'lucas' ; } } } ;
560
+ scope . fn = function ( ) {
561
+ count ++ ;
562
+ return element ;
563
+ } ;
564
+ expect ( scope . $eval ( 'fn().name.anotherFn()' ) ) . toBe ( 'lucas' ) ;
565
+ expect ( count ) . toBe ( 1 ) ;
566
+ } ) ;
567
+
568
+
569
+ it ( 'should call the function once when it is part of a sub-expression' , function ( ) {
570
+ var count = 0 ;
571
+ scope . element = [ { } ] ;
572
+ scope . fn = function ( ) {
573
+ count ++ ;
574
+ return 0 ;
575
+ } ;
576
+ expect ( scope . $eval ( 'element[fn()].name = "lucas"' ) ) . toBe ( 'lucas' ) ;
577
+ expect ( scope . element [ 0 ] . name ) . toBe ( 'lucas' ) ;
578
+ expect ( count ) . toBe ( 1 ) ;
579
+ } ) ;
580
+
581
+
476
582
describe ( 'promises' , function ( ) {
477
583
var deferred , promise , q ;
478
584
0 commit comments