]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/matchers/throw_symbol.rb
Show whether a trace is public or private in the trace list, so that a user can easil...
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / matchers / throw_symbol.rb
1 module Spec
2   module Matchers
3     
4     class ThrowSymbol #:nodoc:
5       def initialize(expected=nil)
6         @expected = expected
7         @actual = nil
8       end
9       
10       def matches?(proc)
11         begin
12           proc.call
13         rescue NameError => e
14           raise e unless e.message =~ /uncaught throw/
15           @actual = e.name.to_sym
16         ensure
17           if @expected.nil?
18             return @actual.nil? ? false : true
19           else
20             return @actual == @expected
21           end
22         end
23       end
24
25       def failure_message
26         if @actual
27           "expected #{expected}, got #{@actual.inspect}"
28         else
29           "expected #{expected} but nothing was thrown"
30         end
31       end
32       
33       def negative_failure_message
34         if @expected
35           "expected #{expected} not to be thrown"
36         else
37           "expected no Symbol, got :#{@actual}"
38         end
39       end
40       
41       def description
42         "throw #{expected}"
43       end
44       
45       private
46       
47         def expected
48           @expected.nil? ? "a Symbol" : @expected.inspect
49         end
50       
51     end
52  
53     # :call-seq:
54     #   should throw_symbol()
55     #   should throw_symbol(:sym)
56     #   should_not throw_symbol()
57     #   should_not throw_symbol(:sym)
58     #
59     # Given a Symbol argument, matches if a proc throws the specified Symbol.
60     #
61     # Given no argument, matches if a proc throws any Symbol.
62     #
63     # == Examples
64     #
65     #   lambda { do_something_risky }.should throw_symbol
66     #   lambda { do_something_risky }.should throw_symbol(:that_was_risky)
67     #
68     #   lambda { do_something_risky }.should_not throw_symbol
69     #   lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
70     def throw_symbol(sym=nil)
71       Matchers::ThrowSymbol.new(sym)
72     end
73   end
74 end