summaryrefslogtreecommitdiff
path: root/test/unit/handler_chain_test.rb
blob: fae0418b80302910ba05a4d190f31c58695d98f8 (plain)
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
68
require 'test_helper'
require 'nickserver/handler_chain'

class HandlerChainTest < Minitest::Test

  def test_initialization
    assert chain
  end

  def test_noop
    assert_nil chain.handle
  end

  def test_triggering_handlers
    handler_mock.expect :call, nil, [:a, :b]
    chain handler_mock
    chain.handle :a, :b
    handler_mock.verify
  end

  def test_returns_handler_result
    chain  handler_with_nil, handler_with_result
    assert_equal :result, chain.handle
  end

  def test_raise_exception
    chain handler_raising, handler_with_result
    assert_raises RuntimeError do
      chain.handle
    end
  end

  def test_continue_on_exception
    chain handler_raising, handler_with_result
    chain.continue_on(RuntimeError)
    assert_equal :result, chain.handle
    assert_equal [RuntimeError], chain.rescued_exceptions.map(&:class)
  end

  def test_continue_on_exception_with_nil
    chain handler_raising, handler_with_nil
    chain.continue_on(RuntimeError)
    assert_nil chain.handle
    assert_equal [RuntimeError], chain.rescued_exceptions.map(&:class)
  end

  protected

  def chain(*handlers)
    @chain ||= Nickserver::HandlerChain.new(*handlers)
  end

  def handler_mock
    @handler ||= Minitest::Mock.new
  end

  def handler_with_nil
    Proc.new {}
  end

  def handler_with_result
    Proc.new { :result }
  end

  def handler_raising(exception = RuntimeError)
    Proc.new { raise exception }
  end
end