Skip to content
Browse files

Bug fixes to event rule and tests

  • Loading branch information...
1 parent f404617 commit 7ea8ef29fff7784d81f738e2f586657932d256f2 @labriola committed Feb 28, 2011
View
191 FlexUnit4/src/org/flexunit/events/rule/EventExpectations.as
@@ -1,3 +1,30 @@
+/**
+ * Copyright (c) 2011 Digital Primates
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @author Michael Labriola
+ * @version
+ **/
package org.flexunit.events.rule {
import flash.events.Event;
import flash.events.EventDispatcher;
@@ -14,21 +41,31 @@ package org.flexunit.events.rule {
import org.hamcrest.object.IsInstanceOfMatcher;
import org.hamcrest.object.hasProperties;
import org.hamcrest.object.hasPropertyWithValue;
-
+
[Event(name="failEventExpectation", type="org.flexunit.events.rule.ExpectationEvent")]
[Event(name="passEventExpectation", type="org.flexunit.events.rule.ExpectationEvent")]
public class EventExpectations extends EventDispatcher implements IEventExpectation {
-
+ private static const NO_EXPECTED_EVENTS:int = -1;
+ private static const NO_EVENTS:int = -2;
+ private static const ONE_EVENT:int = 1;
+ private static const TWO_EVENTS:int = 2;
+ private static const THREE_EVENTS:int = 3;
+
private var target:IEventDispatcher;
private var rule:IMethodRule;
private var quantityMatcher:QuantityMatcher;
private var multiMatcher:MultiMatcher;
private var actualEvents:Array;
-
+
private var typeName:String;
private var timeoutMonitor:TimeoutMonitor;
-
+
+ // We need to check how many events happen, even after we meet pass conditions.
+ private var multipleEventsRequired:int = NO_EXPECTED_EVENTS;
+ // Once we pass we don't care how many events happen.
+ private var multipleEventsLazy:Boolean = false;
+
private var methodToCallStack:Array;
private var _complete:Boolean = false;
@@ -38,77 +75,113 @@ package org.flexunit.events.rule {
}
/**
- mock(core).ordered();
-**/
+ mock(core).ordered();
+ **/
public function get pendingAsync():Boolean {
if (!timeoutMonitor) {
return false;
}
return timeoutMonitor.pending;
}
-
+
+ private function get waitingForEvents():Boolean {
+ // We don't have to wait for anything.
+ if ( !multipleEventsLazy && ( multipleEventsRequired == NO_EXPECTED_EVENTS ) ) {
+ return false;
+ }
+
+ // Figure out whether or not we've passed at this moment.
+ var passed:Boolean = quantityMatcher.matches( actualEvents.length );
+
+ // Once a lazy async has passed we don't care what happens afterwards.
+ if ( multipleEventsLazy && passed ) {
+ return false;
+ }
+
+ // Check to see if we have recieved enough events to test for failure.
+ var haveEnoughEvents:Boolean = ( actualEvents.length >= multipleEventsRequired );
+
+ // If we were required to wait, but already failed, we don't need to wait anymore.
+ if ( multipleEventsRequired != -1 && haveEnoughEvents && !passed ) {
+ return false;
+ }
+
+ return true;
+ }
+
public function startMonitor():void {
if (timeoutMonitor) {
timeoutMonitor.start();
}
}
-
+
public function never():IEventExpectation {
quantityMatcher.never();
+ multipleEventsRequired = NO_EVENTS;
return this;
}
-
+
public function once():IEventExpectation {
quantityMatcher.once();
+ multipleEventsRequired = ONE_EVENT;
return this;
}
-
+
public function twice():IEventExpectation {
quantityMatcher.twice();
+ multipleEventsRequired = TWO_EVENTS;
return this;
}
-
+
public function thrice():IEventExpectation {
quantityMatcher.thrice();
+ multipleEventsRequired = THREE_EVENTS;
return this;
}
-
+
public function times( value:Number ):IEventExpectation {
quantityMatcher.times( value );
+ multipleEventsRequired = value;
return this;
}
-
+
public function atLeast( value:Number ):IEventExpectation {
quantityMatcher.atLeast( value );
+ multipleEventsLazy = true;
return this;
}
-
+
public function atMost( value:Number ):IEventExpectation {
quantityMatcher.atMost( value );
+ multipleEventsRequired = value;
return this;
}
-
+
public function greaterThan( value:Number ):IEventExpectation {
quantityMatcher.greaterThan( value );
+ multipleEventsLazy = true;
return this;
}
-
+
public function greaterThanOrEqualTo( value:Number ):IEventExpectation {
quantityMatcher.greaterThanOrEqualTo( value );
+ multipleEventsLazy = true;
return this;
}
-
+
public function lessThan( value:Number ):IEventExpectation {
quantityMatcher.lessThan( value );
+ multipleEventsRequired = value;
return this;
}
-
+
public function lessThanOrEqualTo( value:Number ):IEventExpectation {
quantityMatcher.lessThanOrEqualTo( value );
+ multipleEventsRequired = value;
return this;
}
-
+
public function instanceOf( eventType:Class ):IEventExpectation {
multiMatcher.addChildMatcher( new IsInstanceOfMatcher( eventType ) );
return this;
@@ -126,25 +199,25 @@ package org.flexunit.events.rule {
}
methodToCallStack.push( method );
-
+
return this;
}
-
+
public function withTimeout( value:Number ):IEventExpectation {
-
+
if ( value > 0 ) {
//Just in case someone tries to set multiple timeouts
if ( timeoutMonitor ) {
timeoutMonitor.removeEventListener( TimeoutMonitor.TIME_OUT_EXPIRED, handleTimeout );
}
-
+
timeoutMonitor = new TimeoutMonitor( value );
timeoutMonitor.addEventListener( TimeoutMonitor.TIME_OUT_EXPIRED, handleTimeout );
}
-
+
return this;
}
-
+
private function handleTimeout( event:Event ):void {
//notifyFailure( "Timeout occurred before event" );
if ( !quantityMatcher.matches( actualEvents.length ) ) {
@@ -159,24 +232,24 @@ package org.flexunit.events.rule {
private function notifyFailure( message:String ):void {
clearTimeout();
_complete = true;
-
+
var event:Event = new ExpectationEvent( ExpectationEvent.FAIL_EXPECTATION, message );
dispatchEvent( event );
}
-
+
private function notifyPass():void {
clearTimeout();
_complete = true;
var event:Event = new ExpectationEvent( ExpectationEvent.PASS_EXPECTATION, "" );
dispatchEvent( event );
}
-
+
public function hasPropertyWithValue( propertyName:String, valueOrMatcher:* ):IEventExpectation {
multiMatcher.addChildMatcher( org.hamcrest.object.hasPropertyWithValue( propertyName, valueOrMatcher ) );
return this;
}
-
+
public function hasProperties( value:Object ):IEventExpectation {
multiMatcher.addChildMatcher( org.hamcrest.object.hasProperties( value ) );
return this;
@@ -187,22 +260,33 @@ package org.flexunit.events.rule {
timeoutMonitor.finished();
}
}
-
+
private function handleEvent( event:Event ):void {
+ // Keep the event.
actualEvents.push( event );
-
+
+ // Call any methods for this event.
+ if ( methodToCallStack ) {
+ for ( var i:int=0; i<methodToCallStack.length; i++ ) {
+ methodToCallStack[ i ]( event );
+ }
+ }
+
+ if ( pendingAsync ) {
+ return;
+ }
+
+ if ( waitingForEvents ) {
+ return;
+ }
+
//Think this is wrong... probably need to know we got the right amount
//first???
clearTimeout();
-
+
if ( !multiMatcher.matches( actualEvents ) ) {
notifyFailure( describeMismatch( actualEvents, multiMatcher ) );
} else {
- if ( methodToCallStack ) {
- for ( var i:int=0; i<methodToCallStack.length; i++ ) {
- methodToCallStack[ i ]( event );
- }
- }
performAggressiveVerification();
}
@@ -212,24 +296,33 @@ package org.flexunit.events.rule {
if ( timeoutMonitor ) {
timeoutMonitor.finished();
}
-
+
if ( typeName && ( typeName.length > 0 ) ) {
this.target.removeEventListener( typeName, handleEvent );
}
}
-
+
private function performAggressiveVerification():void {
+ if ( !quantityMatcher.matches( actualEvents.length ) ) {
+ notifyFailure( describeMismatch( actualEvents.length, quantityMatcher ) );
+ }
+ else {
+ notifyPass();
+ }
+
+ /*
quantityMatcher.matches( actualEvents.length );
if ( quantityMatcher.resolved ) {
- if ( quantityMatcher.passed ) {
- notifyPass();
- } else if ( quantityMatcher.failed ) {
- notifyFailure( describeMismatch( actualEvents.length, quantityMatcher ) );
- }
+ if ( quantityMatcher.passed ) {
+ notifyPass();
+ } else if ( quantityMatcher.failed ) {
+ notifyFailure( describeMismatch( actualEvents.length, quantityMatcher ) );
}
+ }
+ */
}
-
+
public function verify():void {
var failureString:String;
@@ -239,20 +332,20 @@ package org.flexunit.events.rule {
if ( timeoutMonitor ) {
timeoutMonitor.finished();
}
-
+
if ( !quantityMatcher.matches( actualEvents.length ) ) {
fail( describeMismatch( actualEvents.length, quantityMatcher ) );
} else if ( !multiMatcher.matches( actualEvents ) ) {
fail( describeMismatch( actualEvents, multiMatcher ) );
}
}
-
+
cleanUp();
}
private function describeMismatch( actual:Object, matcher:Matcher ):String {
var descriptionStr:String = "";
-
+
if (!matcher.matches(actual)) {
var description:Description = new StringDescription();
@@ -267,7 +360,7 @@ package org.flexunit.events.rule {
return descriptionStr;
}
-
+
public function EventExpectations( target:IEventDispatcher, rule:IMethodRule ) {
this.target = target;
this.rule = rule;
View
114 FlexUnit4Test/src/org/flexunit/events/rule/cases/AsyncEventQuantityTesting.as
@@ -14,21 +14,21 @@ package org.flexunit.events.rule.cases {
import org.hamcrest.object.instanceOf;
import org.hamcrest.object.isFalse;
import org.hamcrest.object.isTrue;
-
+
public class AsyncEventQuantityTesting {
[Rule]
public var expectEvent:EventRule = new EventRule();
-
-/* [Test]
+
+ [Test]
public function shouldPassNever():void {
var timer:Timer = new Timer( 5, 1);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
never();
-
+
//timer.start();
}
@@ -37,57 +37,57 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 1);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
never();
timer.start();
}
-*/
+
[Test]
public function shouldPassOnce():void {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
once();
timer.start();
}
- /*
-
+
+
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailOnceUnder():void {
var timer:Timer = new Timer( 5, 1);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
once();
//timer.start();
}
-
+
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailOnceOver():void {
var timer:Timer = new Timer( 5, 2);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
once();
timer.start();
}
-
+
[Test]
public function shouldPassTwice():void {
var timer:Timer = new Timer( 5, 2);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
twice();
@@ -99,33 +99,33 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
twice();
timer.start();
-
+
}
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailTwiceOver():void {
var timer:Timer = new Timer( 5, 3);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
twice();
timer.start();
}
-
+
[Test]
public function shouldPassThrice():void {
var timer:Timer = new Timer( 5, 3);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
thrice();
@@ -137,35 +137,35 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 2);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
thrice();
timer.start();
-
+
}
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailThriceOver():void {
var timer:Timer = new Timer( 5, 4);
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
thrice();
timer.start();
-
+
}
-
+
[Test]
public function shouldPassTimes():void {
var timer:Timer = new Timer( 5, 4 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
times(4);
@@ -177,33 +177,33 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
times(4);
timer.start();
-
+
}
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailTimesOver():void {
var timer:Timer = new Timer( 5, 5 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
times(4);
timer.start();
}
-
+
[Test]
public function shouldPassAtMost():void {
var timer:Timer = new Timer( 5, 2 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
atMost(2);
@@ -215,32 +215,32 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
atMost(2);
timer.start();
}
-
+
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailAtMost():void {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
atMost(2);
timer.start();
}
-
+
[Test]
public function shouldPassLessThan():void {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
lessThan(2);
@@ -252,33 +252,33 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 2 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
lessThan(2);
timer.start();
}
-
+
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailLessThanExceed():void {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
lessThan(2);
timer.start();
}
-
+
[Test]
public function shouldPassLessThanEqualUnder():void {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
lessThanOrEqualTo(2);
@@ -290,7 +290,7 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 2 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
lessThanOrEqualTo(2);
@@ -302,56 +302,56 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
lessThanOrEqualTo(2);
timer.start();
}
-
+
[Test]
public function shouldPassAtLeastEqual():void {
var timer:Timer = new Timer( 5, 2 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
atLeast(2);
timer.start();
}
-
+
[Test]
public function shouldPassAtLeastExceed():void {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
atLeast(2);
timer.start();
}
-
+
[Test(expects="flexunit.framework.AssertionFailedError")]
public function shouldFailLessAtLeast():void {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
atLeast(2);
timer.start();
}
-
+
[Test]
public function shouldPassGreaterThan():void {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
greaterThan(2);
@@ -363,7 +363,7 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 2 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
greaterThan(2);
@@ -375,7 +375,7 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
greaterThan(2);
@@ -387,7 +387,7 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 3 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
greaterThanOrEqualTo(2);
@@ -399,7 +399,7 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 2 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
greaterThanOrEqualTo(2);
@@ -411,11 +411,11 @@ package org.flexunit.events.rule.cases {
var timer:Timer = new Timer( 5, 1 );
expectEvent.from( timer ).
- hasType( TimerEvent.TIMER_COMPLETE ).
+ hasType( TimerEvent.TIMER ).
withTimeout( 100 ).
greaterThanOrEqualTo(2);
timer.start();
}
-*/ }
+ }
}

0 comments on commit 7ea8ef2

Please sign in to comment.
Something went wrong with that request. Please try again.