Forums Archived

The forums are being archived. See this topic for more information.

Unit tests on Hooks, any ideas?

Get help and support with Rubygame

Unit tests on Hooks, any ideas?

Postby cgmjr » Sun Jan 31, 2010 4:45 am

So things are progressing nicely for my little project. I'm trying to stay disciplined about my unit tests, and I need some advise from you guys.

I have a base class in which I'm mixing in Rubygame::EventHandler::HasEventHandler and setting up handlers for just the basic keyboard keys to quit. ( I shamelessly stole from the Image Viewer Demo in the Cookbook, thank you very much jacius :)
Code: Select all
    @hooks=make_magic_hooks( :q            => :exit_script,
                             :escape       => :exit_script,
                             :mouse_right  => :exit_script,
                             QuitRequested => :exit_script
                           )

I'd like to add a unit test to ensure that those hooks are still present in the base class, and in subclasses. I'd like to actually test that there is a hook that will respond to, say, :q. But I don't see a way to inspect the Key that is present in the Trigger for the Hook.

Sounds like that nursery rhyme...wart on the frog on the knot on the log...ha ha...I kill me...ahem...sorry ...getting late. Anyway,

Got any ideas? I would accept that it is a worthless test, I suppose.
User avatar
cgmjr
 
Posts: 39
Joined: Mon Jan 11, 2010 6:41 am

Re: Unit tests on Hooks, any ideas?

Postby jacius » Mon Feb 01, 2010 8:11 am

Hrm. You are right that there's no clean way to inspect the trigger's attributes. I suppose I should add that, eh? :lol:

In the meantime, I would suggest creating a KeyPressed instance and passing it to the handler, and using some technique to verify that the exit_script method gets called. The way you verify it depends on what library you're using for your tests.

If I were writing it in RSpec, I would do it something like this (I haven't actually run this code, it may be broken):
Code: Select all
describe MyClass do
  it "should exit_script when it handles a Q key press event" do
    mything = MyClass.new
    event = Rubygame::Events::KeyPressed.new(:q)
    mything.should_receive(:exit_script).at_least(:once).and_return(nil)
    mything.handle(event)
  end
end

Another way would be to create a subclass where the exit_script does something that you can detect, like throwing a symbol or modifying a variable, and then check that it happens when you give it the event.

Hope that helps. If you need more help, tell us what test library you're using and I could come up with a specific example for that. :)
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm

Re: Unit tests on Hooks, any ideas?

Postby cgmjr » Mon Feb 01, 2010 3:47 pm

Ah, nice! I am just using the standard test/unit at the moment. Once I have become more comfortable with Ruby, Rubygame, and Netbeans I'll get my BDD on :)

I like the subclassing idea a lot, since I intend to do that quite a bit going forward. I suppose the only real value in testing against my core base class is in case something in Rubygame or Ruby or SDL or such changes in the future.

Thanks jacius.

Edit: that worked pretty good. But jacius, I wonder, what would be the impact of changing Rubygame::EventHandler#handle to return a boolean instead of nil? If it were to return true if any match? happened, we could do something like:

Code: Select all
assert_true(my_class.handle(Rubygame::Events::KeyPressed.new(:q)))


That'd be kinda cool.
User avatar
cgmjr
 
Posts: 39
Joined: Mon Jan 11, 2010 6:41 am

Re: Unit tests on Hooks, any ideas?

Postby jacius » Mon Feb 01, 2010 8:54 pm

cgmjr wrote:I wonder, what would be the impact of changing Rubygame::EventHandler#handle to return a boolean instead of nil? If it were to return true if any match?

That's an interesting idea. I'll definitely give that some strong consideration for the future. :)
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm


Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron