@@ -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
183251end ; end # RDF::Spec
0 commit comments