Roop says

I created gravity?

Roop says header image 1

Actionscript 3.0 Object Equality

September 2nd, 2007 · 9 Comments

This weekend I began tinkering around with FlexUnit 2.0 for Actionscript 3.0. So far I am excited about the framework. It doesn’t do much, but it does the basics. When writing some of my tests I found I needed to compare two objects together. I know how to handle this in Java you would define the method equals(Object o) on the Customer object. I searched for a few hours and could find no such solution in Actionscript. Ofcourse I could make it my own standard to define this method but then frameworks like FlexUnit wouldn’t know anything about it.

Finally after searching I came across mx.utils.ObjectUtil. This object has numerous methods on it that expose the innards of objects. It has an especially powerful compare method that will transverse an object and compare each sub element testing for equality. This eliminates the need to write most equals methods on objects, but unfortunately it doesn’t get automatically used when you use the equality (==) operator.

This confuses me because the docs for equality (==) say it actually compares the value of the operand and strict equality (===) compares the memory references. Unfortunately this doesn’t seem to apply to objects as == and === have the same outcome in all my tests.

Although I am a little dissappoitned I can’t write something like:


object1 == object2

I have to instead write:


import mx.utils.ObjectUtil;
ObjectUtil.compare(object1, object2) == 0

I wouldn’t mind as much if FlexUnit took this into account with there assertEquals method. Currently I believe it just runs an equality (==) operation and is done. So my tests have to use the assertTrue.

Anyways I wanted to write this post to do one of two things.

  1. Save someone the trouble of searching for hours to be as dissappointed as me
  2. Have someone point out my flaws and show me the easy great Actionscript way to do equality
Tags: ,

Filed Under: languages · programming

9 responses so far ↓

  • 1 Sandro // Oct 29, 2007 at 1:19 pm

    Did you find a solution in the meantime?
    The problem is serious: what about checking if a list contains an Object (using a Java “equals” like way?).

  • 2 Michal Kuklis // Dec 4, 2007 at 2:53 pm

    thank you,
    you saved me hours ;-)

  • 3 Daniel Roop // Dec 4, 2007 at 9:11 pm

    @Michal

    Glad I could help. it is a little annoying, but really at the end of the day, it is just the frameworks that need to adopt something.

  • 4 SJ // Feb 6, 2008 at 6:51 pm

    For tests you can use something like:

    public static function assertObjectEquals(msg:String, exp:Object, act:Object):void {

    if (ObjectUtil.compare(exp, act) != 0) {
    Assert.fail(msg + “: expected , but was “);
    }
    }

    FlexUnit should support this more natively and no question ActionScript should add Object.equals() and have the == operator use it.

  • 5 Daniel Roop // Feb 8, 2008 at 11:30 pm

    @SJ

    Thanks for the input. I agree I could right my own assert method, but ultimately I would like to see FlexUnit do this for me, as it seems to be useful outside of just my tests. Also I would prefer it to be the same assertEquals method instead of having a seperate name.

  • 6 Bill Shirley // Apr 7, 2008 at 2:39 pm

    Definitely a design oversight. Especially when selling Flex a client/server solution - there will all kinds of “duplicate” copies of instances that are the same.

    The collectionContains functionality becomes complicated when it should be a no-brainer.

    Glad to find your post and confirm my assumptions and solutions.

  • 7 codecraig // May 15, 2008 at 6:19 am

    Is this still the case???? I hope not!

    I could create an interface that has an “equals” method and then have any classes I care about comparing implement it. Then create a special subclass of ArrayCollection (or some other collection type) that only uses objects that implement my interface (i.e. think using generics in Java to enforce this). Then override the contains method in the collection.

    Wow….totally lame.

  • 8 Daniel Roop // May 15, 2008 at 8:54 am

    @codecraig

    I believe so and yes it should be relatively simple to implement this yourself, but ideally someone will update the Flex Libraries to include this since most of it is open source now.

  • 9 Ryan Christiansen // Jul 7, 2008 at 8:23 am

    I’ve been developing a FlexUnit alternative called FUnit and came across this issue this weekend. Through my research, I found that FlexUnit is definitely an exception to the rule. The result of my findings can be found here:

    http://blog.funit.org/?p=13

    Pending community feedback, Assert.areEqual() will support member-wise value comparison like what has been suggested here, and NOT reference comparison. A second Assert.areSame() will be available for that purpose.

Leave a Comment