This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
throw_symbol.rb
74 lines (65 loc) · 1.8 KB
/
throw_symbol.rb
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
69
70
71
72
73
74
module Spec
module Matchers
class ThrowSymbol #:nodoc:
def initialize(expected=nil)
@expected = expected
@actual = nil
end
def matches?(given_proc)
begin
given_proc.call
rescue NameError => e
raise e unless e.message =~ /uncaught throw/
@actual = e.name.to_sym
ensure
if @expected.nil?
return @actual.nil? ? false : true
else
return @actual == @expected
end
end
end
def failure_message
if @actual
"expected #{expected}, got #{@actual.inspect}"
else
"expected #{expected} but nothing was thrown"
end
end
def negative_failure_message
if @expected
"expected #{expected} not to be thrown"
else
"expected no Symbol, got :#{@actual}"
end
end
def description
"throw #{expected}"
end
private
def expected
@expected.nil? ? "a Symbol" : @expected.inspect
end
end
# :call-seq:
# should throw_symbol()
# should throw_symbol(:sym)
# should_not throw_symbol()
# should_not throw_symbol(:sym)
#
# Given a Symbol argument, matches if the given proc throws the specified Symbol.
#
# Given no argument, matches if a proc throws any Symbol.
#
# == Examples
#
# lambda { do_something_risky }.should throw_symbol
# lambda { do_something_risky }.should throw_symbol(:that_was_risky)
#
# lambda { do_something_risky }.should_not throw_symbol
# lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
def throw_symbol(sym=nil)
Matchers::ThrowSymbol.new(sym)
end
end
end