Sha256: 25775832459b1c6b7775270f7c43c9a47e9f8a3128f75bb4528b2926c0fa54e5

Contents?: true

Size: 1.61 KB

Versions: 107

Compression:

Stored size: 1.61 KB

Contents

 require 'mspec/helpers/tmp'
 require 'fileutils'
 
 # Lower-level output speccing mechanism for a single
 # output stream. Unlike OutputMatcher which provides
 # methods to capture the output, we actually replace
 # the FD itself so that there is no reliance on a
 # certain method being used.
 class OutputToFDMatcher
   def initialize(expected, to)
     @to, @expected = to, expected
 
     case @to
     when STDOUT
       @to_name = "STDOUT"
     when STDERR
       @to_name = "STDERR"
     when IO
       @to_name = @to.object_id.to_s
     else
       raise ArgumentError, "#{@to.inspect} is not a supported output target"
     end
   end
 
   def matches?(block)
     old_to = @to.dup
     out = File.open(tmp("mspec_output_to_#{$$}_#{Time.now.to_i}"), 'w+')
 
     # Replacing with a file handle so that Readline etc. work
     @to.reopen out
 
     block.call
 
   ensure
     begin
       @to.reopen old_to
 
       out.rewind
       @actual = out.read
 
       case @expected
         when Regexp
           return !(@actual =~ @expected).nil?
         else
           return @actual == @expected
       end
 
     # Clean up
     ensure
       out.close unless out.closed?
       FileUtils.rm out.path
     end
 
     return true
   end
 
   def failure_message()
     ["Expected (#{@to_name}): #{@expected.inspect}\n",
      "#{'but got'.rjust(@to_name.length + 10)}: #{@actual.inspect}\nBacktrace"]
   end
 
   def negative_failure_message()
     ["Expected output (#{@to_name}) to NOT be:\n", @actual.inspect]
   end
 end
 
 class Object
   def output_to_fd(what, where = STDOUT)
     OutputToFDMatcher.new what, where
   end
 end

Version data entries

107 entries across 83 versions & 1 rubygems

Version Path
rhodes-3.1.1 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.1.beta lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.0 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.0.beta.5 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.0.beta.4 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.0.beta.3 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.0.beta.2 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.1.0.beta.1 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.2 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.2.beta.1 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.8 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.7 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.6 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.5 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.4 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.3 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.1.beta.2 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.0 lib/extensions/mspec/mspec/matchers/output_to_fd.rb
rhodes-3.0.0.beta.7 lib/extensions/mspec/mspec/matchers/output_to_fd.rb