core.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. module('core');
  2. test('V', function() {
  3. ok(/^\d+$/.test(K.V));
  4. });
  5. test('each', function() {
  6. var arr = ['a', 'b'];
  7. var obj = {a : 'aa', b : 'bb', c : 0, d : null};
  8. var i = 0;
  9. K.each(arr, function(key, val) {
  10. if (key === 0) ok(val === 'a');
  11. if (key === 1) ok(val === 'b');
  12. i++;
  13. });
  14. ok(i === 2);
  15. i = 0;
  16. K.each(arr, function(idx) {
  17. if (idx === 0) ok(this == 'a');
  18. if (idx === 1) ok(this == 'b');
  19. i++;
  20. });
  21. ok(i === 2);
  22. i = 0;
  23. K.each(obj, function(key, val) {
  24. if (key === 'a') ok(val === 'aa');
  25. if (key === 'b') ok(val === 'bb');
  26. if (key === 'c') ok(val === 0);
  27. if (key === 'd') ok(val === null);
  28. i++;
  29. });
  30. ok(i === 4);
  31. i = 0;
  32. K.each(obj, function(key) {
  33. if (key === 'a') ok(this == 'aa');
  34. if (key === 'b') ok(this == 'bb');
  35. if (key === 'c') ok(this == 0);
  36. i++;
  37. });
  38. ok(i === 4);
  39. i = 0;
  40. K.each(arr, function(key, val) {
  41. i++;
  42. return false;
  43. });
  44. ok(i === 1);
  45. i = 0;
  46. K.each(obj, function(key, val) {
  47. i++;
  48. return true;
  49. });
  50. ok(i === 4);
  51. });
  52. test('isArray', function() {
  53. ok(K.isArray([]) === true);
  54. ok(K.isArray(['a', 'b']) === true);
  55. ok(K.isArray({a : 'a'}) === false);
  56. ok(K.isArray(null) === false);
  57. ok(K.isArray(1) === false);
  58. ok(K.isArray('a') === false);
  59. ok(K.isArray(0) === false);
  60. ok(K.isArray('') === false);
  61. });
  62. test('inArray', function() {
  63. arr = [null, 0, '', 10, '11', true];
  64. ok(K.inArray(null, arr) === 0);
  65. ok(K.inArray(0, arr) === 1);
  66. ok(K.inArray('', arr) === 2);
  67. ok(K.inArray(10, arr) === 3);
  68. ok(K.inArray(11, arr) === -1);
  69. ok(K.inArray('11', arr) === 4);
  70. ok(K.inArray(true, arr) === 5);
  71. ok(K.inArray(false, arr) === -1);
  72. });
  73. test('trim', function() {
  74. equals(K.trim(' a '), 'a');
  75. equals(K.trim(' a a '), 'a a');
  76. equals(K.trim(' \xa0 '), '\xa0');
  77. });
  78. test('addUnit', function() {
  79. ok(K.addUnit() === undefined);
  80. ok(K.addUnit(null) === null);
  81. ok(K.addUnit(0) === 0);
  82. equals(K.addUnit(100), '100px');
  83. equals(K.addUnit('100px'), '100px');
  84. equals(K.addUnit('100%'), '100%');
  85. equals(K.addUnit(100, 'em'), '100em');
  86. });
  87. test('removeUnit', function() {
  88. ok(K.removeUnit() === 0);
  89. ok(K.removeUnit(null) === 0);
  90. ok(K.removeUnit(0) === 0);
  91. equals(K.removeUnit(100), 100);
  92. equals(K.removeUnit('100px'), 100);
  93. });
  94. test('escape', function() {
  95. same(K.escape('<div id="abc">&</div>'), '&lt;div id=&quot;abc&quot;&gt;&amp;&lt;/div&gt;');
  96. });
  97. test('unescape', function() {
  98. same(K.unescape('&lt;div id=&quot;abc&quot;&gt;&amp;&lt;/div&gt;'), '<div id="abc">&</div>');
  99. });
  100. test('toHex', function() {
  101. equals(K.toHex('rgb(0, 0, 0)'), '#000000');
  102. equals(K.toHex('rgb(0, 0, 0)'), '#000000');
  103. equals(K.toHex(' rgb(0, 0, 0) rgb (255, 255, 255) '), ' #000000 #FFFFFF ');
  104. });
  105. test('toMap', function() {
  106. same(K.toMap('a,b'), {a : true, b : true});
  107. same(K.toMap('a,1..3,b'), {a : true, '1' : true, '2' : true, '3' : true, b : true});
  108. });
  109. test('toArray', function() {
  110. same(K.toArray([1, 2]), [1, 2]);
  111. });
  112. test('undef', function() {
  113. same(K.undef(1, 0), 1);
  114. var obj = {};
  115. same(K.undef(obj.aaa, 0), 0);
  116. obj.aaa = 1;
  117. same(K.undef(obj.aaa, 0), 1);
  118. });
  119. test('invalidUrl', function() {
  120. ok(K.invalidUrl('http://www.kindsoft.net/') === false);
  121. ok(K.invalidUrl('http://www.kindsoft.net/<br>') === true);
  122. ok(K.invalidUrl('http://www.kindsoft.net/"abcd"') === true);
  123. ok(K.invalidUrl('http://www.kindsoft.net/<b>abcd</b>') === true);
  124. });
  125. test('addParam', function() {
  126. same(K.addParam('upload.php', 'b=2'), 'upload.php?b=2');
  127. same(K.addParam('upload.php?a=1', 'b=2'), 'upload.php?a=1&b=2');
  128. });
  129. test('extend', function() {
  130. function Parent() {
  131. this.init();
  132. }
  133. K.extend(Parent, {
  134. sex : 'sex',
  135. type : 'person',
  136. init : function() {
  137. this.name = 'parent';
  138. console.log(this.name + ': constructor');
  139. },
  140. getSex : function() {
  141. return this.name + ': ' + this.sex;
  142. },
  143. getType : function() {
  144. return this.type;
  145. },
  146. say : function() {
  147. return this.name + ': say()';
  148. },
  149. run : function() {
  150. return this.name + ': run()';
  151. }
  152. });
  153. function Child() {
  154. this.init();
  155. }
  156. K.extend(Child, Parent, {
  157. init : function() {
  158. this.name = 'child';
  159. Child.parent.init();
  160. console.log(this.name + ': constructor');
  161. },
  162. run : function() {
  163. return this.name + ': run()';
  164. },
  165. walk : function() {
  166. return this.name + ': walk()';
  167. }
  168. });
  169. var child = new Child();
  170. console.log(Parent.prototype.constructor.valueOf());
  171. console.log(Child.prototype.constructor.valueOf());
  172. equals(child.sex, 'sex');
  173. equals(child.name, 'child');
  174. equals(child.type, 'person');
  175. equals(child.getSex(), 'child: sex');
  176. equals(child.getType(), 'person');
  177. equals(child.say(), 'child: say()');
  178. equals(child.run(), 'child: run()');
  179. equals(child.walk(), 'child: walk()');
  180. });