Skip to content

Commit

Permalink
Added further tests to cover all Util functions except dump object to…
Browse files Browse the repository at this point in the history
… logger. Also fixed a typo in ProcessManager comments, and in PBUtil comments.

git-svn-id: https://pushbuttonengine.googlecode.com/svn/trunk@1059 6b8e678a-0755-11de-b5bf-434079a3519f
  • Loading branch information
boushley committed Jul 30, 2010
1 parent 8ca3182 commit 0aa72d9
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/com/pblabs/engine/PBUtil.as
Expand Up @@ -187,7 +187,7 @@ package com.pblabs.engine
* easier internal processing and validation.</p>
*
* @param source Object to read fields from.
* @param dest Object to assign fields to.
* @param destination Object to assign fields to.
* @param abortOnMismatch If true, throw an error if a field in source is absent in destination.
*
*/
Expand Down
2 changes: 1 addition & 1 deletion src/com/pblabs/engine/core/ProcessManager.as
Expand Up @@ -63,7 +63,7 @@ package com.pblabs.engine.core
* system can catch up in extraordinary cases. If your game is just
* slow, then you will see that the ProcessManager can never catch up
* and you will constantly get the "too many ticks per frame" warning,
* if you have disableSlowWarning set to true.</p>
* if you have disableSlowWarning set to false.</p>
*/
public static const MAX_TICKS_PER_FRAME:int = 5;

