changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module 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
74end