Changeset 87 for trunk/spec/lib/matchers.rb
- Timestamp:
- 09/01/08 15:24:13 (4 months ago)
- Files:
-
- 1 modified
-
trunk/spec/lib/matchers.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/spec/lib/matchers.rb
r86 r87 1 1 #!/usr/bin/env ruby 2 2 3 require 'bluecloth' 4 require 'spec/matchers' 3 begin 4 require 'bluecloth' 5 require 'diff/lcs' 6 require 'diff/lcs/callbacks' 7 require 'spec/lib/constants' 8 rescue LoadError 9 unless Object.const_defined?( :Gem ) 10 require 'rubygems' 11 retry 12 end 13 raise 14 end 15 5 16 6 17 ### Fixturing functions 7 module BlueCloth Matchers18 module BlueCloth::Matchers 8 19 9 20 class TransformMatcher … … 19 30 @bluecloth = bluecloth 20 31 @output_html = bluecloth.to_html 21 return @output_html == @html32 return @output_html.strip == @html.strip 22 33 end 23 34 24 35 ### Build a failure message for the matching case. 25 36 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 ] 28 40 end 29 41 … … 33 45 [ @output_html, @html ] 34 46 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 35 73 end 36 74 … … 87 125 end 88 126 89 return BlueCloth Matchers::TransformMatcher.new( html )127 return BlueCloth::Matchers::TransformMatcher.new( html ) 90 128 end 91 129 92 130 ### Generate a matcher that expects to match the given +regexp+. 93 131 def be_transformed_into_html_matching( regexp ) 94 return BlueCloth Matchers::TransformMatcher.new( regexp )132 return BlueCloth::Matchers::TransformMatcher.new( regexp ) 95 133 end 96 134 97 end 135 end # module BlueCloth::Matchers 98 136 99
