Permalink
Browse files

Rewrote as3 commons collections matchers as hamcrest matchers. Added …

…IntelliJ project
  • Loading branch information...
1 parent d43a0a5 commit 1bcd7f217f9f5662b4d828b0b5f0337d64397b74 @arne-schroppe arne-schroppe committed Dec 31, 2011
View
@@ -1,3 +1,11 @@
-.idea
+*.cache
+.DS_Store
+
+.idea/workspace.xml
+.idea/misc.xml
+.idea/dictionaries
+.idea/copyright
+.idea/scopes
+
bin
-*.iml
+
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectCodeStyleSettingsManager">
+ <option name="PER_PROJECT_SETTINGS">
+ <value />
+ </option>
+ </component>
+</project>
+
@@ -1,60 +0,0 @@
-package net.wooga.utils.flexunit.assertcollection {
- import net.wooga.utils.flexunit.assertcollection.itemasserts.IItemAssert;
-
- import org.as3commons.collections.framework.IIterable;
- import org.as3commons.collections.framework.IIterator;
- import org.flexunit.asserts.fail;
-
- public class AssertCollection {
-
- public static function any(objects:IIterable, itemAssert:IItemAssert, failMessage:String = null):void {
- moreThan(objects, 0, itemAssert, failMessage);
- }
-
-
- public static function none(objects:IIterable, itemAssert:IItemAssert, failMessage:String = null):void {
- exactCount(objects, 0, itemAssert, failMessage);
- }
-
-
- public static function exactCount(objects:IIterable, expectedAmount:int, itemAssert:IItemAssert, failMessage:String = null):void {
-
- var counter:int = getCount(objects, itemAssert);
- if(counter != expectedAmount) {
- fail(failMessage);
- }
- }
-
-
- public static function moreThan(objects:IIterable, minimumAmount:int, itemAssert:IItemAssert, failMessage:String = null):void {
-
- var counter:int = getCount(objects, itemAssert);
- if(counter <= minimumAmount) {
- fail(failMessage);
- }
- }
-
-
- public static function lessThan(objects:IIterable, minimumAmount:int, itemAssert:IItemAssert, failMessage:String = null):void {
-
- var counter:int = getCount(objects, itemAssert);
- if(counter >= minimumAmount) {
- fail(failMessage);
- }
- }
-
-
- private static function getCount(objects:IIterable, itemAssert:IItemAssert):int {
- var counter:int = 0;
- var iterator:IIterator = objects.iterator();
-
- while (iterator.hasNext()) {
- var object:Object = iterator.next();
- if (itemAssert.isMatch(object)) {
- ++counter;
- }
- }
- return counter;
- }
- }
-}
@@ -1,5 +0,0 @@
-package net.wooga.utils.flexunit.assertcollection.itemasserts {
- public interface IItemAssert {
- function isMatch(subject:Object):Boolean;
- }
-}
@@ -1,16 +0,0 @@
-package net.wooga.utils.flexunit.assertcollection.itemasserts {
- import flash.utils.getQualifiedClassName;
-
- public class IsObjectOfClass implements IItemAssert {
-
- private var _type:Class;
-
- public function IsObjectOfClass(type:Class) {
- _type = type;
- }
-
- public function isMatch(subject:Object):Boolean {
- return getQualifiedClassName(subject) == getQualifiedClassName(_type);
- }
- }
-}
@@ -1,17 +0,0 @@
-package net.wooga.utils.flexunit.assertcollection.itemasserts {
- import flash.display.DisplayObject;
-
- public class IsObjectOfClassWithName extends IsObjectOfClass {
- private var _name:String;
-
- public function IsObjectOfClassWithName(type:Class, name:String) {
- super(type);
- _name = name;
- }
-
-
- override public function isMatch(subject:Object):Boolean {
- return super.isMatch(subject) && (subject as DisplayObject).name == _name;
- }
- }
-}
@@ -0,0 +1,54 @@
+package net.wooga.utils.flexunit.hamcrestcollection
+{
+
+import org.as3commons.collections.framework.IIterable;
+import org.as3commons.collections.framework.IIterator;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+
+public class EveryInCollectionMatcher extends TypeSafeDiagnosingMatcher
+{
+ private var _matcher:Matcher;
+
+
+ public function EveryInCollectionMatcher(matcher:Matcher)
+ {
+ super(Object);
+ _matcher = matcher;
+ }
+
+
+ override public function matchesSafely(collection:Object, description:Description):Boolean
+ {
+
+ var iterable:IIterable = collection as IIterable;
+ if(!iterable) {
+ description.appendText("Object is not IIterable");
+ return false;
+ }
+
+ var iterator:IIterator = iterable.iterator();
+ while(iterator.hasNext()) {
+ var item:* = iterator.next();
+ if (!_matcher.matches(item))
+ {
+ description
+ .appendText("an item ")
+ .appendMismatchOf(_matcher, item);
+
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+
+ override public function describeTo(description:Description):void
+ {
+ description.appendText("every item in collection is ").appendDescriptionOf(_matcher);
+ }
+}
+}
@@ -0,0 +1,54 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+ import org.as3commons.collections.framework.IIterable;
+ import org.as3commons.collections.framework.IIterator;
+ import org.hamcrest.Description;
+ import org.hamcrest.Matcher;
+ import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+ public class ExactCountInCollectionMatcher extends TypeSafeDiagnosingMatcher{
+ private var _matcher:Matcher;
+ private var _expectedCount:int;
+
+ public function ExactCountInCollectionMatcher(expectedCount:int, matcher:Matcher) {
+ super(Object);
+ _matcher = matcher;
+ _expectedCount = expectedCount;
+
+
+ }
+
+
+ override public function matchesSafely(collection:Object, description:Description):Boolean {
+
+ var iterable:IIterable = collection as IIterable;
+ if(!iterable) {
+ description.appendText("Object is not IIterable");
+ return false;
+ }
+
+ var actualMatchCount:int = 0;
+ var iterator:IIterator = iterable.iterator();
+ while(iterator.hasNext()) {
+ var item:* = iterator.next();
+ if (_matcher.matches(item))
+ {
+ ++actualMatchCount;
+
+ }
+ }
+
+ if(actualMatchCount !== _expectedCount) {
+ description.appendText(actualMatchCount + " items matched ");
+ return false;
+ }
+
+ return true;
+ }
+
+
+ override public function describeTo(description:Description):void {
+ description.appendText("exactly " + _expectedCount + " items in collection matching ").appendDescriptionOf(_matcher);
+ }
+ }
+}
@@ -0,0 +1,47 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+ import org.as3commons.collections.framework.IIterable;
+ import org.as3commons.collections.framework.IIterator;
+ import org.hamcrest.BaseMatcher;
+ import org.hamcrest.Description;
+ import org.hamcrest.Matcher;
+
+ public class IsCollectionContainingMatcher extends BaseMatcher
+ {
+ private var _elementMatcher:Matcher;
+
+
+ public function IsCollectionContainingMatcher(elementMatcher:Matcher)
+ {
+ super();
+ _elementMatcher = elementMatcher;
+ }
+
+
+ override public function matches(collection:Object):Boolean
+ {
+ var iterable:IIterable = collection as IIterable;
+ if(!iterable) {
+ return false;
+ }
+
+
+ var iterator:IIterator = iterable.iterator();
+ while(iterator.hasNext()) {
+ var item:* = iterator.next();
+ if (_elementMatcher.matches(item))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+
+ override public function describeTo(description:Description):void
+ {
+ description.appendText("a collection containing ").appendDescriptionOf(_elementMatcher);
+ }
+ }
+}
@@ -0,0 +1,9 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+ import org.hamcrest.Matcher;
+
+ public function containsExactly(count:int, itemMatcher:Matcher):Matcher
+ {
+ return new ExactCountInCollectionMatcher(count, itemMatcher);
+ }
+}
@@ -0,0 +1,9 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+import org.hamcrest.Matcher;
+
+ public function everyItemInCollection(itemMatcher:Matcher):Matcher
+ {
+ return new EveryInCollectionMatcher(itemMatcher);
+ }
+}
@@ -0,0 +1,18 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+ import org.hamcrest.Matcher;
+ import org.hamcrest.collection.hasItem;
+ import org.hamcrest.object.equalTo;
+
+ public function hasItemInCollection(value:Object):Matcher
+ {
+ if (value is Matcher)
+ {
+ return new IsCollectionContainingMatcher(value as Matcher);
+ }
+ else
+ {
+ return hasItemInCollection(equalTo(value));
+ }
+ }
+}
View
@@ -0,0 +1,14 @@
+package {
+
+ import net.wooga.utils.flexunit.hamcrestcollection.EveryInCollectionMatcherTest;
+ import net.wooga.utils.flexunit.hamcrestcollection.ExactCountInCollectionMatcherTest;
+ import net.wooga.utils.flexunit.hamcrestcollection.IsCollectionContainingMatcherTest;
+
+ [Suite]
+ [RunWith("org.flexunit.runners.Suite")]
+ public class TestSuite {
+ public var everyInCollectionMatcherTest:EveryInCollectionMatcherTest;
+ public var isCollectionContainingMatcherTest:IsCollectionContainingMatcherTest;
+ public var exactCountInCollectionMatcherTest:ExactCountInCollectionMatcherTest;
+ }
+}
Oops, something went wrong.

0 comments on commit 1bcd7f2

Please sign in to comment.