Sha256: e382e170f77f5a8d345a1ccfa045f16eb344493da9d79bffc46b0a0d1b964b3a

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

require File.expand_path 'test_helper', File.dirname(__FILE__)

class TestEventEmitter < MiniTest::Unit::TestCase

  class Foo
    include EventEmitter
    attr_accessor :created_at
  end

  def setup
    @foo = Foo.new
    @foo.created_at = @now = Time.now
  end
  
  def test_on_emit
    result = nil
    __created_at = nil
    @foo.on :chat do |data|
      result = data
      __created_at = created_at
    end

    @foo.emit :chat, :user => 'shokai', :message => 'hello world'

    assert result[:user] == 'shokai'
    assert result[:message] == 'hello world'
    assert __created_at == @now, 'instance method'
  end

  def test_add_listener
    result = nil
    __created_at = nil
    @foo.add_listener :chat do |data|
      result = data
      __created_at = created_at
    end

    @foo.emit :chat, :user => 'shokai', :message => 'hello world'

    assert result[:user] == 'shokai'
    assert result[:message] == 'hello world'
    assert __created_at == @now, 'instance method'
  end

  def test_remove_listener
    @foo.on :bar do |data|
      puts "bar #{data}"
    end
    @foo.on :bar do |data|
      puts "barbar: #{data}"
    end

    id = @foo.on :baz do |data|
      p data
    end

    assert @foo.events.size == 3, 'check registerd listener count'
    @foo.remove_listener id
    assert @foo.events.size == 2, 'remove listener by id'

    @foo.remove_listener :bar
    assert @foo.events.size == 0, 'remove all "bar" listener'
  end

  def test_once
    total = 0
    @foo.once :add do |data|
      total += data
    end

    @foo.emit :add, 1
    assert total == 1, 'first call'
    @foo.emit :add, 1
    assert total == 1, 'call listener only first time'
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
event_emitter-0.0.6 test/test_event_emitter.rb
event_emitter-0.0.5 test/test_event_emitter.rb
event_emitter-0.0.4 test/test_event_emitter.rb
event_emitter-0.0.3 test/test_event_emitter.rb