FLEX-35031 -Added more unit tests for ObjectUtil.getEnumerableProperties. -Added the option of specifying whether strict equality should be used in ArrayUtil.arraysMatch() and ArrayUtil.arrayValuesMatch(), or just a regular equality check.
Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/7a0e9eb9 Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/7a0e9eb9 Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/7a0e9eb9 Branch: refs/heads/develop Commit: 7a0e9eb9f2ec1e8e169652f31bd0512909e7b5b9 Parents: 680b405 Author: Mihai Chira <[email protected]> Authored: Wed Feb 17 13:56:00 2016 +0100 Committer: Mihai Chira <[email protected]> Committed: Wed Feb 17 13:56:00 2016 +0100 ---------------------------------------------------------------------- .../framework/src/mx/utils/ArrayUtil.as | 22 +++++++-- .../framework/tests/mx/utils/ArrayUtil_Tests.as | 29 +++++++++--- .../tests/mx/utils/ObjectUtil_Tests.as | 49 ++++++++++++++++++-- 3 files changed, 85 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/7a0e9eb9/frameworks/projects/framework/src/mx/utils/ArrayUtil.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/src/mx/utils/ArrayUtil.as b/frameworks/projects/framework/src/mx/utils/ArrayUtil.as index 85ec79c..5fd30d9 100644 --- a/frameworks/projects/framework/src/mx/utils/ArrayUtil.as +++ b/frameworks/projects/framework/src/mx/utils/ArrayUtil.as @@ -110,10 +110,13 @@ public class ArrayUtil /** * Checks if the Array instances contain the same values - * against the same indexes. + * against the same indexes, even if in different orders. * * @param a The first Array instance. * @param b The second Array instance. + * @param strictEqualityCheck true if we should compare the + * values of the two Arrays using the strict equality + * operator (===) or not (==). * @return true if the two Arrays contain the same values * (determined using the strict equality operator) associated * with the same indexes. @@ -123,7 +126,7 @@ public class ArrayUtil * @playerversion AIR 1.1 * @productversion Flex 3 */ - public static function arraysMatch(a:Array, b:Array):Boolean + public static function arraysMatch(a:Array, b:Array, strictEqualityCheck:Boolean = true):Boolean { if(!a || !b) return false; @@ -137,7 +140,13 @@ public class ArrayUtil { var index:String = indexesA[i]; - if(!b.hasOwnProperty(index) || a[index] !== b[index]) + if(!b.hasOwnProperty(index)) + return false; + + if(strictEqualityCheck && a[index] !== b[index]) + return false; + + if(!strictEqualityCheck && a[index] != b[index]) return false; } @@ -150,6 +159,9 @@ public class ArrayUtil * * @param a The first Array instance. * @param b The second Array instance. + * @param strictEqualityCheck true if we should compare the + * values of the two Arrays using the strict equality + * operator (===) or not (==). * @return true if the two Arrays contain the same values. * * @langversion 3.0 @@ -157,7 +169,7 @@ public class ArrayUtil * @playerversion AIR 1.1 * @productversion Flex 3 */ - public static function arrayValuesMatch(a:Array, b:Array):Boolean + public static function arrayValuesMatch(a:Array, b:Array, strictEqualityCheck:Boolean = true):Boolean { if(!a || !b) return false; @@ -168,7 +180,7 @@ public class ArrayUtil var valuesOfB:Array = getArrayValues(b); valuesOfB.sort(); - return arraysMatch(valuesOfA, valuesOfB); + return arraysMatch(valuesOfA, valuesOfB, strictEqualityCheck); } /** http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/7a0e9eb9/frameworks/projects/framework/tests/mx/utils/ArrayUtil_Tests.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/tests/mx/utils/ArrayUtil_Tests.as b/frameworks/projects/framework/tests/mx/utils/ArrayUtil_Tests.as index d11f468..36c8b84 100644 --- a/frameworks/projects/framework/tests/mx/utils/ArrayUtil_Tests.as +++ b/frameworks/projects/framework/tests/mx/utils/ArrayUtil_Tests.as @@ -114,22 +114,37 @@ package mx.utils public function test_arrays_match_when_indexes_expressed_in_string_and_int():void { //given + var arrayA:Array = ["value", "value"]; + var arrayB:Array = []; + + //when + arrayB["0"] = "value"; + arrayB["1"] = "value"; + + //then + assertTrue(ArrayUtil.arraysMatch(arrayA, arrayB)); + } + + [Test] + public function test_arrays_dont_match_when_values_expressed_in_string_and_int_if_strict_equality_used():void + { + //given var arrayA:Array = []; var arrayB:Array = []; //when - arrayA[3] = "value"; - arrayA["4"] = "value"; + arrayA[3] = 3; + arrayA[4] = 4; - arrayB["3"] = "value"; - arrayB[4] = "value"; + arrayB[3] = "3"; + arrayB[4] = "4"; //then - assertTrue(ArrayUtil.arraysMatch(arrayA, arrayB)); + assertFalse(ArrayUtil.arraysMatch(arrayA, arrayB, true)); } [Test] - public function test_arrays_dont_match_when_values_expressed_in_string_and_int():void + public function test_arrays_match_when_values_expressed_in_string_and_int_if_strict_equality_not_used():void { //given var arrayA:Array = []; @@ -143,7 +158,7 @@ package mx.utils arrayB[4] = "4"; //then - assertFalse(ArrayUtil.arraysMatch(arrayA, arrayB)); + assertTrue(ArrayUtil.arraysMatch(arrayA, arrayB, false)); } [Test] http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/7a0e9eb9/frameworks/projects/framework/tests/mx/utils/ObjectUtil_Tests.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/tests/mx/utils/ObjectUtil_Tests.as b/frameworks/projects/framework/tests/mx/utils/ObjectUtil_Tests.as index a373267..7a1b2e4 100644 --- a/frameworks/projects/framework/tests/mx/utils/ObjectUtil_Tests.as +++ b/frameworks/projects/framework/tests/mx/utils/ObjectUtil_Tests.as @@ -108,7 +108,7 @@ package mx.utils { public function test_enumerable_properties_of_dictionary():void { //given - var object:Object = new Dictionary(false); + var object:Dictionary = new Dictionary(false); object["name"] = "John"; object["age"] = 9; @@ -120,10 +120,10 @@ package mx.utils { } [Test] - public function test_enumerable_properties_of_dynamic_object():void + public function test_enumerable_properties_of_dynamic_class_instance():void { //given - var object:Object = new DynamicVO("John"); + var object:DynamicVO = new DynamicVO("John"); object["age"] = 9; //when @@ -134,6 +134,49 @@ package mx.utils { } [Test] + public function test_enumerable_properties_of_associative_array():void + { + //given + var object:Array = []; + object["age"] = 9; + object["name"] = "John"; + + //when + var enumerableProperties:Array = ObjectUtil.getEnumerableProperties(object); + + //then + assertTrue(ArrayUtil.arrayValuesMatch(["age", "name"], enumerableProperties)); + } + + [Test] + public function test_enumerable_properties_of_indexed_array():void + { + //given + var object:Array = [9, "John"]; + + //when + var enumerableProperties:Array = ObjectUtil.getEnumerableProperties(object); + + //then + assertTrue(ArrayUtil.arrayValuesMatch([0, 1], enumerableProperties)); + } + + [Test] + public function test_enumerable_properties_of_manually_indexed_array():void + { + //given + var object:Array = []; + object[3] = 9; + object[5] = "John"; + + //when + var enumerableProperties:Array = ObjectUtil.getEnumerableProperties(object); + + //then + assertTrue(ArrayUtil.arrayValuesMatch([3, 5], enumerableProperties)); + } + + [Test] public function test_enumerable_properties_of_sealed_class_instance():void { //given