Expand Down
286 changes: 260 additions & 26 deletions test/src/com/pblabs/engine/tests/UtilTests.as
Expand Up @@ -9,8 +9,13 @@
package com.pblabs.engine.tests
{
import com.pblabs.engine.PBUtil;

import org.flexunit.Assert;

import flash.display.DisplayObject;

import flash.display.Shape;
import flash.geom.Matrix;

import org.flexunit.Assert;
import org.hamcrest.object.nullValue;

/**
Expand All @@ -22,32 +27,55 @@ package com.pblabs.engine.tests
[Test]
public function testRadiansToDegrees():void
{
Assert.assertEquals(-180, PBUtil.getDegreesFromRadians(Math.PI * -1));
Assert.assertEquals(360, PBUtil.getDegreesFromRadians(Math.PI * 2));
Assert.assertEquals(90, PBUtil.getDegreesFromRadians(Math.PI / 2));
Assert.assertEquals(180, PBUtil.getDegreesFromRadians(Math.PI));
Assert.assertEquals(0, PBUtil.getDegreesFromRadians(0));
var values:Array = [
{'expect': -180, 'input': Math.PI * -1},
{'expect': 360, 'input': Math.PI * 2},
{'expect': 90, 'input': Math.PI / 2},
{'expect': 180, 'input': Math.PI},
{'expect': 0, 'input': 0}
];

for ( var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.getDegreesFromRadians(values[i].input));
}

[Test]
public function testDegreesToRadians():void
{
Assert.assertEquals(Math.PI * -1, PBUtil.getRadiansFromDegrees(-180));
Assert.assertEquals(Math.PI * 2, PBUtil.getRadiansFromDegrees(360));
Assert.assertEquals(Math.PI / 2, PBUtil.getRadiansFromDegrees(90));
Assert.assertEquals(Math.PI, PBUtil.getRadiansFromDegrees(180));
Assert.assertEquals(0, PBUtil.getRadiansFromDegrees(0));
var values:Array = [
{'expect': Math.PI * -1, 'input': -180},
{'expect': Math.PI * 2, 'input': 360},
{'expect': Math.PI / 2, 'input': 90},
{'expect': Math.PI, 'input': 180},
{'expect': 0, 'input': 0}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.getRadiansFromDegrees(values[i].input));
}

[Test]
public function testClamp():void
{
Assert.assertEquals(0, PBUtil.clamp(0, 0, 0));
Assert.assertEquals(.25, PBUtil.clamp(-1, .25, 2));
Assert.assertEquals(1, PBUtil.clamp(8));
Assert.assertEquals(1, PBUtil.clamp(1.25));
Assert.assertEquals(6.25, PBUtil.clamp(8, 0, 6.25));
Assert.assertEquals(5.1, PBUtil.clamp(5.1, 0.5, 10.5));
var values:Array = [
{'expect': 0, 'val': 0, 'min':0 , 'max':0},
{'expect': .25, 'val': -1, 'min':.25 , 'max':2},
{'expect': 6.25, 'val': 8, 'min':0 , 'max':6.25},
{'expect': 5.1, 'val': 5.1, 'min':0.5 , 'max':10.5}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.clamp(values[i].val, values[i].min, values[i].max));

values = [
{'expect': 0, 'input': 0},
{'expect': 1, 'input': 8},
{'expect': .5, 'input': .5},
{'expect': 0, 'input': -.5}
];

for (i = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.clamp(values[i].input));
}

[Test]
Expand All @@ -73,14 +101,19 @@ package com.pblabs.engine.tests

[Test]
public function testUnwrapRadian():void
{
Assert.assertEquals(0, PBUtil.unwrapRadian(0));
Assert.assertEquals(Math.PI, PBUtil.unwrapRadian(Math.PI));
Assert.assertEquals(-Math.PI, PBUtil.unwrapRadian(-Math.PI));
Assert.assertEquals(Math.PI/2, PBUtil.unwrapRadian(Math.PI/2));
Assert.assertEquals(0, PBUtil.unwrapRadian(Math.PI * 2));
Assert.assertEquals(0, PBUtil.unwrapRadian(Math.PI * -2));
Assert.assertEquals(Math.PI, PBUtil.unwrapRadian(Math.PI * 3));
{
var values:Array = [
{'expect': 0, 'input': 0},
{'expect': Math.PI, 'input': Math.PI},
{'expect': -Math.PI, 'input': Math.PI * -1},
{'expect': Math.PI/2, 'input': Math.PI / 2},
{'expect': 0, 'input': Math.PI * 2},
{'expect': 0, 'input': Math.PI * -2},
{'expect': Math.PI, 'input': Math.PI * 3}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.unwrapRadian(values[i].input));
}

[Test]
Expand Down Expand Up @@ -195,6 +228,207 @@ package com.pblabs.engine.tests
Assert.assertEquals(10, dest.integer);
Assert.assertEquals(11, dest.unsigned);
}

[Test]
public function testXYLength():void
{
var values:Array = [
{'expect':13, 'x':5, 'y':12},
{'expect':5, 'x':3, 'y':4}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.xyLength(values[i].x, values[i].y));
}

[Test]
public function testEscapeHTML():void
{
var values:Array = [
{'expect':'Foo &amp; Bar', 'input':'Foo & Bar'},
{'expect':'&lt; &gt; &apos; &quot;', 'input':'< > \' "'},
{'expect':'&amp;amp;', 'input':'&amp;'},
{'expect':'', 'input':''}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.escapeHTMLText(values[i].input));
}

[Test]
public function testStringToBool():void
{
var values:Array = [
{'expect': true, 'input': 'TRUE'},
{'expect': true, 'input': 'true'},
{'expect': true, 'input': 't'},
{'expect': true, 'input': 'T'},
{'expect': true, 'input': '1'},
{'expect': true, 'input': 'tRuE'},
{'expect': false, 'input': 'FALSE'},
{'expect': false, 'input': 'false'},
{'expect': false, 'input': 'F'},
{'expect': false, 'input': 'f'},
{'expect': false, 'input': '0'},
{'expect': false, 'input': 'fAlSe'}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertStrictlyEquals(values[i].expect, PBUtil.stringToBoolean(values[i].input));
}

[Test]
public function testCapitalize():void
{
var values:Array = [
{'expect': 'This', 'input': 'this'},
{'expect': '', 'input': ''},
{'expect': 'This', 'input': 'This'},
{'expect': 'Other', 'input': 'other'},
{'expect': '1234', 'input': '1234'},
{'expect': ' how is this', 'input': ' how is this'}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.capitalize(values[i].input));
}

[Test]
public function testTrim():void
{
var values:Array = [
{'expect': 'foo', 'input':' foo ', 'ch':' '},
{'expect': 'bar', 'input':'fffbarfff', 'ch':'f'},
{'expect': '', 'input':' ', 'ch':' '},
{'expect': '', 'input':'', 'ch':' '},
{'expect': 'foo', 'input':'foo', 'ch':' '},
{'expect': 'foo', 'input':'foo ', 'ch':' '},
{'expect': 'foo', 'input':' foo', 'ch':' '}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.trim(values[i].input, values[i].ch));
}

[Test]
public function testTrimFront():void
{
var values:Array = [
{'expect': 'foo ', 'input':' foo ', 'ch':' '},
{'expect': 'barfff', 'input':'fffbarfff', 'ch':'f'},
{'expect': '', 'input':' ', 'ch':' '},
{'expect': '', 'input':'', 'ch':' '},
{'expect': 'foo', 'input':'foo', 'ch':' '},
{'expect': 'foo ', 'input':'foo ', 'ch':' '},
{'expect': 'foo', 'input':' foo', 'ch':' '}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.trimFront(values[i].input, values[i].ch));
}

[Test]
public function testTrimBack():void
{
var values:Array = [
{'expect': ' foo', 'input':' foo ', 'ch':' '},
{'expect': 'fffbar', 'input':'fffbarfff', 'ch':'f'},
{'expect': '', 'input':' ', 'ch':' '},
{'expect': '', 'input':'', 'ch':' '},
{'expect': 'foo', 'input':'foo', 'ch':' '},
{'expect': 'foo', 'input':'foo ', 'ch':' '},
{'expect': ' foo', 'input':' foo', 'ch':' '}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.trimBack(values[i].input, values[i].ch));
}

[Test]
public function testStringToCharacter():void
{
var values:Array = [
{'expect': 'f', 'input':'foobar'},
{'expect': ' ', 'input':' '},
{'expect': '', 'input':''},
{'expect': '1', 'input':'123456'},
{'expect': '@', 'input':'@'},
{'expect': '!', 'input':'!220adsf'}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.stringToCharacter(values[i].input));
}

[Test]
public function testGetFileExtension():void
{
var values:Array = [
{'expect':'exe', 'input':'some.file.exe'},
{'expect':'', 'input':'some_file'},
{'expect':'', 'input':''},
{'expect':'thisIsSomeFile', 'input':'a.thisIsSomeFile'},
{'expect':'htaccess', 'input':'.htaccess'}
];

for (var i:int = 0; i < values.length; i++)
Assert.assertEquals(values[i].expect, PBUtil.getFileExtension(values[i].input));
}

[Test]
public function testFlipDisplayObject():void
{
var a:Shape = new Shape();
var b:Shape = new Shape();
var c:Shape = new Shape();
a.x = b.x = c.x = 10;
a.y = b.y = c.y = 15;

a.graphics.beginFill(0x000000);
a.graphics.lineStyle(0x000000, 0x000000);
a.graphics.drawRect(0, 0, 20, 30);
a.graphics.endFill();
b.graphics.beginFill(0x000000);
b.graphics.lineStyle(0x000000, 0x000000);
b.graphics.drawRect(0, 0, 20, 30);
b.graphics.endFill();
c.graphics.beginFill(0x000000);
c.graphics.lineStyle(0x000000, 0x000000);
c.graphics.drawRect(0, 0, 20, 30);
c.graphics.endFill();

var aM:Matrix = a.transform.matrix;
var bM:Matrix = b.transform.matrix;
var cM:Matrix = c.transform.matrix;

bM.a = 1;
bM.d = 1;
b.transform.matrix = bM;

cM.a = 1.5;
cM.d = .5;
cM.tx = 15;
cM.ty = 25;
c.transform.matrix = cM;

PBUtil.flipDisplayObject(a, PBUtil.FLIP_HORIZONTAL);
PBUtil.flipDisplayObject(b, PBUtil.FLIP_VERTICAL);
PBUtil.flipDisplayObject(c, PBUtil.FLIP_HORIZONTAL);
PBUtil.flipDisplayObject(c, PBUtil.FLIP_VERTICAL);

var aNM:Matrix = a.transform.matrix;
var bNM:Matrix = b.transform.matrix;
var cNM:Matrix = c.transform.matrix;

Assert.assertEquals(-1, aNM.a);
Assert.assertEquals(30, aNM.tx);
Assert.assertEquals(-1, bNM.d);
Assert.assertEquals(45, bNM.ty);
Assert.assertEquals(-1.5, cNM.a);
Assert.assertEquals(45, cNM.tx);
Assert.assertEquals(-.5, cNM.d);
Assert.assertEquals(40, cNM.ty);
}
}
}

Expand Down

0 comments on commit 0aa72d9

Please sign in to comment.