Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rewrote as3 commons collections matchers as hamcrest matchers. Added …

…IntelliJ project
  • Loading branch information...
commit 1bcd7f217f9f5662b4d828b0b5f0337d64397b74 1 parent d43a0a5
@arne-schroppe arne-schroppe authored
Showing with 350 additions and 100 deletions.
  1. +10 −2 .gitignore
  2. +9 −0 .idea/codeStyleSettings.xml
  3. +0 −60 src/net/wooga/utils/flexunit/assertcollection/AssertCollection.as
  4. +0 −5 src/net/wooga/utils/flexunit/assertcollection/itemasserts/IItemAssert.as
  5. +0 −16 src/net/wooga/utils/flexunit/assertcollection/itemasserts/IsObjectOfClass.as
  6. +0 −17 src/net/wooga/utils/flexunit/assertcollection/itemasserts/IsObjectOfClassWithName.as
  7. +54 −0 src/net/wooga/utils/flexunit/hamcrestcollection/EveryInCollectionMatcher.as
  8. +54 −0 src/net/wooga/utils/flexunit/hamcrestcollection/ExactCountInCollectionMatcher.as
  9. +47 −0 src/net/wooga/utils/flexunit/hamcrestcollection/IsCollectionContainingMatcher.as
  10. +9 −0 src/net/wooga/utils/flexunit/hamcrestcollection/containsExactly.as
  11. +9 −0 src/net/wooga/utils/flexunit/hamcrestcollection/everyItemInCollection.as
  12. +18 −0 src/net/wooga/utils/flexunit/hamcrestcollection/hasItemInCollection.as
  13. +14 −0 test/TestSuite.as
  14. +76 −0 test/net/wooga/utils/flexunit/hamcrestcollection/EveryInCollectionMatcherTest.as
  15. +27 −0 test/net/wooga/utils/flexunit/hamcrestcollection/ExactCountInCollectionMatcherTest.as
  16. +23 −0 test/net/wooga/utils/flexunit/hamcrestcollection/IsCollectionContainingMatcherTest.as
