Creating a custom TypeSafeDiagnosingMatcher

313 views
Skip to first unread message

Michael McCarthy

unread,
Nov 8, 2011, 4:47:34 AM11/8/11
to mock...@googlegroups.com
This is really a hamcrest as3 question, sorry to hijack the mockolate list but I couldn't find a Hamcrest specific group and I guess technically it applies :)
Does anyone have any pointers on how to create and use a TypeSafeDiagnosingMatcher a la http://blogs.atlassian.com/2009/06/hamcrest_saves_your_soul_now_w/. Annoying I've created a few in Java but I can't work out how it ports to AS3 without the factory annotation.

Any help appreciated

Thanks!

Drew Bourne

unread,
Nov 8, 2011, 6:01:35 AM11/8/11
to mock...@googlegroups.com
Michael,

Looking at the existing TypeSafeDiagnosingMatchers and their package-level factory functions is a good place to start. https://github.com/drewbourne/hamcrest-as3 Bear in mind the lack of generics in AS3 so some things from the Java Hamcrest don't translate across. 

Can you describe the scenario you want to a matcher for?

cheers, 
Drew

Michael McCarthy

unread,
Nov 8, 2011, 7:20:51 AM11/8/11
to mock...@googlegroups.com
Cool, I've been using the HasPropertyWithValue code as a start. I'm trying to check multiple properties on a custom object. Here's the start I have made:

public class PlayerMatcher extends TypeSafeDiagnosingMatcher {

        private var _userNameMatcher:Matcher;

        public function PlayerMatcher(player:Player) {

            super(Player);
            _userNameMatcher.matches(player.userName);

        }

        override public function matchesSafely(item:Object, mismatchDescription:Description):Boolean {

            var matches:Boolean = true;

            if (!item) {
                mismatchDescription
                        .appendText('Trying to compare against a null object ')
                matches = false;
            }

            var player:Player = (item as Player);
            var userName:String = player.userName;
            if (!_userNameMatcher.matches(userName)) {
                mismatchDescription
                        .appendText('userName ')
                        .appendValue(userName)
                        .appendText(' ')
                        .appendMismatchOf(_userNameMatcher, userName);
            }

            return matches;

        }
    }

There would be additional matchers in there for each property of this object. How would I go about making the call from the test code (that replaces multiple assertions across two objects?)

Thanks!

Michael McCarthy

unread,
Nov 8, 2011, 7:31:06 AM11/8/11
to mock...@googlegroups.com
Ok, this I have got it - found the syntactic sugar type methods such as https://github.com/drewbourne/hamcrest-as3/blob/master/hamcrest/src/org/hamcrest/object/hasPropertyChain.as. Cheers!
Reply all
Reply to author
Forward
0 new messages