summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/matchers/block_caller.rb
blob: 30fab4fc682eb416503977e8e119a52fa72d5c03 (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
class BlockingMatcher
  def matches?(block)
    t = Thread.new do
      block.call
    end

    loop do
      case t.status
      when "sleep"    # blocked
        t.kill
        t.join
        return true
      when false      # terminated normally, so never blocked
        t.join
        return false
      when nil        # terminated exceptionally
        t.value
      else
        Thread.pass
      end
    end
  end

  def failure_message
    ['Expected the given Proc', 'to block the caller']
  end

  def negative_failure_message
    ['Expected the given Proc', 'to not block the caller']
  end
end

module MSpecMatchers
  private def block_caller
    BlockingMatcher.new
  end
end