View
12 .gitignore
@@ -1,3 +1,11 @@
-.idea
+*.cache
+.DS_Store
+
+.idea/workspace.xml
+.idea/misc.xml
+.idea/dictionaries
+.idea/copyright
+.idea/scopes
+
bin
-*.iml
+
View
9 .idea/codeStyleSettings.xml
@@ -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>
+
View
60 src/net/wooga/utils/flexunit/assertcollection/AssertCollection.as
@@ -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;
- }
- }
-}
View
5 src/net/wooga/utils/flexunit/assertcollection/itemasserts/IItemAssert.as
@@ -1,5 +0,0 @@
-package net.wooga.utils.flexunit.assertcollection.itemasserts {
- public interface IItemAssert {
- function isMatch(subject:Object):Boolean;
- }
-}
View
16 src/net/wooga/utils/flexunit/assertcollection/itemasserts/IsObjectOfClass.as
@@ -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);
- }
- }
-}
View
17 src/net/wooga/utils/flexunit/assertcollection/itemasserts/IsObjectOfClassWithName.as
@@ -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;
- }
- }
-}
View
54 src/net/wooga/utils/flexunit/hamcrestcollection/EveryInCollectionMatcher.as
@@ -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);
+ }
+}
+}
View
54 src/net/wooga/utils/flexunit/hamcrestcollection/ExactCountInCollectionMatcher.as
@@ -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);
+ }
+ }
+}
View
47 src/net/wooga/utils/flexunit/hamcrestcollection/IsCollectionContainingMatcher.as
@@ -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);
+ }
+ }
+}
View
9 src/net/wooga/utils/flexunit/hamcrestcollection/containsExactly.as
@@ -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);
+ }
+}
View
9 src/net/wooga/utils/flexunit/hamcrestcollection/everyItemInCollection.as
@@ -0,0 +1,9 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+import org.hamcrest.Matcher;
+
+ public function everyItemInCollection(itemMatcher:Matcher):Matcher
+ {
+ return new EveryInCollectionMatcher(itemMatcher);
+ }
+}
View
18 src/net/wooga/utils/flexunit/hamcrestcollection/hasItemInCollection.as
@@ -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
14 test/TestSuite.as
@@ -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;
+ }
+}
View
76 test/net/wooga/utils/flexunit/hamcrestcollection/EveryInCollectionMatcherTest.as
@@ -0,0 +1,76 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+import flash.display.Sprite;
+
+import org.as3commons.collections.ArrayList;
+import org.hamcrest.assertThat;
+import org.hamcrest.object.equalTo;
+
+public class EveryInCollectionMatcherTest {
+
+
+
+
+ [Before]
+ public function setUp():void {
+
+ }
+
+ [Test]
+ public function should_match_every_item_in_collection():void {
+
+ var collection:ArrayList = new ArrayList();
+ collection.add("1");
+ collection.add("2");
+ collection.add("3");
+ collection.add("4");
+
+
+ var testMatcher:TestMatcher = new TestMatcher(collection);
+
+ assertThat(collection, everyItemInCollection( testMatcher ));
+
+ assertThat(testMatcher.haveAllObjectsBeenMatchedExactlyOnce(), equalTo(true));
+ }
+
+
+}
+}
+
+import org.as3commons.collections.ArrayList;
+import org.as3commons.collections.framework.IIterator;
+import org.hamcrest.BaseMatcher;
+
+class TestMatcher extends BaseMatcher {
+
+ private var _collection:ArrayList;
+ private var _matchedObjects:ArrayList;
+
+
+ public function TestMatcher(collection:ArrayList) {
+ _collection = collection;
+ _matchedObjects = new ArrayList();
+ }
+
+ override public function matches(item:Object):Boolean {
+
+ _matchedObjects.add(item);
+ if(_collection.count(item) == 0) {
+ throw new ArgumentError("Matched item that is not in collection");
+ }
+
+ return true;
+ }
+
+ public function haveAllObjectsBeenMatchedExactlyOnce():Boolean {
+ var iterator:IIterator = _collection.iterator();
+ while(iterator.hasNext()) {
+ var item:* = iterator.next();
+ if(_matchedObjects.count(item) != 1) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
View
27 test/net/wooga/utils/flexunit/hamcrestcollection/ExactCountInCollectionMatcherTest.as
@@ -0,0 +1,27 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+ import org.as3commons.collections.ArrayList;
+ import org.hamcrest.assertThat;
+ import org.hamcrest.object.equalTo;
+
+ public class ExactCountInCollectionMatcherTest {
+
+ [Test]
+ public function should_match_exactly_three_items():void {
+
+ var collection:ArrayList = new ArrayList();
+ collection.add("1");
+ collection.add("2");
+ collection.add("TARGET");
+ collection.add("3");
+ collection.add("4");
+ collection.add("TARGET");
+ collection.add("5");
+ collection.add("TARGET");
+
+
+ assertThat(collection, containsExactly(3, equalTo("TARGET")));
+
+ }
+ }
+}
View
23 test/net/wooga/utils/flexunit/hamcrestcollection/IsCollectionContainingMatcherTest.as
@@ -0,0 +1,23 @@
+package net.wooga.utils.flexunit.hamcrestcollection {
+
+ import org.as3commons.collections.ArrayList;
+ import org.hamcrest.assertThat;
+ import org.hamcrest.object.equalTo;
+
+ public class IsCollectionContainingMatcherTest {
+
+ [Test]
+ public function should_match_one_item():void {
+
+ var collection:ArrayList = new ArrayList();
+ collection.add("1");
+ collection.add("2");
+ collection.add("3");
+ collection.add("4");
+ collection.add("5");
+
+
+ assertThat(collection, hasItemInCollection(equalTo("3")));
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.