Skip to content

Instantly share code, notes, and snippets.

@destroytoday
Created February 10, 2011 18:18
Show Gist options
  • Save destroytoday/821023 to your computer and use it in GitHub Desktop.
Save destroytoday/821023 to your computer and use it in GitHub Desktop.
Proposal for the use of interfaces to allow mocking of final classes like Stage
package flash.display.DisplayObject
{
public class DisplayObject extends EventDispatcher implements IDisplayObject
{
public function DisplayObject()
{
}
public function get stage():IStage
{
// return Stage by default
}
// ...other interface methods...
}
}
package
{
public class InvalidatingSprite extends Sprite
{
protected var isInvalidating:Boolean;
public function InvalidatingSprite()
{
}
public function invalidateDisplayList()
{
if (stage && !isInvalidating)
{
isInvalidating = true;
addEventListener(Event.RENDER, renderHandler);
addEventListener(Event.ENTERFRAME, enterframeHandler);
stage.invalidate();
}
}
// ...other invalidation code...
}
}
package
{
public class InvalidatingSpriteTest
{
public var sprite:InvalidatingSprite;
public function InvalidatingSpriteTest()
{
}
[Before]
public function setUp():void
{
sprite = new InvalidatingSprite();
}
[After]
public function tearDown():void
{
sprite = null;
}
[Test]
public function sprite_invalidates_stage():void
{
var mockStage:MockStage = new MockStage();
sprite.stage = mockStage;
sprite.invalidateDisplayList();
assertThat(mockStage.hasInvalidated);
}
}
}
package
{
public class MockStage implements IStage
{
public var hasInvalidated:Boolean;
public function MockStage()
{
}
public function invalidate():void
{
hasInvalidated = true;
}
// ...other interface methods...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment