Skip to content

Commit 935bc75

Browse files
committed
Added write expectation, to test output to $stdout/$stderr.
1 parent 835959f commit 935bc75

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.3
1+
1.1.4

lib/rdf/spec/matchers.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,73 @@ module Matchers
179179
end
180180
end
181181
end
182+
183+
RSpec::Matchers.define :write do |message|
184+
chain(:to) do |io|
185+
@io = io
186+
end
187+
188+
supports_block_expectations {true}
189+
190+
match do |block|
191+
@output =
192+
case io
193+
when :output then fake_stdout(&block)
194+
when :error then fake_stderr(&block)
195+
else raise("Allowed values for `to` are :output and :error, got `#{io.inspect}`")
196+
end
197+
@output.include? message
198+
end
199+
200+
description do
201+
"write \"#{message}\" #{io_name}"
202+
end
203+
204+
failure_message do
205+
@exception ? @exception.message :
206+
"expected to include #{description.inspect} in #{@output.inspect}"
207+
end
208+
209+
failure_message_when_negated do
210+
@exception ? @exception.message :
211+
"expected to not include #{description.inspect} in #{@output.inspect}"
212+
end
213+
214+
# Fake $stderr and return a string written to it.
215+
def fake_stderr
216+
original_stderr = $stderr
217+
$stderr = StringIO.new
218+
yield
219+
$stderr.string
220+
rescue RSpec::Expectations::ExpectationNotMetError => e
221+
@exception = e
222+
raise
223+
ensure
224+
$stderr = original_stderr
225+
end
226+
227+
# Fake $stdout and return a string written to it.
228+
def fake_stdout
229+
original_stdout = $stdout
230+
$stdout = StringIO.new
231+
yield
232+
$stdout.string
233+
rescue RSpec::Expectations::ExpectationNotMetError => e
234+
@exception = e
235+
raise
236+
ensure
237+
$stdout = original_stdout
238+
end
239+
240+
# default IO is standard output
241+
def io
242+
@io ||= :output
243+
end
244+
245+
# IO name is used for description message
246+
def io_name
247+
{:output => "standard output", :error => "standard error"}[io]
248+
end
249+
end
182250
end # Matchers
183251
end; end # RDF::Spec

0 commit comments

Comments
 (0)