This repository has been archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stdiotrap_spec.rb
204 lines (176 loc) · 4.61 KB
/
stdiotrap_spec.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'spec_helper'
require 'stdiotrap'
describe StdioTrap do
describe 'trap!' do
it "raises RuntimeError when called twice" do
lambda {
begin
StdioTrap.trap!
StdioTrap.trap!
ensure
StdioTrap.release!
end
}.should raise_error RuntimeError
end
it "raises RuntimeError when called inside capture{}" do
lambda {
begin
StdioTrap.trap!
StdioTrap.trap!
ensure
StdioTrap.release!
end
}.should raise_error RuntimeError
end
it "can fake stdin" do
StdioTrap.trap!('foobar')
input = $stdin.read
StdioTrap.release!
input.should == 'foobar'
end
end
describe 'release!' do
it "raises RuntimeError when called twice" do
lambda {
begin
StdioTrap.trap!
StdioTrap.release!
StdioTrap.release!
ensure
StdioTrap.release!
end
}.should raise_error RuntimeError
end
it "raises RuntimeError when called before trap!" do
lambda {
StdioTrap.release!
}.should raise_error RuntimeError
end
it "raises RuntimeError when called inside capture{}" do
lambda {
trapped = StdioTrap.capture {
puts "Hello world!"
StdioTrap.release!
}
}.should raise_error RuntimeError
end
it "returns to_h" do
StdioTrap.trap!
puts "foo"
want = StdioTrap.to_h
got = StdioTrap.release!
got[:stdout].should == "foo\n"
want.should == got
end
end
describe 'trap!, release!' do
before :each do
StdioTrap.trap!
end
after :each do
StdioTrap.release!
end
it "captures stdout" do
puts "Hello world!"
StdioTrap.stdout.should == "Hello world!\n"
end
it "captures stderr" do
$stderr.puts "Hello world!"
StdioTrap.stderr.should == "Hello world!\n"
end
it "captures stdout and stderr" do
puts "Hello world!"
$stderr.puts "Hello other world!"
StdioTrap.stdout.should == "Hello world!\n"
StdioTrap.stderr.should == "Hello other world!\n"
end
end
describe 'capture' do
it "captures stdout" do
trapped = StdioTrap.capture {
puts "Hello world!"
}
trapped[:stdout].should == "Hello world!\n"
end
it "captures stderr" do
trapped = StdioTrap.capture {
$stderr.puts "Hello world!"
}
trapped[:stderr].should == "Hello world!\n"
end
it "captures stdout and stderr" do
trapped = StdioTrap.capture {
$stdout.puts "Hello world!"
$stderr.puts "Hello other world!"
}
trapped[:stdout].should == "Hello world!\n"
trapped[:stderr].should == "Hello other world!\n"
end
it "can fake stdin" do
trapped = StdioTrap.capture('foobar') {
input = $stdin.read
input.should == 'foobar'
}
end
end
describe 'status' do
it "returns correct status before trap!" do
StdioTrap.status.should == false
end
it "returns correct status after trap!" do
StdioTrap.trap!
StdioTrap.status.should == true
StdioTrap.release!
end
it "returns correct status after trap!, release!" do
StdioTrap.trap!
StdioTrap.release!
StdioTrap.status.should == false
end
it "returns correct status inside capture{}" do
StdioTrap.capture {
StdioTrap.status.should == true
}
end
it "returns correct status after capture{}" do
StdioTrap.capture {
}
StdioTrap.status.should == false
end
end
describe 'to_h' do
it "works inside capture{}" do
StdioTrap.capture {
puts "Hello world!"
$stderr.puts "Hello other world!"
state = StdioTrap.to_h
state.keys.length.should == 2
state[:stdout].should == "Hello world!\n"
state[:stderr].should == "Hello other world!\n"
}
end
it "works between trap! and release!" do
StdioTrap.trap!
puts "Hello world!"
$stderr.puts "Hello other world!"
state = StdioTrap.to_h
state.keys.length.should == 2
state[:stdout].should == "Hello world!\n"
state[:stderr].should == "Hello other world!\n"
StdioTrap.release!
end
it "raises RuntimeError when called before trap!" do
lambda {
StdioTrap.to_h
}.should raise_error RuntimeError
end
it "raises RuntimeError when called after release!" do
lambda {
StdioTrap.trap!
puts "Hello world!"
StdioTrap.release!
StdioTrap.to_h
}.should raise_error RuntimeError
end
end
end