Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added matchers/macros for testing.

  • Loading branch information...
commit 7dc38f3a8c3c43348e59d747626f33cfbb2c3fa9 1 parent 0a64245
@technicalpickles technicalpickles authored
View
20 lib/timeline_fu/macros.rb
@@ -0,0 +1,20 @@
+module TimelineFu
+ module Macros
+ def should_fire_event(event_type, opts = {})
+ should "fire #{event_type} on #{opts[:on]}" do
+ matcher = fire_event(event_type, opts)
+
+ assert_accepts matcher, self.class.name.gsub(/Test$/, '').constantize
+ end
+ end
+
+ def should_not_fire_event(event_type, opts = {})
+ should "fire #{event_type} on #{opts[:on]}" do
+ matcher = fire_event(event_type, opts)
+
+ assert_rejects matcher, self.class.name.gsub(/Test$/, '').constantize
+ end
+ end
+
+ end
+end
View
58 lib/timeline_fu/matchers.rb
@@ -0,0 +1,58 @@
+module TimelineFu
+ module Matchers
+ class FireEvent
+ def initialize(event_type, opts = {})
+ @event_type = event_type
+ @opts = opts
+ @method = :"fire_#{@event_type}_after_#{@opts[:on]}"
+ end
+
+ def matches?(subject)
+ @subject = subject
+
+ defines_callback_method? && setups_up_callback?
+ end
+
+ def defines_callback_method?
+ if @subject.instance_methods.include?(@method.to_s)
+ true
+ else
+ @missing = "#{@subject.name} does not respond to #{@method}"
+ false
+ end
+ end
+
+ def setups_up_callback?
+ callback_chain_name = "after_#{@opts[:on]}_callback_chain"
+ callback_chain = @subject.send(callback_chain_name)
+ if callback_chain.any? {|chain| chain.method == @method }
+ true
+ else
+ @missing = "does setup after #{@opts[:on]} callback for #{@method}"
+ false
+ end
+ end
+
+ def description
+ "fire a #{@event_type} event"
+ end
+
+ def expectation
+ expected = "#{@subject.name} to #{description}"
+ end
+
+ def failure_message
+ "Expected #{expectation} (#{@missing})"
+ end
+
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
+
+ end
+
+ def fire_event(event_type, opts)
+ FireEvent.new(event_type, opts)
+ end
+ end
+end
View
11 shoulda_macros/timeline_fu_shoulda.rb
@@ -0,0 +1,11 @@
+require 'timeline_fu/matchers'
+require 'timeline_fu/macros'
+
+module ActiveSupport
+ class TestCase
+ include TimelineFu::Matchers
+ if ! defined? Spec
+ extend TimelineFu::Macros
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.