Sha256: 684f9309aa6dccef3509e3451bf6f0e7fa3be7b1f6e30aff59fae9fcd26e8fb2

Contents?: true

Size: 1.69 KB

Versions: 23

Compression:

Stored size: 1.69 KB

Contents

class RecordedEvent
  attr_reader :name, :data

  def initialize(name, data)
    @name = name.to_s
    @data = data
  end

  def inspect
    "<Event:#{name} #{inspect_data}>"
  end

  def ==(other)
    name == other.name && data == other.data
  end

  private

  def inspect_data
    data.inject([]) { |result, (key, value)|
      result << "#{key.inspect} => #{value.inspect.slice(0, 20)}"
    }.join(", ")
  end
end

class RecordingObserver
  def self.instance
    @@instance ||= self.new
  end

  attr_accessor :events

  def initialize
    @events = []
  end

  def respond_to?(message)
    true
  end

  def method_missing(name, data)
    @events << RecordedEvent.new(name, data)
  end
end

RSpec::Matchers.define :notify_observers do |event_name, data|
  match do |ignored_subject|
    @event = RecordedEvent.new(event_name, data)
    recorder.events.include? @event
  end

  failure_message do
    "Expected event:\n#{@event.inspect}\n\nGot events:\n#{recorder.events.map(&:inspect).join("\n")}"
  end

  def recorder
    RecordingObserver.instance
  end
end

RSpec::Matchers.define :notify_observers_once do |event_name, data|
  match do |ignored_subject|
    @event = RecordedEvent.new(event_name, data)
    recorder.events.select{|ev| ev == @event }.size.should == 1
  end

  failure_message do
    times_notified = recorder.events.select{|ev| ev == @event }.size
    "Expected event 1 time:\n#{@event.inspect}\n\nGot event #{times_notified} times"
  end

  def recorder
    RecordingObserver.instance
  end
end

RSpec.configure do |config|
  config.before do
    Saucy::Notifications.clear_observers
    Saucy::Notifications.register_observer(RecordingObserver.instance)
    RecordingObserver.instance.events = []
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
saucy-0.16.1 spec/support/notifications.rb
saucy-0.16.0 spec/support/notifications.rb
saucy-0.15.2 spec/support/notifications.rb
saucy-0.15.1 spec/support/notifications.rb
saucy-0.15.0 spec/support/notifications.rb
saucy-0.14.5 spec/support/notifications.rb
saucy-0.14.3 spec/support/notifications.rb
saucy-0.14.2 spec/support/notifications.rb
saucy-0.14.1 spec/support/notifications.rb
saucy-0.14.0 spec/support/notifications.rb
saucy-0.13.3 spec/support/notifications.rb
saucy-0.13.2 spec/support/notifications.rb
saucy-0.13.1 spec/support/notifications.rb
saucy-0.13.0 spec/support/notifications.rb
saucy-0.12.5 spec/support/notifications.rb
saucy-0.12.4 spec/support/notifications.rb
saucy-0.12.3 spec/support/notifications.rb
saucy-0.12.2 spec/support/notifications.rb
saucy-0.12.1 spec/support/notifications.rb
saucy-0.12.0 spec/support/notifications.rb