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.

159 lines
5.4 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.test.session.addTest('Function', {
  16. name: 'Global Function Decorators',
  17. planned: 71,
  18. // 3
  19. test_createCallback: function(){
  20. var fn = function(a, b){
  21. Y.Assert.areEqual('a', a, 'Test passed param');
  22. Y.Assert.areEqual('b', b, 'Test passed param');
  23. return 'x';
  24. };
  25. var cb = fn.createCallback('a', 'b'),
  26. rt = cb(); // does not accept params
  27. Y.Assert.areEqual('x', rt, 'Test return value of callback');
  28. },
  29. // 24
  30. test_createDelegate: function(){
  31. var scope = {
  32. foo: 'bar'
  33. };
  34. var fn = function(a, b, c){
  35. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  36. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  37. Y.Assert.areEqual('a', a, 'Test passed param');
  38. Y.Assert.areEqual('b', b, 'Test passed param');
  39. Y.Assert.areEqual('c', c, 'Test passed param');
  40. return 'x';
  41. };
  42. var cb = fn.createDelegate(scope, ['b', 'c'], true /* appendArgs: true appends these to passed params */),
  43. rt = cb('a'); // a b c
  44. Y.Assert.areEqual('x', rt, 'Test return value');
  45. var cbTwo = fn.createDelegate(scope, ['a', 'b'], 0);
  46. rt = cbTwo('c'); // a b c
  47. Y.Assert.areEqual('x', rt, 'Test return value');
  48. var cbThree = fn.createDelegate(scope, ['b'], 1 /* this replaces at pos 1 */);
  49. rt = cbThree('a', 'c'); // a b c
  50. Y.Assert.areEqual('x', rt, 'Test return value');
  51. var cbFour = fn.createDelegate(scope, ['a', 'b', 'c']);
  52. rt = cbFour('x', 'y'); // overridden with a b c
  53. Y.Assert.areEqual('x', rt, 'Test return value');
  54. },
  55. // 19
  56. test_createInterceptor: function(){
  57. var scope = {
  58. foo: 'bar',
  59. n: 0
  60. };
  61. var fn = function(a, b){
  62. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  63. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  64. Y.Assert.areEqual('a', a, 'Test passed param');
  65. Y.Assert.areEqual('b', b, 'Test passed param');
  66. this.n++;
  67. return 'x';
  68. };
  69. // normal
  70. var rt = fn.call(scope, 'a', 'b'); // n 1
  71. Y.Assert.areEqual('x', rt, 'Test the return value');
  72. var cb = fn.createDelegate(scope).createInterceptor(function(a, b, z){
  73. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  74. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  75. Y.Assert.areEqual('a', a, 'Test passed param');
  76. Y.Assert.areEqual('b', b, 'Test passed param');
  77. return z === undefined;
  78. }, scope);
  79. // intercepted, but allowed to continue
  80. rt = cb('a', 'b'); // n 2
  81. Y.Assert.areEqual('x', rt, 'Test the return value');
  82. // intercepted, and cancelled
  83. cb('a', 'b', 'z');
  84. Y.Assert.areEqual(2, scope.n, 'Test the interceptor call count');
  85. },
  86. // 16
  87. test_createSequence: function(){
  88. var scope = {
  89. foo: 'bar',
  90. seq: 0
  91. };
  92. var fn = function(a, b){
  93. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  94. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  95. Y.Assert.areEqual('a', a, 'Test passed param');
  96. Y.Assert.areEqual('b', b, 'Test passed param');
  97. this.seq++;
  98. return 'x';
  99. };
  100. var rt = fn.call(scope, 'a', 'b'); // seq 1
  101. Y.Assert.areEqual('x', rt, 'Test the return value');
  102. Y.Assert.areEqual(1, scope.seq, 'Test the counter');
  103. var cb = fn.createDelegate(scope).createSequence(fn, scope);
  104. rt = cb('a', 'b'); // seq 2, 3
  105. Y.Assert.areEqual('x', rt, 'Test the return value');
  106. Y.Assert.areEqual(3, scope.seq, 'Test the number of times the sequence was called');
  107. },
  108. // 9
  109. test_defer: function(){
  110. var scope = {
  111. foo: 'bar',
  112. n: 0
  113. };
  114. var fn = function(a, b){
  115. Y.Assert.areEqual(scope, this, 'Test if "this" is correct');
  116. Y.Assert.areEqual('bar', this.foo, 'Test if property matches');
  117. Y.Assert.areEqual('a', a, 'Test passed param');
  118. Y.Assert.areEqual('b', b, 'Test passed param');
  119. this.n++;
  120. };
  121. fn.defer(1, scope, ['a', 'b']);
  122. fn.defer(2, scope, ['a', 'b']);
  123. setTimeout(function(){
  124. Y.Assert.areEqual(2, scope.n, 'Test if the counter matches the call timer count');
  125. }, 4);
  126. }
  127. });