This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
/
add_allow_switch_test.rb
151 lines (125 loc) · 3.16 KB
/
add_allow_switch_test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
begin
require 'rubygems'
rescue LoadError
end
gem 'test-spec'
require 'test/spec'
$:.unshift(File.expand_path('../', __FILE__))
require 'add_allow_switch'
SILENT_COMMAND = 'ls > /dev/null'
module Factory
def self.run
true
end
def self.ran(name, &block)
block.call("#{name} was run")
end
end
Factory.add_allow_switch :run, :default => true
Factory.add_allow_switch :ran
describe "Factory with an allow switch on run" do
it "should alias the original method" do
Factory.respond_to?(:_run_before_allow_switch, include_private=true).should == true
end
it "should define a getter and setter" do
Factory.should.respond_to(:allow_run)
Factory.should.respond_to(:allow_run=)
end
it "should switch off" do
Factory.allow_run = false
lambda {
Factory.run
}.should.raise(RuntimeError)
end
it "should switch on" do
Factory.allow_run = true
lambda {
Factory.run.should == true
}.should.not.raise
end
it "should forward passed blocks and arguments" do
Factory.allow_ran = true
Factory.ran('Machine') do |name|
name + '!'
end.should == 'Machine was run!'
end
end
class Bunny
def hip(name, &block)
block.call("#{name} is hip")
end
def hop
'Hop hop!'
end
end
Bunny.add_allow_switch :hip
Bunny.add_allow_switch :hop
describe "Bunny with an allow switch on hop" do
before do
@bunny = Bunny.new
end
it "should alias the original method" do
@bunny.respond_to?(:_hop_before_allow_switch).should == true
end
it "should define a getter and setter" do
Bunny.should.respond_to(:allow_hop)
Bunny.should.respond_to(:allow_hop=)
Bunny.allow_hop.should == false
Bunny.allow_hop = true
Bunny.allow_hop.should == true
Bunny.allow_hop = false
end
it "should switch off" do
Bunny.allow_hop = false
lambda {
@bunny.hop
}.should.raise(RuntimeError)
end
it "should switch on" do
Bunny.allow_hop = true
lambda {
@bunny.hop.should == 'Hop hop!'
}.should.not.raise
Bunny.allow_hop = false
end
it "should forward passed blocks and arguments" do
Bunny.allow_hip = true
@bunny.hip('Bunny') do |name|
name + '!'
end.should == 'Bunny is hip!'
Bunny.allow_hop = false
end
end
Kernel.add_allow_switch :system
describe "Kernel with an allow switch on system" do
it "should alias the original method" do
Kernel.respond_to?(:_system_before_allow_switch, include_private=true).should == true
end
it "should define a getter and setter" do
Kernel.should.respond_to(:allow_system)
Kernel.should.respond_to(:allow_system=)
end
it "should switch off" do
Kernel.allow_system = false
lambda {
system(SILENT_COMMAND)
}.should.raise(RuntimeError)
Kernel.allow_system = false
end
it "should switch on" do
Kernel.allow_system = true
lambda {
system(SILENT_COMMAND)
}.should.not.raise
Kernel.allow_system = false
end
end
Kernel.add_allow_switch :`
describe "Kernel with an allow switch on `" do
it "switches off" do
Kernel.allow_backtick = false
lambda {
`#{SILENT_COMMAND}`
}.should.raise(RuntimeError)
end
end