File: callback_spec.rb

package info (click to toggle)
ruby-ponder 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 240 kB
  • sloc: ruby: 1,051; makefile: 4
file content (67 lines) | stat: -rw-r--r-- 2,113 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'spec_helper'
require 'ponder/thaum'

describe Ponder::Callback do
  before(:all) { @proc = Proc.new { } }

  before(:each) do
    @ponder = Ponder::Thaum.new { |c| c.verbose = true }
  end

  context 'tries to create a callback' do
    it 'with valid arguments' do
      expect { Ponder::Callback.new(:channel, /foo/, @proc) }.not_to raise_error
    end

    it 'with an invalid type' do
      expect { Ponder::Callback.new(:invalid, /foo/, @proc) }.to raise_error(TypeError)
    end

    it 'with an invalid match' do
      expect { Ponder::Callback.new(:channel, 8, @proc) }.to raise_error(TypeError)
    end

    it 'with an invalid proc' do
      expect { Ponder::Callback.new(:channel, /foo/, {}, 8) }.to raise_error(TypeError)
    end
  end

  it "calls the callback's proc on right match" do
    callback = Ponder::Callback.new(:channel, /wizzard/, {}, Proc.new { 8 })
    expect(callback.call(:channel, {:message => 'I like wizzards'})).to eql(8)
  end

  it "does not call the callback's proc on the wrong match" do
    p = Proc.new { 8 }
    expect(p).not_to receive(:call)
    callback = Ponder::Callback.new(:channel, /wizzard/, p)
    expect(callback.call(:channel, {:message => 'I like hot dogs'})).to be_nil
  end

  it "calls the callback's proc on the right match and the right event type" do
    # `@proc.should_receive(:call).once` does not work here in 1.8.7
    proc = Proc.new { @called = true }
    @ponder.on(:channel, /wizzard/, &proc)
    EM.run do
      @ponder.process_callbacks(:channel, {:message => 'I like wizzards'})
      EM.schedule { EM.stop }
    end

    expect(@called).to be_truthy
  end

  it "calls the callback's proc on the right match and the right event type with multiple types" do
    # `@proc.should_receive(:call).once` does not work here in 1.8.7
    proc = Proc.new { @called = true }
    @ponder.on([:channel, :query], /wizzard/, &proc)
    EM.run do
      @ponder.process_callbacks(:query, {:message => 'I like wizzards'})
      EM.schedule { EM.stop }
    end

    expect(@called).to be_truthy
  end
end