PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/TestUtil.hx

http://github.com/andyli/casahx
Haxe | 900 lines | 653 code | 227 blank | 20 comment | 52 complexity | e93781085738206e0e8e7bc1d5ca9f1c MD5 | raw file
  1. package test;
  2. import haxe.unit.TestCase;
  3. #if !php import flash.display.Sprite; #end
  4. import flash.geom.Rectangle;
  5. import flash.geom.Point;
  6. import org.casalib.core.UInt;
  7. import org.casalib.math.Percent;
  8. #if !(php || cpp || neko) import org.casalib.util.AlignUtil; #end
  9. import org.casalib.util.ArrayUtil;
  10. import org.casalib.util.ClassUtil;
  11. import org.casalib.util.ColorUtil;
  12. import org.casalib.util.ConversionUtil;
  13. import org.casalib.util.DateUtil;
  14. #if !php import org.casalib.util.DisplayObjectUtil; #end
  15. #if !php import org.casalib.util.DrawUtil; #end
  16. #if flash import org.casalib.util.FlashVarUtil; #end
  17. #if flash import org.casalib.util.FrameUtil; #end
  18. import org.casalib.util.GeomUtil;
  19. #if flash import org.casalib.util.LibraryManager; #end
  20. import org.casalib.util.LoadUtil;
  21. #if !php import org.casalib.util.LocationUtil; #end
  22. #if !php import org.casalib.util.NavigateUtil; #end
  23. import org.casalib.util.NumberUtil;
  24. import org.casalib.util.ObjectUtil;
  25. import org.casalib.util.PropertySetter;
  26. import org.casalib.util.QueryStringUtil;
  27. import org.casalib.util.RatioUtil;
  28. import org.casalib.util.SingletonUtil;
  29. #if !php import org.casalib.util.StageReference; #end
  30. import org.casalib.util.StringUtil;
  31. #if !php import org.casalib.util.TextFieldUtil; #end
  32. #if flash import org.casalib.util.UrlVariablesUtil; #end
  33. import org.casalib.util.ValidationUtil;
  34. //below modified from AS3's doc
  35. private class User {
  36. public var name:String;
  37. public var age:Float;
  38. public function new(name:String, age:UInt) {
  39. this.name = name;
  40. this.age = age;
  41. }
  42. public function toString():String {
  43. return this.name + ":" + this.age;
  44. }
  45. }
  46. class TestUtil extends TestCase {
  47. #if flash
  48. public function testAlignUtil():Void {
  49. //just run all function once...
  50. var d = new Sprite();
  51. var b = new Rectangle(0,0,100,100);
  52. flash.Lib.current.addChild(d);
  53. AlignUtil.alignBottom(d,b);
  54. AlignUtil.alignCenter(d,b);
  55. AlignUtil.alignMiddleCenter(d,b);
  56. AlignUtil.alignLeft(d,b);
  57. this.assertEquals(b.x,d.x);
  58. AlignUtil.alignMiddle(d,b);
  59. AlignUtil.alignRight(d,b);
  60. AlignUtil.alignTop(d,b);
  61. this.assertEquals(b.y,d.y);
  62. }
  63. #end
  64. function dumpAry(ary:Array<Dynamic>,?field:String):String {
  65. var str:String = "[";
  66. for (i in 0...ary.length){
  67. str += field == null || ObjectUtil.isEmpty(ary[i]) ? ary[i] : Reflect.field(ary[i],field);
  68. str += i == ary.length-1?"":",";
  69. }
  70. return str + "]";
  71. }
  72. function dumpUsers(users:Array<Dynamic>):String {
  73. var ary:Array<String> = [];
  74. for (user in users) {
  75. ary.push(user.toString());
  76. }
  77. return ary.join(",");
  78. }
  79. public function testArrayUtil():Void {
  80. var a1:Array<Int> = [1,6,3,3,8];
  81. this.assertEquals(2,ArrayUtil.indexOf(a1,3));
  82. this.assertEquals(3,ArrayUtil.indexOf(a1,3,3));
  83. this.assertEquals(3,ArrayUtil.lastIndexOf(a1,3));
  84. this.assertEquals(2,ArrayUtil.lastIndexOf(a1,3,2));
  85. var alphabet:Array<String> = ["a", "d", "e"];
  86. var parts:Array<String> = ["b", "c"];
  87. this.assertTrue(ArrayUtil.addItemsAt(alphabet, parts, 1));
  88. this.assertEquals(5,alphabet.length);
  89. this.assertEquals("a,b,c,d,e",alphabet.join(','));
  90. this.assertTrue(4 == ArrayUtil.average([2, 3, 8, 3]));
  91. this.assertEquals(3, ArrayUtil.contains([1, 2, 3, 7, 7, 7, 4, 5],7));
  92. this.assertTrue(ArrayUtil.containsAll([1,2,3,4,5],[1,3,5]));
  93. this.assertFalse(ArrayUtil.containsAll([1,2,3,4,5],[1,3,6]));
  94. this.assertTrue(ArrayUtil.containsAny([1,2,3,4,5],[1,6,5]));
  95. this.assertFalse(ArrayUtil.containsAny([1,2,3,4,5],[-1,0,6]));
  96. this.assertTrue(ArrayUtil.equals([1,2,-1],[1,2,-1]));
  97. this.assertFalse(ArrayUtil.equals([1,2,-1],[1,2,1]));
  98. this.assertTrue(10 == ArrayUtil.getHighestValue([-123,2,5,6,2,10,-0.11]));
  99. this.assertEquals(3,ArrayUtil.getIndexOfDifference(["Red", "Blue", "Green", "Indigo", "Violet"],["Red", "Blue", "Green", "Violet"]));
  100. var item = {a:123, b:456};
  101. this.assertEquals(item, ArrayUtil.getItemByKey([{a:0,b:12},{a:0,b:15},item],"b",456));
  102. var array = [1,2,3,4,5,5,7,5,9,0,11,12,13];
  103. this.assertEquals(array, ArrayUtil.getItemByType([14,array,"","asdfn"],Type.getClass(array)));
  104. this.assertEquals(3, ArrayUtil.getItemsByKey(([{b:12},{b:15},item,{b:15},{b:15}]:Array<Dynamic>),"b",15).length);
  105. this.assertEquals(2, ArrayUtil.getItemsByType([14,array,"","asdfn"],String).length);
  106. this.assertTrue(1 == ArrayUtil.getLowestValue([2, 1, 5, 4, 3]));
  107. this.assertFalse(ArrayUtil.equals(ArrayUtil.randomize(array),array));
  108. this.assertEquals(4,ArrayUtil.removeDuplicates(a1).length);
  109. var testAry:Array<Int> = [1,2,1,2];
  110. this.assertEquals(2,ArrayUtil.removeDuplicates(testAry).length);
  111. this.assertEquals(3,ArrayUtil.removeItem(array,5));
  112. var arrayDyna:Array<Dynamic> = [1,2,3,4,5,5,[],5,9,0,11,{a:0},13];
  113. this.assertEquals(3,ArrayUtil.removeItem(arrayDyna,5));
  114. ArrayUtil.removeItems(array,[1,11]);
  115. this.assertEquals(8,array.length);
  116. ArrayUtil.retainItems(array,[0]);
  117. this.assertEquals(0,array[0]);
  118. this.assertEquals(5.0,ArrayUtil.sum([1,1,1,0.25,1.75]));
  119. a1 = [1,2,3,4,5];
  120. this.assertEquals(1, ArrayUtil.contains(a1,ArrayUtil.random(a1)));
  121. var ta = [{name: "Aaron", sex: "Male", hair: "Brown"}, {name: "Linda", sex: "Female", hair: "Blonde"}, {name: "Katie", sex: "Female", hair: "Brown"}, {name: "Nikki", sex: "Female", hair: "Blonde"}];
  122. this.assertEquals("Katie", ArrayUtil.getItemByKeys(ta ,{sex: "Female", hair: "Brown"}).name);
  123. var tb:Array<Dynamic> = ArrayUtil.getItemsByKeys(ta,{sex: "Female", hair: "Blonde"});
  124. this.assertEquals(2,tb.length);
  125. this.assertEquals("Aaron",ArrayUtil.getItemByAnyKey(ta, {sex: "Female", hair: "Brown"}).name);
  126. tb = ArrayUtil.getItemsByAnyKey(ta, {sex: "Female", hair: "Brown"});
  127. this.assertEquals(4,tb.length);
  128. var tc = ArrayUtil.getValuesByKey(ta, "sex");
  129. this.assertTrue(ArrayUtil.containsAll(["Male","Female"],cast tc));
  130. //////////Test sortOn...//////////
  131. var ary:Array<{a:String}> = [{a:"A"},{a:"a"},{a:"z"},{a:"Z"}];
  132. var str0:String;
  133. var str1:String;
  134. str0 = dumpAry([{a:"A"},{a:"Z"},{a:"a"},{a:"z"}]);
  135. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"]));
  136. this.assertEquals(str0,str1);
  137. str0 = dumpAry([{a:"A"},{a:"Z"},{a:"a"},{a:"z"}]);
  138. str1 = dumpAry(ArrayUtil.sortOnLite(ary,["a"]));
  139. this.assertEquals(str0,str1);
  140. str0 = dumpAry([{a:"z"},{a:"a"},{a:"Z"},{a:"A"}]);
  141. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"],ArrayUtil.SORT_DESCENDING));
  142. this.assertEquals(str0,str1);
  143. str0 = dumpAry([{a:"z"},{a:"a"},{a:"Z"},{a:"A"}]);
  144. str1 = dumpAry(ArrayUtil.sortOnLite(ary,["a"],ArrayUtil.SORT_DESCENDING));
  145. this.assertEquals(str0,str1);
  146. str0 = dumpAry([{a:"A"},{a:"a"},{a:"Z"},{a:"z"}]);
  147. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"],ArrayUtil.SORT_CASEINSENSITIVE));
  148. this.assertEquals(str0.toLowerCase(),str1.toLowerCase());
  149. str0 = dumpAry([{a:"A"},{a:"a"},{a:"Z"},{a:"z"}]);
  150. str1 = dumpAry(ArrayUtil.sortOnLite(ary,["a"],ArrayUtil.SORT_CASEINSENSITIVE));
  151. this.assertEquals(str0.toLowerCase(),str1.toLowerCase());
  152. str0 = dumpAry([{a:"Z"},{a:"z"},{a:"A"},{a:"a"}]);
  153. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"],ArrayUtil.SORT_CASEINSENSITIVE|ArrayUtil.SORT_DESCENDING));
  154. this.assertEquals(str0.toLowerCase(),str1.toLowerCase());
  155. str0 = dumpAry([{a:"Z"},{a:"z"},{a:"A"},{a:"a"}]);
  156. str1 = dumpAry(ArrayUtil.sortOnLite(ary,["a"],ArrayUtil.SORT_CASEINSENSITIVE|ArrayUtil.SORT_DESCENDING));
  157. this.assertEquals(str0.toLowerCase(),str1.toLowerCase());
  158. str0 = dumpAry([{a:"A"},{a:"Z"},{a:"a"},{a:"z"}]);
  159. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"],ArrayUtil.SORT_UNIQUESORT));
  160. this.assertEquals(str0,str1);
  161. str0 = dumpAry([{a:"A"},{a:"Z"},{a:"a"},{a:"z"}]);
  162. str1 = dumpAry(ArrayUtil.sortOnLite(ary,["a"],ArrayUtil.SORT_UNIQUESORT));
  163. this.assertEquals(str0,str1);
  164. var p = str1;
  165. str0 = dumpAry([]);
  166. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"],ArrayUtil.SORT_CASEINSENSITIVE|ArrayUtil.SORT_UNIQUESORT));
  167. this.assertEquals(str0,str1);
  168. this.assertEquals(p,dumpAry(ary));
  169. str0 = dumpAry([]);
  170. str1 = dumpAry(ArrayUtil.sortOnLite(ary,["a"],ArrayUtil.SORT_CASEINSENSITIVE|ArrayUtil.SORT_UNIQUESORT));
  171. this.assertEquals(str0,str1);
  172. this.assertEquals(p,dumpAry(ary));
  173. str0 = dumpAry([3,2,1,0]);
  174. str1 = dumpAry(ArrayUtil.sortOn(ary,["a"],ArrayUtil.SORT_DESCENDING|ArrayUtil.SORT_RETURNINDEXEDARRAY));
  175. this.assertEquals(str0,str1);
  176. str0 = dumpAry([3,2,1,0]);
  177. str1 = dumpAry(ArrayUtil.indicesOfSorted(ary,["a"],ArrayUtil.SORT_DESCENDING|ArrayUtil.SORT_RETURNINDEXEDARRAY));
  178. this.assertEquals(str0,str1);
  179. //below modified from AS3's doc
  180. var users:Array<Dynamic> = [];
  181. var rUser:Array<Dynamic>;
  182. users.push(new User("Bob", 3));
  183. users.push(new User("barb", 35));
  184. users.push(new User("abcd", 4));
  185. users.push(new User("catchy", 5));
  186. this.assertEquals("Bob:3,abcd:4,barb:35,catchy:5",dumpUsers(ArrayUtil.sortOn(users,["name"])));
  187. this.assertEquals("Bob:3,abcd:4,barb:35,catchy:5",dumpUsers(ArrayUtil.sortOnLite(users,["name"])));
  188. this.assertEquals("abcd:4,barb:35,Bob:3,catchy:5",dumpUsers(ArrayUtil.sortOn(users,["name"],ArrayUtil.SORT_CASEINSENSITIVE)));
  189. this.assertEquals("abcd:4,barb:35,Bob:3,catchy:5",dumpUsers(ArrayUtil.sortOnLite(users,["name"],ArrayUtil.SORT_CASEINSENSITIVE)));
  190. this.assertEquals("catchy:5,Bob:3,barb:35,abcd:4",dumpUsers(ArrayUtil.sortOn(users,["name"],ArrayUtil.SORT_CASEINSENSITIVE|ArrayUtil.SORT_DESCENDING)));
  191. this.assertEquals("catchy:5,Bob:3,barb:35,abcd:4",dumpUsers(ArrayUtil.sortOnLite(users,["name"],ArrayUtil.SORT_CASEINSENSITIVE|ArrayUtil.SORT_DESCENDING)));
  192. rUser = ArrayUtil.sortOn(users,['age']);
  193. this.assertEquals("Bob:3,barb:35,abcd:4,catchy:5",dumpUsers(rUser));
  194. rUser = ArrayUtil.sortOnLite(users,['age']);
  195. this.assertEquals("Bob:3,barb:35,abcd:4,catchy:5",dumpUsers(rUser));
  196. rUser = ArrayUtil.sortOn(users,['age'],ArrayUtil.SORT_NUMERIC);
  197. this.assertEquals("Bob:3,abcd:4,catchy:5,barb:35",dumpUsers(rUser));
  198. rUser = ArrayUtil.sortOnLite(users,['age'],ArrayUtil.SORT_NUMERIC);
  199. this.assertEquals("Bob:3,abcd:4,catchy:5,barb:35",dumpUsers(rUser));
  200. rUser = ArrayUtil.sortOn(users,['age'],ArrayUtil.SORT_DESCENDING|ArrayUtil.SORT_NUMERIC);
  201. this.assertEquals("barb:35,catchy:5,abcd:4,Bob:3",dumpUsers(rUser));
  202. rUser = ArrayUtil.sortOnLite(users,['age'],ArrayUtil.SORT_DESCENDING|ArrayUtil.SORT_NUMERIC);
  203. this.assertEquals("barb:35,catchy:5,abcd:4,Bob:3",dumpUsers(rUser));
  204. rUser = ArrayUtil.sortOn(users,['age'],ArrayUtil.SORT_NUMERIC);
  205. this.assertEquals("Bob:3,abcd:4,catchy:5,barb:35",dumpUsers(rUser));
  206. rUser = ArrayUtil.sortOnLite(users,['age'],ArrayUtil.SORT_NUMERIC);
  207. this.assertEquals("Bob:3,abcd:4,catchy:5,barb:35",dumpUsers(rUser));
  208. var indices = ArrayUtil.sortOn(users,['age'],ArrayUtil.SORT_NUMERIC | ArrayUtil.SORT_RETURNINDEXEDARRAY);
  209. var index:UInt;
  210. var tempA:Array<String> = new Array<String>();
  211. for(i in indices) {
  212. tempA.push(users[i].name);
  213. }
  214. this.assertEquals("Bob,abcd,catchy,barb",tempA.join(','));
  215. var indices2 = ArrayUtil.indicesOfSorted(users,['age'],ArrayUtil.SORT_NUMERIC | ArrayUtil.SORT_RETURNINDEXEDARRAY);
  216. index = 0;
  217. tempA = new Array<String>();
  218. for(i in indices2) {
  219. tempA.push(users[i].name);
  220. }
  221. this.assertEquals("Bob,abcd,catchy,barb",tempA.join(','));
  222. }
  223. public function testClassUtil():Void {
  224. this.assertTrue(Std.is(ClassUtil.construct(Array),Array));
  225. this.assertTrue(new Rectangle(0,0,100,100).equals(ClassUtil.construct(Rectangle,[0,0,100,100])));
  226. }
  227. public function testColorUtil():Void {
  228. var myRGB = ColorUtil.getARGB(0xCCFF0011);
  229. this.assertEquals(0xCC,myRGB.a);
  230. this.assertEquals(0xFF,myRGB.r);
  231. this.assertEquals(0x00,myRGB.g);
  232. this.assertEquals(0x11,myRGB.b);
  233. var color:UInt = ColorUtil.getColor(myRGB.r,myRGB.g,myRGB.b,myRGB.a);
  234. var expColor:UInt = 0xCCFF0011;
  235. this.assertEquals(expColor,color);
  236. this.assertEquals("CCFF0011",ColorUtil.getHexStringFromARGB(myRGB.a,myRGB.r,myRGB.g,myRGB.b));
  237. this.assertEquals("FF0011",ColorUtil.getHexStringFromRGB(myRGB.r,myRGB.g,myRGB.b));
  238. var ct0 = new flash.geom.ColorTransform(1,1,1,1,0x00,0x66,0x44,0xff);
  239. var ct1 = new flash.geom.ColorTransform(1,1,1,1,0,0,0,0xff);
  240. var ct05 = ColorUtil.interpolateColor(ct0,ct1,new Percent(0.5));
  241. this.assertEquals(1.0*0x00,ct05.redOffset);
  242. this.assertEquals(1.0*0x33,ct05.greenOffset);
  243. this.assertEquals(1.0*0x22,ct05.blueOffset);
  244. this.assertEquals(1.0*0xff,ct05.alphaOffset);
  245. }
  246. public function testConversionUtil():Void {
  247. this.assertTrue(1 == ConversionUtil.bitsToBytes(8));
  248. this.assertTrue(1 == ConversionUtil.bitsToKilobits(1024));
  249. this.assertTrue(1 == ConversionUtil.bitsToKilobytes(1024*8));
  250. this.assertTrue(1 == ConversionUtil.bytesToBits(1/8));
  251. this.assertTrue(1 == ConversionUtil.bytesToKilobytes(1024));
  252. this.assertTrue(1 == ConversionUtil.millisecondsToDays(ConversionUtil.daysToMilliseconds(1)));
  253. this.assertTrue(1 == ConversionUtil.minutesToDays(ConversionUtil.daysToMinutes(1)));
  254. this.assertTrue(1 == ConversionUtil.secondsToDays(ConversionUtil.daysToSeconds(1)));
  255. this.assertTrue(1 == ConversionUtil.radiansToDegrees(ConversionUtil.degreesToRadians(1)));
  256. this.assertTrue(1 == ConversionUtil.daysToHours(ConversionUtil.hoursToDays(1)));
  257. this.assertTrue(1 == ConversionUtil.millisecondsToHours(ConversionUtil.hoursToMilliseconds(1)));
  258. this.assertTrue(1 == ConversionUtil.minutesToHours(ConversionUtil.hoursToMinutes(1)));
  259. this.assertTrue(1 == ConversionUtil.secondsToHours(ConversionUtil.hoursToSeconds(1)));
  260. this.assertTrue(1 == ConversionUtil.bitsToKilobits(ConversionUtil.kilobitsToBits(1)));
  261. this.assertTrue(1024 == ConversionUtil.bytesToBits(ConversionUtil.kilobitsToBytes(1)));
  262. this.assertTrue(1 == ConversionUtil.kilobytesToKilobits(ConversionUtil.kilobitsToKilobytes(1)));
  263. }
  264. public function testDate():Void {
  265. var d0:Date = Date.fromString("1988-06-14 12:34:56");
  266. var d1:Date = Date.fromString("1988-06-14 12:34:56");
  267. this.assertTrue(DateUtil.equals(d0,d1));
  268. this.assertEquals("14",DateUtil.formatDate(d0, "d"));
  269. this.assertEquals("Tue",DateUtil.formatDate(d0, "D"));
  270. this.assertEquals("14",DateUtil.formatDate(d0, "j"));
  271. this.assertEquals("Tuesday",DateUtil.formatDate(d0, "l"));
  272. this.assertEquals("2",DateUtil.formatDate(d0, "N"));
  273. this.assertEquals("th",DateUtil.formatDate(d0, "S"));
  274. this.assertEquals("2",DateUtil.formatDate(d0, "w"));
  275. this.assertTrue(Std.string(DateUtil.getDayOfTheYear(d0))==DateUtil.formatDate(d0, "z"));
  276. this.assertTrue(Std.string(DateUtil.getWeekOfTheYear(d0))==DateUtil.formatDate(d0, "W"));
  277. this.assertEquals("June",DateUtil.formatDate(d0, "F"));
  278. this.assertEquals("06",DateUtil.formatDate(d0, "m"));
  279. this.assertEquals("Jun",DateUtil.formatDate(d0, "M"));
  280. this.assertEquals("6",DateUtil.formatDate(d0, "n"));
  281. this.assertEquals("30",DateUtil.formatDate(d0, "t"));
  282. this.assertEquals("1",DateUtil.formatDate(d0, "L"));
  283. this.assertEquals("1988",DateUtil.formatDate(d0, "o"));
  284. this.assertEquals("1988",DateUtil.formatDate(d0, "Y"));
  285. this.assertEquals("88",DateUtil.formatDate(d0, "y"));
  286. this.assertEquals("pm",DateUtil.formatDate(d0, "a"));
  287. this.assertEquals("PM",DateUtil.formatDate(d0, "A"));
  288. this.assertEquals("12",DateUtil.formatDate(d0, "g"));
  289. this.assertEquals("12",DateUtil.formatDate(d0, "G"));
  290. this.assertEquals("12",DateUtil.formatDate(d0, "h"));
  291. this.assertEquals("12",DateUtil.formatDate(d0, "H"));
  292. this.assertEquals("34",DateUtil.formatDate(d0, "i"));
  293. this.assertEquals("56",DateUtil.formatDate(d0, "s"));
  294. this.assertTrue(Std.string(d0.getTime()/1000)==DateUtil.formatDate(d0, "U"));
  295. this.assertEquals(31,DateUtil.getDaysInMonth(2010,12));
  296. d1 = DateTools.delta(d1,ConversionUtil.daysToMilliseconds(3));
  297. this.assertEquals(3,DateUtil.getCountdownUntil(d0,d1).days);
  298. //just to run it once... because I don't know your computer setting...
  299. DateUtil.getTimezoneOffset();
  300. var offset = DateUtil.getTimezoneOffset(d0);
  301. this.assertEquals(d0.getTime()-offset*60*1000,DateUtil.iso8601ToDate("1988-06-14T12:34:56-00:00").getTime());
  302. this.assertEquals(offset/60,DateUtil.getDifferenceFromUTCInHours(d0));
  303. this.assertEquals(offset*60,DateUtil.getDifferenceFromUTCInSeconds(d0));
  304. this.assertEquals(5,DateUtil.getFormattedDifferenceFromUTC(d0).length);
  305. //just to run it once... because I don't know your computer setting...
  306. DateUtil.isDaylightSavings(d0);
  307. this.assertTrue(DateUtil.getTimezone(d0).length>0);
  308. this.assertTrue(NumberUtil.isBetween(DateUtil.getInternetTime(Date.now()),0,1000));
  309. }
  310. #if !php
  311. public function testDisplayObjectUtil():Void {
  312. var d = new Sprite();
  313. var e = new Sprite();
  314. for (i in 0...10){
  315. d.addChild(new Sprite());
  316. e.addChild(new Sprite());
  317. }
  318. d.addChild(e);
  319. this.assertEquals(11,d.numChildren);
  320. this.assertEquals(10,e.numChildren);
  321. DisplayObjectUtil.removeAllChildren(d);
  322. this.assertEquals(0,d.numChildren);
  323. this.assertEquals(10,e.numChildren);
  324. d.addChild(e);
  325. this.assertEquals(1,d.numChildren);
  326. DisplayObjectUtil.removeAllChildren(d,false,true);
  327. this.assertEquals(0,d.numChildren);
  328. this.assertEquals(0,e.numChildren);
  329. }
  330. #end
  331. #if !php
  332. public function testDrawUtil():Void {
  333. var d = new Sprite();
  334. var e = new Sprite();
  335. flash.Lib.current.addChild(d);
  336. flash.Lib.current.addChild(e);
  337. d.graphics.beginFill(0,1);
  338. e.graphics.beginFill(0,1);
  339. DrawUtil.drawRoundRect(d.graphics,0,0,100,100,5,5);
  340. #if flash
  341. this.assertEquals(100.0,d.width);
  342. #end
  343. DrawUtil.drawWedge(e.graphics,new org.casalib.math.geom.Ellipse(0,0,100,100),0,180);
  344. #if flash
  345. this.assertEquals(50.0,e.width);
  346. this.assertEquals(100.0,e.height);
  347. #end
  348. DrawUtil.drawPath(d.graphics,[new Point(),new Point(34,66),new Point(10,124)]);
  349. DrawUtil.drawShape(e.graphics,[new Point(),new Point(34,66),new Point(10,124)]);
  350. this.assertTrue(true);
  351. flash.Lib.current.removeChild(d);
  352. flash.Lib.current.removeChild(e);
  353. }
  354. #end
  355. #if flash
  356. public function testFlashVarUtil():Void {
  357. StageReference.setStage(flash.Lib.current.stage);
  358. if (LocationUtil.isWeb(flash.Lib.current)){
  359. this.assertTrue(FlashVarUtil.hasKey("flashvar"));
  360. this.assertEquals("something",FlashVarUtil.getValue("flashvar"));
  361. } else {
  362. this.assertFalse(FlashVarUtil.hasKey("flashvar"));
  363. }
  364. }
  365. #end
  366. #if flash
  367. public function testFrameUtil():Void {
  368. //TO-DO Test with a swf that really have some frames...
  369. var mc = new flash.display.MovieClip();
  370. var reach = false;
  371. this.assertTrue(FrameUtil.addFrameScript(mc,1,function(){trace("here");reach = true;}));
  372. FrameUtil.removeFrameScript(mc,1);
  373. this.assertEquals(-1,FrameUtil.getFrameNumberForLabel(mc,"test"));
  374. }
  375. #end
  376. public function testGeomUtil():Void {
  377. var pt = new Point(100,100);
  378. var pt0 = new Point();
  379. this.assertEquals(45.0,GeomUtil.angle(pt0,pt));
  380. this.assertTrue(300 == GeomUtil.getRectanglePerimeter(new Rectangle(0,0,100,50)));
  381. GeomUtil.rotatePoint(pt,pt0,90);
  382. GeomUtil.rotatePoint(pt,pt0,90);
  383. this.assertTrue(-100 == pt.x);
  384. this.assertTrue(-100 == pt.y);
  385. GeomUtil.rotatePoint(pt,pt0,90);
  386. GeomUtil.rotatePoint(pt,pt0,90);
  387. this.assertTrue(100 == pt.x);
  388. this.assertTrue(100 == pt.y);
  389. }
  390. public function testLoadUtil():Void {
  391. this.assertTrue(100 == LoadUtil.calculateBps(100,100,1100));
  392. //calculateMillisecondsUntilBuffered is also tested here...
  393. this.assertTrue(1 == LoadUtil.calculateBufferPercent(100,100,100,1100,1000).decimalPercentage);
  394. this.assertEquals(100/1024,LoadUtil.calculateKBps(100,100,1100));
  395. }
  396. #if !php
  397. public function testLocationUtil():Void {
  398. #if flash
  399. var domain = LocationUtil.getDomain(flash.Lib.current);
  400. LocationUtil.isAirApplication();
  401. this.assertTrue(LocationUtil.isDomain(flash.Lib.current,domain));
  402. LocationUtil.isIde();
  403. LocationUtil.isPlugin();
  404. LocationUtil.isStandAlone();
  405. LocationUtil.isWeb(flash.Lib.current);
  406. #elseif js
  407. var domain = LocationUtil.getDomain(flash.Lib.current);
  408. this.assertFalse(LocationUtil.isAirApplication());
  409. this.assertTrue(LocationUtil.isDomain(flash.Lib.current,domain));
  410. this.assertFalse(LocationUtil.isIde());
  411. this.assertFalse(LocationUtil.isPlugin());
  412. this.assertFalse(LocationUtil.isStandAlone());
  413. this.assertTrue(LocationUtil.isWeb(flash.Lib.current));
  414. #else
  415. var domain = LocationUtil.getDomain(flash.Lib.current);
  416. this.assertFalse(LocationUtil.isAirApplication());
  417. this.assertTrue(LocationUtil.isDomain(flash.Lib.current,domain));
  418. this.assertFalse(LocationUtil.isIde());
  419. this.assertFalse(LocationUtil.isPlugin());
  420. this.assertFalse(LocationUtil.isStandAlone());
  421. this.assertFalse(LocationUtil.isWeb(flash.Lib.current));
  422. #end
  423. }
  424. #end
  425. #if false
  426. public function testNavigateUtil():Void {
  427. #if flash
  428. this.assertEquals(flash.external.ExternalInterface.available,NavigateUtil.openWindow("http://www.google.com/#swf"));
  429. #elseif js
  430. this.assertTrue(NavigateUtil.openWindow("http://www.google.com/#js"));
  431. #elseif neko
  432. this.assertTrue(NavigateUtil.openWindow("http://www.google.com/#neko"));
  433. #elseif cpp
  434. this.assertTrue(NavigateUtil.openWindow("http://www.google.com/#cpp"));
  435. #end
  436. }
  437. #end
  438. public function testNumberUtil():Void {
  439. this.assertEquals("00",NumberUtil.addLeadingZero(0));
  440. this.assertTrue(-1 == NumberUtil.constrain(-1,-1.001,100));
  441. this.assertTrue(0 == NumberUtil.constrain(-1,0,1));
  442. this.assertEquals("10 20 30 40",NumberUtil.createStepsBetween(0,50,4).join(' '));
  443. this.assertEquals("01,234,567",NumberUtil.format(1234567, ",", 8));
  444. this.assertEquals("32nd",32 + NumberUtil.getOrdinalSuffix(32));
  445. this.assertEquals(0.2,NumberUtil.getWeightedAverage(0,1,5));
  446. this.assertEquals(5.0,NumberUtil.interpolate(new Percent(0.5), 0, 10));
  447. this.assertTrue(NumberUtil.isBetween(3, 0, 5));
  448. this.assertFalse(NumberUtil.isBetween(7, 0, 5));
  449. this.assertFalse(NumberUtil.isEqual(3.042, 3, 0));
  450. this.assertTrue(NumberUtil.isEqual(3.042, 3, 0.5));
  451. this.assertFalse(NumberUtil.isEven(7));
  452. this.assertTrue(NumberUtil.isEven(12));
  453. this.assertTrue(NumberUtil.isInteger(13));
  454. this.assertFalse(NumberUtil.isInteger(1.2345));
  455. this.assertTrue(NumberUtil.isOdd(7));
  456. this.assertFalse(NumberUtil.isOdd(12));
  457. this.assertTrue(NumberUtil.isPrime(13));
  458. this.assertFalse(NumberUtil.isPrime(4));
  459. var colors:Array<String> = ["Red", "Green", "Blue"];
  460. this.assertEquals("Blue",colors[NumberUtil.loopIndex(2, colors.length)]);
  461. this.assertEquals("Green",colors[NumberUtil.loopIndex(4, colors.length)]);
  462. this.assertEquals("Red",colors[NumberUtil.loopIndex(-6, colors.length)]);
  463. this.assertEquals(75.0,NumberUtil.map(0.75, 0, 1, 0, 100));
  464. this.assertTrue(-5 == NumberUtil.max(-5, null));
  465. this.assertTrue(-5 == NumberUtil.max(-5, "CASA"));
  466. this.assertTrue(-5 == NumberUtil.max(-5, -13));
  467. this.assertTrue(5 == NumberUtil.min(5, null));
  468. this.assertTrue(5 == NumberUtil.min(5, "CASA"));
  469. this.assertTrue(5 == NumberUtil.min(5, 13));
  470. this.assertTrue(0.25 == NumberUtil.normalize(8, 4, 20).decimalPercentage);
  471. for (i in 0...10) {
  472. var randomI = NumberUtil.randomIntegerWithinRange(0,1);
  473. this.assertTrue(NumberUtil.isInteger(randomI));
  474. this.assertTrue(NumberUtil.isBetween(randomI,0,1));
  475. }
  476. for (i in 0...10) {
  477. var random = NumberUtil.randomWithinRange(0,1);
  478. this.assertTrue(NumberUtil.isBetween(random,0,1));
  479. }
  480. this.assertEquals(3.14,NumberUtil.roundDecimalToPlace(3.14159, 2));
  481. this.assertEquals(3.142,NumberUtil.roundDecimalToPlace(3.14159, 3));
  482. }
  483. public function testObjectUtil():Void {
  484. var rect = {
  485. x: 1.0,
  486. y: 2.0,
  487. width: 3.0,
  488. height: 4.0
  489. };
  490. var cloned = ObjectUtil.clone(rect);
  491. this.assertEquals(rect.x, cloned.x);
  492. this.assertEquals(rect.y, cloned.y);
  493. this.assertEquals(rect.width, cloned.width);
  494. this.assertEquals(rect.height, cloned.height);
  495. this.assertTrue(ObjectUtil.contains(rect,3.0));
  496. this.assertTrue(ObjectUtil.getKeys(rect).length >= 4);
  497. this.assertTrue(ObjectUtil.isEmpty([]));
  498. this.assertTrue(ObjectUtil.isEmpty({}));
  499. this.assertFalse(ObjectUtil.isEmpty(rect));
  500. this.assertFalse(ObjectUtil.isNull(12));
  501. this.assertFalse(ObjectUtil.isNull(rect));
  502. var that = this;
  503. var test = function (?n:Dynamic):Void{
  504. that.assertTrue(ObjectUtil.isNull(n));
  505. }
  506. test();
  507. }
  508. public function testPropertySetter():Void {
  509. var rect = new Rectangle(0,0,640,480);
  510. var ps = new PropertySetter(rect,"y");
  511. ps.defineProperty([10.0]);
  512. this.assertEquals(10.0,rect.y);
  513. }
  514. public function testQueryStringUtil():Void {
  515. #if (php||js)
  516. this.assertEquals("q=123",QueryStringUtil.queryString);
  517. this.assertTrue(QueryStringUtil.hasKey("q"));
  518. this.assertEquals("123",QueryStringUtil.getValue("q"));
  519. this.assertFalse(QueryStringUtil.hasKey("w"));
  520. #elseif flash
  521. if (LocationUtil.isWeb(flash.Lib.current)){
  522. this.assertEquals("q=123",QueryStringUtil.queryString);
  523. this.assertTrue(QueryStringUtil.hasKey("q"));
  524. this.assertEquals("123",QueryStringUtil.getValue("q"));
  525. this.assertFalse(QueryStringUtil.hasKey("w"));
  526. }else {
  527. this.assertEquals(null,QueryStringUtil.queryString);
  528. }
  529. #elseif (neko||cpp)
  530. this.assertEquals(null,QueryStringUtil.queryString);
  531. #end
  532. }
  533. public function testRatioUtil():Void {
  534. var rect = new Rectangle(0,0,640,480);
  535. this.assertEquals(3/4,RatioUtil.heightToWidth(rect));
  536. var rect2 = new Rectangle(0,0,640*0.33,480*0.33);
  537. this.assertTrue(RatioUtil.scale(rect,new Percent(0.33),false).equals(rect2));
  538. this.assertTrue(NumberUtil.isInteger(RatioUtil.scale(rect,new Percent(0.33),true).width));
  539. this.assertTrue(RatioUtil.scaleHeight(rect,rect2.width,false).equals(rect2));
  540. this.assertTrue(NumberUtil.isInteger(RatioUtil.scaleHeight(rect,rect2.width,true).width));
  541. this.assertTrue(RatioUtil.scaleToFill(rect2,rect,false).containsRect(rect2));
  542. this.assertTrue(RatioUtil.scaleToFit(rect2,rect,false).containsRect(rect2));
  543. this.assertTrue(RatioUtil.scaleWidth(rect,rect2.height,true).equals(RatioUtil.scale(rect2,new Percent(1))));
  544. this.assertEquals(4/3,RatioUtil.widthToHeight(rect));
  545. }
  546. public function testSingletonUtil():Void {
  547. this.assertEquals(SingletonUtil.singleton(haxe.Serializer),SingletonUtil.singleton(haxe.Serializer));
  548. }
  549. #if !php
  550. public function testStageReference():Void {
  551. var stage = flash.Lib.current.stage;
  552. StageReference.setStage(stage);
  553. this.assertEquals(stage,StageReference.getStage());
  554. this.assertEquals(StageReference.STAGE_DEFAULT,StageReference.getIds()[0]);
  555. this.assertEquals(StageReference.STAGE_DEFAULT,StageReference.getStageId(stage));
  556. StageReference.setStage(stage,"test");
  557. this.assertEquals(2,StageReference.getIds().length);
  558. StageReference.removeStage("test");
  559. this.assertEquals(1,StageReference.getIds().length);
  560. this.assertEquals(StageReference.STAGE_DEFAULT,StageReference.getStageId(stage));
  561. }
  562. #end
  563. public function testStringUtil():Void {
  564. this.assertEquals("123456", StringUtil.addAt("12456",2,"3"));
  565. this.assertEquals(2, StringUtil.contains("12345234","234"));
  566. this.assertEquals(0, StringUtil.contains("12345234","235"));
  567. this.assertEquals(0, StringUtil.contains("-14","."));
  568. this.assertEquals("ab",StringUtil.getLettersFromString("a123b456 .?<>"));
  569. this.assertEquals("123456",StringUtil.getNumbersFromString("a123b456"));
  570. this.assertEquals("12345",StringUtil.getUniqueCharacters("12345234"));
  571. this.assertEquals(6,StringUtil.getWordCount("String in which to count words."));
  572. this.assertEquals("adf234qi^%&amp;%^@51trega&lt;&gt;",StringUtil.htmlEncode("adf234qi^%&%^@51trega<>"));
  573. this.assertEquals("adf234qi^%&%^@51trega<>",StringUtil.htmlDecode("adf234qi^%&amp;%^@51trega&lt;&gt;"));
  574. this.assertEquals(4,StringUtil.indexOfLowerCase("0123aAdf"));
  575. this.assertEquals(5,StringUtil.indexOfUpperCase("0123aAdf"));
  576. this.assertTrue(StringUtil.isLowerCase("asdg"));
  577. this.assertFalse(StringUtil.isLowerCase("asdGads"));
  578. this.assertFalse(StringUtil.isNumber("asd"));
  579. this.assertFalse(StringUtil.isNumber("f"));
  580. this.assertFalse(StringUtil.isNumber("0f0"));
  581. this.assertFalse(StringUtil.isNumber("0-0"));
  582. this.assertFalse(StringUtil.isNumber("0..0"));
  583. this.assertFalse(StringUtil.isNumber("."));
  584. this.assertFalse(StringUtil.isNumber("-."));
  585. this.assertTrue(StringUtil.isNumber("12.14"));
  586. this.assertTrue(StringUtil.isNumber("000"));
  587. this.assertTrue(StringUtil.isNumber("-1"));
  588. this.assertTrue(StringUtil.isNumber("-0"));
  589. this.assertTrue(StringUtil.isNumber("-00.000"));
  590. /*
  591. this.assertTrue(StringUtil.isNumber("-0xffffff"));
  592. this.assertTrue(StringUtil.isNumber("-0x0000"));
  593. this.assertTrue(StringUtil.isNumber("-0x0.000"));
  594. this.assertTrue(StringUtil.isNumber("-0xff.00"));
  595. this.assertTrue(StringUtil.isNumber("0.00f"));
  596. this.assertTrue(StringUtil.isNumber("0F"));
  597. this.assertTrue(StringUtil.isNumber("-0x0.0"));
  598. this.assertTrue(StringUtil.isNumber(".00"));
  599. this.assertTrue(StringUtil.isNumber("0X0f"));
  600. */
  601. this.assertTrue(StringUtil.isPunctuation("!@#$%^&*()_+-=[]{}\\|;:'\",.<>/?"));
  602. this.assertFalse(StringUtil.isPunctuation("asdf!@34s<>"));
  603. this.assertTrue(StringUtil.isUpperCase("ASDF"));
  604. this.assertFalse(StringUtil.isUpperCase("ASsDF"));
  605. this.assertEquals("sno134531lns",StringUtil.remove("asno134531lnsa","a"));
  606. this.assertEquals("asno14531lnsa",StringUtil.removeAt("asno134531lnsa",5));
  607. this.assertEquals("1 2 3 4",StringUtil.removeExtraSpaces("1 2 3 4"));
  608. this.assertEquals("1234",StringUtil.removeWhitespace("1 2 3 4"));
  609. this.assertEquals("1234",StringUtil.replace("124564","456","3"));
  610. this.assertEquals("1234",StringUtil.replaceAt("1254",2,"3"));
  611. this.assertEquals("I Am Title", StringUtil.toTitleCase("i am title"));
  612. this.assertEquals("I Am Title O—K", StringUtil.toTitleCase("i am title o—k"));
  613. this.assertEquals("hel lo",StringUtil.trim(" hel lo "));
  614. this.assertEquals("hel lo ",StringUtil.trimLeft(" hel lo "));
  615. this.assertEquals(" hel lo",StringUtil.trimRight(" hel lo "));
  616. this.assertEquals('Kevin <b>van</b> <i>Zonneveld</i>', StringUtil.stripTags('<p>Kevin</p> <b>van</b> <i>Zonneveld</i>', '<i><b>'));
  617. this.assertEquals('<p>Kevin van Zonneveld</p>', StringUtil.stripTags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>'));
  618. }
  619. #if !(php || cpp || neko)
  620. public function testTextFieldUtil():Void {
  621. var title:flash.text.TextField = new flash.text.TextField();
  622. title.autoSize = flash.text.TextFieldAutoSize.LEFT;
  623. #if flash
  624. var smallCapsStyle:flash.text.StyleSheet = new flash.text.StyleSheet();
  625. smallCapsStyle.parseCSS("p {font-size:15px;} .smallCaps {font-size:30px;}");
  626. title.styleSheet = smallCapsStyle;
  627. #end
  628. title.htmlText = "<p>This Text is Small Caps.</p>";
  629. TextFieldUtil.classSmallCaps(title, "smallCaps");
  630. this.assertEquals(('<P><SPAN CLASS="SMALLCAPS">T</SPAN>HIS <SPAN CLASS="SMALLCAPS">T</SPAN>EXT IS <SPAN CLASS="SMALLCAPS">S</SPAN>MALL <SPAN CLASS="SMALLCAPS">C</SPAN>APS.</P>').toLowerCase(),title.htmlText.toLowerCase());
  631. #if flash
  632. var small:flash.text.TextFormat = new flash.text.TextFormat();
  633. var large:flash.text.TextFormat = new flash.text.TextFormat();
  634. small.size = 15;
  635. large.size = 30;
  636. title = new flash.text.TextField();
  637. title.autoSize = flash.text.TextFieldAutoSize.LEFT;
  638. title.text = "This Text is Small Caps.";
  639. title.setTextFormat(small);
  640. TextFieldUtil.formatSmallCaps(title, large);
  641. title.width = 10;
  642. this.assertTrue(TextFieldUtil.hasOverFlow(title));
  643. TextFieldUtil.removeOverFlow(title);
  644. this.assertFalse(TextFieldUtil.hasOverFlow(title));
  645. #end
  646. }
  647. #end
  648. #if flash
  649. public function testUrlVariablesUtil():Void {
  650. var vars = new flash.net.URLVariables();
  651. vars.abc = 123;
  652. vars.def = 456;
  653. vars.acc = 789;
  654. this.assertEquals("abc=123&acc=789&def=456",UrlVariablesUtil.alphabetize(vars));
  655. }
  656. #end
  657. public function testValidationUtil():Void {
  658. //card numbers from https://www.paypal.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
  659. this.assertEquals("visa",ValidationUtil.getCreditCardProvider("4111111111111111"));
  660. this.assertEquals("mastercard",ValidationUtil.getCreditCardProvider("5555555555554444"));
  661. this.assertEquals("discover",ValidationUtil.getCreditCardProvider("6011000990139424"));
  662. this.assertEquals("amex",ValidationUtil.getCreditCardProvider("378282246310005"));
  663. this.assertEquals("diners",ValidationUtil.getCreditCardProvider("30569309025904"));
  664. this.assertEquals("other",ValidationUtil.getCreditCardProvider("3566002020360505"));
  665. this.assertEquals("invalid",ValidationUtil.getCreditCardProvider("123"));
  666. this.assertTrue(ValidationUtil.isCreditCard("378282246310005"));
  667. this.assertFalse(ValidationUtil.isCreditCard("345"));
  668. this.assertTrue(ValidationUtil.isEmail("andy@onthewings.net"));
  669. this.assertFalse(ValidationUtil.isEmail("12@3234"));
  670. this.assertTrue(ValidationUtil.isUsaStateAbbreviation("dC"));
  671. this.assertFalse(ValidationUtil.isUsaStateAbbreviation("hk"));
  672. }
  673. }