Show
Ignore:
Timestamp:
09/01/08 15:24:13 (4 months ago)
Author:
deveiant
Message:
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/spec/lib/matchers.rb

    r86 r87  
    11#!/usr/bin/env ruby 
    22 
    3 require 'bluecloth' 
    4 require 'spec/matchers' 
     3begin 
     4    require 'bluecloth' 
     5    require 'diff/lcs' 
     6    require 'diff/lcs/callbacks' 
     7    require 'spec/lib/constants' 
     8rescue LoadError 
     9    unless Object.const_defined?( :Gem ) 
     10        require 'rubygems' 
     11        retry 
     12    end 
     13    raise 
     14end 
     15 
    516 
    617### Fixturing functions 
    7 module BlueClothMatchers 
     18module BlueCloth::Matchers 
    819 
    920    class TransformMatcher 
     
    1930            @bluecloth = bluecloth 
    2031            @output_html = bluecloth.to_html 
    21             return @output_html == @html 
     32            return @output_html.strip == @html.strip 
    2233        end 
    2334         
    2435        ### Build a failure message for the matching case. 
    2536        def failure_message 
    26             return "Expected the generated html:\n\n  %p\n\nto be the same as:\n\n  %p\n\n" % 
    27                 [ @output_html, @html ] 
     37            patch = self.make_patch( @html, @output_html ) 
     38            return "Expected the generated html:\n\n  %p\n\nto be the same as:\n\n  %p\n\nDiffs:\n\n%s" % 
     39                [ @output_html, @html, patch ] 
    2840        end 
    2941         
     
    3345                [ @output_html, @html ] 
    3446        end 
     47         
     48        ### Compute a patch between the given +expected+ output and the +actual+ output 
     49        ### and return it as a string. 
     50        def make_patch( expected, actual ) 
     51            diffs = Diff::LCS.sdiff( expected.split("\n"), actual.split("\n"), 
     52                Diff::LCS::ContextDiffCallbacks ) 
     53 
     54            maxcol = diffs.flatten. 
     55                collect {|d| [d.old_element.to_s.length, d.new_element.to_s.length ] }. 
     56                flatten.max || 0 
     57            maxcol += 4 
     58 
     59            patch = "              %#{maxcol}s | %s\n" % [ "Expected", "Actual" ] 
     60            patch << diffs.collect do |changeset| 
     61                changeset.collect do |change| 
     62                    "%s [%03d, %03d]: %#{maxcol}s | %-#{maxcol}s" % [ 
     63                        change.action, 
     64                        change.old_position, 
     65                        change.new_position, 
     66                        change.old_element.inspect, 
     67                        change.new_element.inspect, 
     68                    ] 
     69                end.join("\n") 
     70            end.join("\n---\n") 
     71        end 
     72         
    3573    end 
    3674 
     
    87125        end 
    88126         
    89         return BlueClothMatchers::TransformMatcher.new( html ) 
     127        return BlueCloth::Matchers::TransformMatcher.new( html ) 
    90128    end 
    91129     
    92130    ### Generate a matcher that expects to match the given +regexp+. 
    93131    def be_transformed_into_html_matching( regexp ) 
    94         return BlueClothMatchers::TransformMatcher.new( regexp ) 
     132        return BlueCloth::Matchers::TransformMatcher.new( regexp ) 
    95133    end 
    96134     
    97 end 
     135end # module BlueCloth::Matchers 
    98136 
    99