# Code adapted from rs, written by Eero Saynatkari. require 'fileutils' require 'tmpdir' class Test::Spec::Should # Captures output from the IO given as # the second argument (STDIN by default) # and matches it against a String or # Regexp given as the first argument. def output(expected, to = STDOUT) # Store the old stream old_to = to.dup # Instead of using out.path, save here the path we're going to # write to. This is needed because JRuby does not change the class # from IO to File, and thus will lack a path method). out_filename = File.join(Dir.tmpdir, "should_output_#{$$}") # Obtain a filehandle to replace (works with Readline) to.reopen File.open(out_filename, "w+") # Execute @object.call # Restore out = to.dup to.reopen old_to # Grab the data out.rewind output = out.read # Match up case expected when Regexp output.should.match expected else output.should.equal expected end # case expected # Clean up ensure out.close # STDIO redirection will break else begin to.seek 0, IO::SEEK_END rescue Errno::ESPIPE rescue Errno::EPIPE end FileUtils.rm_f out_filename end # output end # Test::Spec::Should