~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/rspec/lib/spec/expectations/differs/default.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mfrom: (1.1.8 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080726154345-c03m49twzxewdwjn
Tags: 0.24.5-2
* Fix puppetlast to work with 0.24.5
* Adjust logcheck to match against new log messages in 0.24.5
* Update standards version to 3.8.0 (no changes)
* Update changelog to reduce length of line to make lintian happy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
begin
 
2
  require 'rubygems'
 
3
  require 'diff/lcs' #necessary due to loading bug on some machines - not sure why - DaC
 
4
  require 'diff/lcs/hunk'
 
5
rescue LoadError ; raise "You must gem install diff-lcs to use diffing" ; end
 
6
 
 
7
require 'pp'
 
8
 
 
9
module Spec
 
10
  module Expectations
 
11
    module Differs
 
12
 
 
13
      # TODO add some rdoc
 
14
      class Default
 
15
        def initialize(options)
 
16
          @options = options
 
17
        end
 
18
 
 
19
        # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
 
20
        def diff_as_string(data_old, data_new)
 
21
          data_old = data_old.split(/\n/).map! { |e| e.chomp }
 
22
          data_new = data_new.split(/\n/).map! { |e| e.chomp }
 
23
          output = ""
 
24
          diffs = Diff::LCS.diff(data_old, data_new)
 
25
          return output if diffs.empty?
 
26
          oldhunk = hunk = nil  
 
27
          file_length_difference = 0
 
28
          diffs.each do |piece|
 
29
            begin
 
30
              hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
 
31
                                         file_length_difference)
 
32
              file_length_difference = hunk.file_length_difference      
 
33
              next unless oldhunk      
 
34
              # Hunks may overlap, which is why we need to be careful when our
 
35
              # diff includes lines of context. Otherwise, we might print
 
36
              # redundant lines.
 
37
              if (context_lines > 0) and hunk.overlaps?(oldhunk)
 
38
                hunk.unshift(oldhunk)
 
39
              else
 
40
                output << oldhunk.diff(format)
 
41
              end
 
42
            ensure
 
43
              oldhunk = hunk
 
44
              output << "\n"
 
45
            end
 
46
          end  
 
47
          #Handle the last remaining hunk
 
48
          output << oldhunk.diff(format) << "\n"
 
49
        end  
 
50
 
 
51
        def diff_as_object(target,expected)
 
52
          diff_as_string(PP.pp(target,""), PP.pp(expected,""))
 
53
        end
 
54
 
 
55
        protected
 
56
        def format
 
57
          @options.diff_format
 
58
        end
 
59
 
 
60
        def context_lines
 
61
          @options.context_lines
 
62
        end
 
63
      end
 
64
    end
 
65
  end
 
66
end