You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. Ext.test.session.addTest('Function', {
  8. name: 'Global Function Decorators',
  9. planned: 71,
  10. // 3
  11. test_createCallback: function(){
  12. var fn = function(a, b){
  13. Y.Assert.areEqual('a', a, 'Test passed param');
  14. Y.Assert.areEqual('b', b, 'Test passed param');
  15. return 'x';
  16. };
  17. var cb = fn.createCallback('a', 'b'),
  18. rt = cb(); // does not accept params
  19. Y.Assert.areEqual('x', rt, 'Test return value of callback');
  20. },
  21. // 24
  22. test_createDelegate: function(){
  23. var scope = {
  24. foo: 'bar'
  25. };
  26. var fn = function(a, b, c){
  27. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  28. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  29. Y.Assert.areEqual('a', a, 'Test passed param');
  30. Y.Assert.areEqual('b', b, 'Test passed param');
  31. Y.Assert.areEqual('c', c, 'Test passed param');
  32. return 'x';
  33. };
  34. var cb = fn.createDelegate(scope, ['b', 'c'], true /* appendArgs: true appends these to passed params */),
  35. rt = cb('a'); // a b c
  36. Y.Assert.areEqual('x', rt, 'Test return value');
  37. var cbTwo = fn.createDelegate(scope, ['a', 'b'], 0);
  38. rt = cbTwo('c'); // a b c
  39. Y.Assert.areEqual('x', rt, 'Test return value');
  40. var cbThree = fn.createDelegate(scope, ['b'], 1 /* this replaces at pos 1 */);
  41. rt = cbThree('a', 'c'); // a b c
  42. Y.Assert.areEqual('x', rt, 'Test return value');
  43. var cbFour = fn.createDelegate(scope, ['a', 'b', 'c']);
  44. rt = cbFour('x', 'y'); // overridden with a b c
  45. Y.Assert.areEqual('x', rt, 'Test return value');
  46. },
  47. // 19
  48. test_createInterceptor: function(){
  49. var scope = {
  50. foo: 'bar',
  51. n: 0
  52. };
  53. var fn = function(a, b){
  54. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  55. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  56. Y.Assert.areEqual('a', a, 'Test passed param');
  57. Y.Assert.areEqual('b', b, 'Test passed param');
  58. this.n++;
  59. return 'x';
  60. };
  61. // normal
  62. var rt = fn.call(scope, 'a', 'b'); // n 1
  63. Y.Assert.areEqual('x', rt, 'Test the return value');
  64. var cb = fn.createDelegate(scope).createInterceptor(function(a, b, z){
  65. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  66. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  67. Y.Assert.areEqual('a', a, 'Test passed param');
  68. Y.Assert.areEqual('b', b, 'Test passed param');
  69. return z === undefined;
  70. }, scope);
  71. // intercepted, but allowed to continue
  72. rt = cb('a', 'b'); // n 2
  73. Y.Assert.areEqual('x', rt, 'Test the return value');
  74. // intercepted, and cancelled
  75. cb('a', 'b', 'z');
  76. Y.Assert.areEqual(2, scope.n, 'Test the interceptor call count');
  77. },
  78. // 16
  79. test_createSequence: function(){
  80. var scope = {
  81. foo: 'bar',
  82. seq: 0
  83. };
  84. var fn = function(a, b){
  85. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  86. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  87. Y.Assert.areEqual('a', a, 'Test passed param');
  88. Y.Assert.areEqual('b', b, 'Test passed param');
  89. this.seq++;
  90. return 'x';
  91. };
  92. var rt = fn.call(scope, 'a', 'b'); // seq 1
  93. Y.Assert.areEqual('x', rt, 'Test the return value');
  94. Y.Assert.areEqual(1, scope.seq, 'Test the counter');
  95. var cb = fn.createDelegate(scope).createSequence(fn, scope);
  96. rt = cb('a', 'b'); // seq 2, 3
  97. Y.Assert.areEqual('x', rt, 'Test the return value');
  98. Y.Assert.areEqual(3, scope.seq, 'Test the number of times the sequence was called');
  99. },
  100. // 9
  101. test_defer: function(){
  102. var scope = {
  103. foo: 'bar',
  104. n: 0
  105. };
  106. var fn = function(a, b){
  107. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  108. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  109. Y.Assert.areEqual('a', a, 'Test passed param');
  110. Y.Assert.areEqual('b', b, 'Test passed param');
  111. this.n++;
  112. };
  113. fn.defer(1, scope, ['a', 'b']);
  114. fn.defer(2, scope, ['a', 'b']);
  115. setTimeout(function(){
  116. Y.Assert.areEqual(2, scope.n, 'Test if the counter matches the call timer count');
  117. }, 4);
  118. }
  119. });