You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by mi...@apache.org on 2015/06/07 18:10:02 UTC

[3/8] git commit: [flex-sdk] [refs/heads/develop] - FLEX-34852 Adding a (unit tested) method to ObjectUtil which will be used by ComplexSortField to retrieve the value of the property at the end of the specified property chain, which will then be used fo

FLEX-34852
Adding a (unit tested) method to ObjectUtil which will be used by ComplexSortField to retrieve the value of the property at the end of the specified property chain, which will then be used for sorting. If the value cannot be reached, the function returns undefined, so as to differentiate from a valid "null" value.


Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/649d811e
Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/649d811e
Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/649d811e

Branch: refs/heads/develop
Commit: 649d811e7930bdd0b11ad1d9b645f944d2b844d1
Parents: bf6d069
Author: Mihai Chira <mi...@apache.org>
Authored: Sun Jun 7 13:08:31 2015 +0200
Committer: Mihai Chira <mi...@apache.org>
Committed: Sun Jun 7 13:08:31 2015 +0200

----------------------------------------------------------------------
 .../framework/src/mx/utils/ObjectUtil.as        |  34 ++++-
 .../tests/ObjectUtil_FLEX_34852_Tests.as        | 127 +++++++++++++++++++
 2 files changed, 160 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/649d811e/frameworks/projects/framework/src/mx/utils/ObjectUtil.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/utils/ObjectUtil.as b/frameworks/projects/framework/src/mx/utils/ObjectUtil.as
index 2d6e7c2..791bbce 100644
--- a/frameworks/projects/framework/src/mx/utils/ObjectUtil.as
+++ b/frameworks/projects/framework/src/mx/utils/ObjectUtil.as
@@ -1199,7 +1199,39 @@ public class ObjectUtil
         }
         return true;
     }
-    
+
+    /**
+     *  Returns the value at the end of the property chain <code>path</code>.
+     *  If the value cannot be reached due to null links on the chain,
+     *  <code>undefined</code>  is returned.
+     *
+     *  @param obj The object at the beginning of the property chain
+     *  @param path The path to inspect (e.g. "address.street")
+     *
+     *  @return the value at the end of the property chain, <code>undefined</code>
+     *  if it cannot be reached, or the object itself when <code>path</code> is empty.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 9
+     *  @playerversion AIR 1.1
+     *  @productversion Flex 3
+     */
+    public static function getValue(obj:Object, path:Array):*
+    {
+        if(!obj)
+            return undefined;
+
+        if(!path || !path.length)
+            return obj;
+
+        var result:* = obj;
+        var i:int = -1;
+        while(++i < path.length && result)
+            result = result.hasOwnProperty(path[i]) ? result[path[i]] : undefined;
+
+        return result;
+    }
+
     /**
      *  @private
      */

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/649d811e/frameworks/projects/framework/tests/ObjectUtil_FLEX_34852_Tests.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/tests/ObjectUtil_FLEX_34852_Tests.as b/frameworks/projects/framework/tests/ObjectUtil_FLEX_34852_Tests.as
new file mode 100644
index 0000000..13820ae
--- /dev/null
+++ b/frameworks/projects/framework/tests/ObjectUtil_FLEX_34852_Tests.as
@@ -0,0 +1,127 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package {
+    import mx.utils.ObjectUtil;
+
+    import org.flexunit.assertThat;
+
+    import org.flexunit.asserts.assertEquals;
+
+    public class ObjectUtil_FLEX_34852_Tests {
+        [Test]
+        public function test_getValue_for_two_field_path():void
+        {
+            //given
+            const streetPrefix:String = "Street no. ";
+            const index:int = 1;
+            var obj:ObjectUtil_FLEX_34852_VO = new ObjectUtil_FLEX_34852_VO(index, "SomeObject", streetPrefix);
+
+            //when
+            var streetName:String = ObjectUtil.getValue(obj, ["address", "street"]) as String;
+
+            //then
+            assertEquals(streetPrefix + index, streetName);
+        }
+
+        [Test]
+        public function test_getValue_for_one_field_path():void
+        {
+            //given
+            const streetPrefix:String = "Street no. ";
+            const index:int = 1;
+            var obj:ObjectUtil_FLEX_34852_VO = new ObjectUtil_FLEX_34852_VO(index, "SomeObject", streetPrefix);
+
+            //when
+            var result:* = ObjectUtil.getValue(obj, ["address"]);
+
+            //then
+            assertEquals(obj.address, result);
+        }
+
+        [Test]
+        public function test_getValue_for_zero_field_path_returns_parameter():void
+        {
+            //given
+            var obj:ObjectUtil_FLEX_34852_VO = new ObjectUtil_FLEX_34852_VO(1, "SomeObject", "Street no. ");
+
+            //when
+            var result:* = ObjectUtil.getValue(obj, null);
+
+            //then
+            assertEquals(obj, result);
+        }
+
+        [Test]
+        public function test_getValue_for_null_object_returns_undefined():void
+        {
+            //given
+            var obj:ObjectUtil_FLEX_34852_VO = null;
+
+            //when
+            var result:* = ObjectUtil.getValue(obj, ["address", "street"]);
+
+            //then
+            assertThat(result === undefined);
+        }
+
+        [Test]
+        public function test_getValue_for_wrong_path_returns_undefined():void
+        {
+            //given
+            var obj:ObjectUtil_FLEX_34852_VO = new ObjectUtil_FLEX_34852_VO(1, "SomeObject", "Street no. ");
+
+            //when
+            var result:* = ObjectUtil.getValue(obj, ["address", "name"]);
+
+            //then
+            assertThat(result === undefined);
+        }
+    }
+}
+
+class ObjectUtil_FLEX_34852_VO
+{
+    [Bindable]
+    public var name:String;
+
+    [Bindable]
+    public var address:ObjectUtil_FLEX_34852_AddressVO;
+
+    [Bindable]
+    public var index:Number;
+
+    public function ObjectUtil_FLEX_34852_VO(index:Number, namePrefix:String, streetPrefix:String)
+    {
+        this.index = index;
+        this.name = namePrefix + index;
+        this.address = new ObjectUtil_FLEX_34852_AddressVO(streetPrefix + index);
+    }
+}
+
+class ObjectUtil_FLEX_34852_AddressVO
+{
+    [Bindable]
+    public var street:String;
+
+    public function ObjectUtil_FLEX_34852_AddressVO(street:String)
+    {
+        this.street = street;
+    }
+}
\ No newline at end of file