Permalink
Browse files

add functions to mimic Array indexOf, filter, reject, every, some usi…

…ng Matchers
  • Loading branch information...
1 parent d5b60d5 commit f57ef3bd1202f717ecae10c93ed6bf7a29c54745 @drewbourne committed Jan 5, 2012
View
26 hamcrest-unit-test/src/org/hamcrest/collection/EveryByMatcherTest.as
@@ -0,0 +1,26 @@
+package org.hamcrest.collection
+{
+ import org.flexunit.assertThat;
+ import org.hamcrest.number.isNumber;
+ import org.hamcrest.object.isFalse;
+ import org.hamcrest.object.isTrue;
+
+ public class EveryByMatcherTest
+ {
+ [Test]
+ public function with_all_matching_items_returns_true():void
+ {
+ var items:Array = [1, 2, 3, 4, 5];
+
+ assertThat(everyByMatcher(items, isNumber()), isTrue());
+ }
+
+ [Test]
+ public function with_any_mismatched_item_returns_false():void
+ {
+ var items:Array = [1, 2, false, 4, 5];
+
+ assertThat(everyByMatcher(items, isNumber()), isFalse());
+ }
+ }
+}
View
25 hamcrest-unit-test/src/org/hamcrest/collection/FilterByMatcherTest.as
@@ -0,0 +1,25 @@
+package org.hamcrest.collection
+{
+ import org.flexunit.assertThat;
+ import org.hamcrest.core.nothing;
+ import org.hamcrest.number.greaterThan;
+
+ public class FilterByMatcherTest
+ {
+ [Test]
+ public function returns_matching_items():void
+ {
+ const items:Array = [1, 2, 3, 4, 5];
+
+ assertThat(filterByMatcher(items, greaterThan(2)), array(3, 4, 5));
+ }
+
+ [Test]
+ public function returns_empty_array_for_no_matching_items():void
+ {
+ const items:Array = [1, 2, 3, 4, 5];
+
+ assertThat(filterByMatcher(items, nothing()), emptyArray());
+ }
+ }
+}
View
29 hamcrest-unit-test/src/org/hamcrest/collection/IndexOfByMatcherTest.as
@@ -0,0 +1,29 @@
+package org.hamcrest.collection
+{
+ import org.flexunit.assertThat;
+ import org.hamcrest.object.equalTo;
+
+ public class IndexOfByMatcherTest
+ {
+ [Test]
+ public function returns_index_of_first_matching_item():void
+ {
+ const items:Array = [1, 2, 3, 2];
+ const SEARCH_ITEM:int = 2;
+ const EXPECTED_INDEX:int = 1;
+
+ assertThat("matches 2 at index 1", indexOfByMatcher(items, equalTo(SEARCH_ITEM)), equalTo(EXPECTED_INDEX));
+ }
+
+ [Test]
+ public function returns_index_of_first_matching_item_from_start_index():void
+ {
+ const items:Array = [1, 2, 3, 2];
+ const SEARCH_ITEM:int = 2;
+ const START_INDEX:int = 2;
+ const EXPECTED_INDEX:int = 3;
+
+ assertThat("matches 2 at index 3", indexOfByMatcher(items, equalTo(SEARCH_ITEM), START_INDEX), equalTo(EXPECTED_INDEX));
+ }
+ }
+}
View
16 hamcrest-unit-test/src/org/hamcrest/collection/IndexesOfByMatcherTest.as
@@ -0,0 +1,16 @@
+package org.hamcrest.collection
+{
+ import org.flexunit.assertThat;
+ import org.hamcrest.number.greaterThan;
+
+ public class IndexesOfByMatcherTest
+ {
+ [Test]
+ public function returns_the_index_of_all_matching_items():void
+ {
+ const items:Array = [1, 2, 3, 2, 4];
+
+ assertThat(indexesOfByMatcher(items, greaterThan(2)), array(2, 4));
+ }
+ }
+}
View
26 hamcrest-unit-test/src/org/hamcrest/collection/RejectByMatcherTest.as
@@ -0,0 +1,26 @@
+package org.hamcrest.collection
+{
+ import org.flexunit.assertThat;
+ import org.hamcrest.core.anything;
+ import org.hamcrest.core.nothing;
+ import org.hamcrest.number.greaterThan;
+
+ public class RejectByMatcherTest
+ {
+ [Test]
+ public function returns_mismatched_items():void
+ {
+ const items:Array = [1, 2, 3, 4, 5];
+
+ assertThat(rejectByMatcher(items, greaterThan(2)), array(1, 2));
+ }
+
+ [Test]
+ public function returns_empty_array_if_every_item_matches():void
+ {
+ const items:Array = [1, 2, 3, 4, 5];
+
+ assertThat(rejectByMatcher(items, anything()), emptyArray());
+ }
+ }
+}
View
26 hamcrest-unit-test/src/org/hamcrest/collection/SomeByMatcherTest.as
@@ -0,0 +1,26 @@
+package org.hamcrest.collection
+{
+ import org.flexunit.assertThat;
+ import org.hamcrest.number.isNumber;
+ import org.hamcrest.object.isFalse;
+ import org.hamcrest.object.isTrue;
+
+ public class SomeByMatcherTest
+ {
+ [Test]
+ public function with_any_matching_items_returns_true():void
+ {
+ var items:Array = [false, 1, false, {}];
+
+ assertThat(someByMatcher(items, isNumber()), isTrue());
+ }
+
+ [Test]
+ public function with_no_matching_items_returns_false():void
+ {
+ var items:Array = [false, "1", false, {}];
+
+ assertThat(someByMatcher(items, isNumber()), isFalse());
+ }
+ }
+}
View
38 hamcrest/src-flex/org/hamcrest/collection/everyByMatcher.as
@@ -0,0 +1,38 @@
+package org.hamcrest.collection
+{
+ import org.hamcrest.Matcher;
+
+ /**
+ * Executes a Matcher on each item in the array until an item is reached that returns false for the specified
+ * matcher. You use this method to determine whether all items in an array meet a criterion, such as having
+ * values less than a particular number.
+ *
+ * @param arrayLike Array or Object iterable with a for-each-loop.
+ * @param elementMatcher Matcher the items must match.
+ * @return A Boolean value of <code>true</code> if all items in the array return true for the specified matcher;
+ * otherwise, <code>false</code>.
+ *
+ * @see org.hamcrest.collection#someByMatcher()
+ *
+ * @example
+ * <listing version="3.0">
+ * var items:Array = [{ value: 1 }, { value: 22 }, { value: 33 }];
+ * var allItemsValid:Boolean = everyByMatcher(items, hasProperties({ value: lessThan(50) }));
+ * if (allItemsValid) {
+ * // ...
+ * }
+ * </listing>
+ */
+ public function everyByMatcher(arrayLike:Object, elementMatcher:Matcher):Boolean
+ {
+ for each (var item:* in arrayLike)
+ {
+ if (! elementMatcher.matches(item))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
View
34 hamcrest/src-flex/org/hamcrest/collection/filterByMatcher.as
@@ -0,0 +1,34 @@
+package org.hamcrest.collection
+{
+ import org.hamcrest.Matcher;
+
+ /**
+ * Executes a Matcher on each item in the array and constructs a new array for all items that return true for the
+ * specified matcher. If an item returns false, it is not included in the new array.
+ *
+ * @param arrayLike Array or Object with a 'length' property and iterable with a for-loop.
+ * @param elementMatcher Matcher the item must match.
+ * @returns A new array that contains all items from the original array that returned <code>true</code>.
+ *
+ * @example
+ * <listing version="3.0">
+ * var items:Array = [{ value: 5 }, { value: 10 }, { value: 15 }, { value: 3 }];
+ * var matching:Array = filterByMatcher(items, hasProperties({ value: greaterThan(5) }));
+ * // matching === [items[1], items[2]]
+ * </listing>
+ */
+ public function filterByMatcher(arrayLike:Object, elementMatcher:Matcher):Array
+ {
+ const matching:Array = [];
+
+ for each (var item:* in arrayLike)
+ {
+ if (elementMatcher.matches(item))
+ {
+ matching[matching.length] = item;
+ }
+ }
+
+ return matching;
+ }
+}
View
35 hamcrest/src-flex/org/hamcrest/collection/indexOfByMatcher.as
@@ -0,0 +1,35 @@
+package org.hamcrest.collection
+{
+ import org.hamcrest.Matcher;
+
+ /**
+ * Searches for an item in an array-like object by using a Matcher and returns the index position of the item.
+ *
+ * @param arrayLike Array or Object with a 'length' property and iterable with a for-loop.
+ * @param elementMatcher Matcher the item must match.
+ * @param startIndex The location in the arrayLike from which to start searching for the item.
+ * @returns A zero-based index position of the item in the array.
+ * If the searchElement argument is not found, the return value is <code>-1</code>.
+ *
+ * @example
+ * <listing version="3.0">
+ * var items:Array = [{ value: 1 }, { value: 5 }, { value: 11 }];
+ * var index:int = indexOfByMatcher(items, hasProperties({ value: 5 }));
+ * // index === 1
+ * </listing>
+ */
+ public function indexOfByMatcher(arrayLike:Object, elementMatcher:Matcher, startIndex:int = 0):int
+ {
+ for (var i:int = startIndex, n:int = arrayLike.length; i < n; i++)
+ {
+ var item:* = arrayLike[i];
+
+ if (elementMatcher.matches(item))
+ {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+}
View
36 hamcrest/src-flex/org/hamcrest/collection/indexesOfByMatcher.as
@@ -0,0 +1,36 @@
+package org.hamcrest.collection
+{
+ import org.hamcrest.Matcher;
+
+ /**
+ * Searches for an item in an array-like object by using a Matcher and returns the index positions of all matching items.
+ *
+ * @param arrayLike Array or Object with a 'length' property and iterable with a for-loop.
+ * @param elementMatcher Matcher the item must match.
+ * @param startIndex The location in the arrayLike from which to start searching for the item.
+ * @return Array of index positions of matching items.
+ *
+ * @example
+ * <listing version="3.0">
+ * var items:Array = [{ value: 5 }, { value: 10 }, { id: 5 }];
+ * var indexes:Array = indexOfByMatcher(items, hasProperties({ value: lessThan(10) }));
+ * // indexes === [0, 2]
+ * </listing>
+ */
+ public function indexesOfByMatcher(arrayLike:Object, elementMatcher:Matcher, startIndex:int = 0):Array
+ {
+ var indexes:Array = [];
+
+ for (var i:int = startIndex, n:int = arrayLike.length; i < n; i++)
+ {
+ var item:* = arrayLike[i];
+
+ if (elementMatcher.matches(item))
+ {
+ indexes[indexes.length] = i;
+ }
+ }
+
+ return indexes;
+ }
+}
View
34 hamcrest/src-flex/org/hamcrest/collection/rejectByMatcher.as
@@ -0,0 +1,34 @@
+package org.hamcrest.collection
+{
+ import org.hamcrest.Matcher;
+
+ /**
+ * Executes a Matcher on each item in the array and constructs a new array for all items that return false for the
+ * specified matcher. If an item returns true, it is not included in the new array.
+ *
+ * @param arrayLike Array or Object with a 'length' property and iterable with a for-loop.
+ * @param elementMatcher Matcher the item must not match.
+ * @return A new array that contains all items from the original array that returned <code>false</code>.
+ *
+ * @example
+ * <listing version="3.0">
+ * var items:Array = [{ value: 5 }, { value: 10 }, { value: 15 }, { value: 3 }];
+ * var matching:Array = rejectByMatcher(items, hasProperties({ value: greaterThan(5) }));
+ * // matching === [items[0], items[3]]
+ * </listing>
+ */
+ public function rejectByMatcher(arrayLike:Object, elementMatcher:Matcher):Array
+ {
+ const mismatched:Array = [];
+
+ for each (var item:* in arrayLike)
+ {
+ if (!elementMatcher.matches(item))
+ {
+ mismatched[mismatched.length] = item;
+ }
+ }
+
+ return mismatched;
+ }
+}
View
37 hamcrest/src-flex/org/hamcrest/collection/someByMatcher.as
@@ -0,0 +1,37 @@
+package org.hamcrest.collection
+{
+ import org.hamcrest.Matcher;
+
+ /**
+ * Executes a Matcher on each item in the array until an item is reached that returns true. Use this method to
+ * determine whether any items in an array meet a criterion, such as having a value less than a particular number.
+ *
+ * @param arrayLike Array or Object iterable with a for-each-loop.
+ * @param elementMatcher Matcher an item must match.
+ * @return A Boolean value of true if any items in the array return <code>true</code> for the Matcher;
+ * otherwise <code>false</code>.
+ *
+ * @see org.hamcrest.collection#everyByMatcher()
+ *
+ * @example
+ * <listing version="3.0">
+ * var items:Array = [{ value: 100 }, { value: 50 }, { value: 20 }];
+ * var anyItemValid:Boolean = someByMatcher(items, hasProperties({ value: lessThan(50) }));
+ * if (anyItemValid) {
+ * // ...
+ * }
+ * </listing>
+ */
+ public function someByMatcher(arrayLike:Object, elementMatcher:Matcher):Boolean
+ {
+ for each (var item:* in arrayLike)
+ {
+ if (elementMatcher.matches(item))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}

0 comments on commit f57ef3b

Please sign in to comment.