Skip to content

Commit

Permalink
just diff
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed May 19, 2010
1 parent ba95bb1 commit 6a6d4c8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 154 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -13,6 +13,7 @@ begin
gem.email = "dchelimsky@gmail.com;chad.humphries@gmail.com"
gem.homepage = "http://github.com/rspec/expectations"
gem.authors = ["David Chelimsky", "Chad Humphries"]
gem.add_dependency('diff-lcs', ">= 1.1.2")
gem.add_development_dependency('cucumber', ">= 0.6.2")
gem.add_development_dependency('aruba', ">= 0.1.1")
gem.add_development_dependency('rspec-core', ">= #{RSpec::Expectations::Version::STRING}")
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/expectations.rb
Expand Up @@ -5,6 +5,7 @@
require 'rspec/expectations/handler'
require 'rspec/expectations/version'
require 'rspec/expectations/backward_compatibility'
require 'rspec/expectations/differ'

module RSpec

Expand Down
4 changes: 4 additions & 0 deletions lib/rspec/expectations/backward_compatibility.rb
Expand Up @@ -6,6 +6,10 @@ def const_missing(name)
name == :Rspec ? RSpec : super(name)
end
end

def differ=(ignore)
RSpec.deprecate("RSpec::Expectations.differ=(differ)", "nothing at all (diffing is now automatic and no longer configurable)")
end
end
end

Expand Down
58 changes: 58 additions & 0 deletions lib/rspec/expectations/differ.rb
@@ -0,0 +1,58 @@
require 'diff/lcs'
require 'diff/lcs/hunk'
require 'pp'

module RSpec
module Expectations
class Differ
def initialize(ignore=nil)
end

# This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
def diff_as_string(data_new, data_old)
data_old = data_old.split(/\n/).map! { |e| e.chomp }
data_new = data_new.split(/\n/).map! { |e| e.chomp }
output = ""
diffs = Diff::LCS.diff(data_old, data_new)
return output if diffs.empty?
oldhunk = hunk = nil
file_length_difference = 0
diffs.each do |piece|
begin
hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
file_length_difference)
file_length_difference = hunk.file_length_difference
next unless oldhunk
# Hunks may overlap, which is why we need to be careful when our
# diff includes lines of context. Otherwise, we might print
# redundant lines.
if (context_lines > 0) and hunk.overlaps?(oldhunk)
hunk.unshift(oldhunk)
else
output << oldhunk.diff(format)
end
ensure
oldhunk = hunk
output << "\n"
end
end
#Handle the last remaining hunk
output << oldhunk.diff(format) << "\n"
end

def diff_as_object(target,expected)
diff_as_string(PP.pp(target,""), PP.pp(expected,""))
end

protected
def format
:unified
end

def context_lines
3
end
end

end
end
62 changes: 0 additions & 62 deletions lib/rspec/expectations/differs/default.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/rspec/expectations/differs/load-diff-lcs.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/rspec/expectations/fail_with.rb
@@ -1,7 +1,9 @@
module RSpec
module Expectations
class << self
attr_accessor :differ
def differ
@differ ||= Differ.new
end

# raises a RSpec::Expectations::ExpectationNotMetError with message
#
Expand Down
12 changes: 7 additions & 5 deletions rspec-expectations.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["David Chelimsky", "Chad Humphries"]
s.date = %q{2010-05-16}
s.date = %q{2010-05-19}
s.description = %q{rspec expectations (should[_not] and matchers)}
s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
s.extra_rdoc_files = [
Expand All @@ -36,8 +36,7 @@ Gem::Specification.new do |s|
"features/support/env.rb",
"lib/rspec/expectations.rb",
"lib/rspec/expectations/backward_compatibility.rb",
"lib/rspec/expectations/differs/default.rb",
"lib/rspec/expectations/differs/load-diff-lcs.rb",
"lib/rspec/expectations/differ.rb",
"lib/rspec/expectations/errors.rb",
"lib/rspec/expectations/extensions.rb",
"lib/rspec/expectations/extensions/kernel.rb",
Expand Down Expand Up @@ -74,7 +73,7 @@ Gem::Specification.new do |s|
"lib/rspec/matchers/satisfy.rb",
"lib/rspec/matchers/throw_symbol.rb",
"rspec-expectations.gemspec",
"spec/rspec/expectations/differs/default_spec.rb",
"spec/rspec/expectations/differ_spec.rb",
"spec/rspec/expectations/extensions/kernel_spec.rb",
"spec/rspec/expectations/fail_with_spec.rb",
"spec/rspec/expectations/handler_spec.rb",
Expand Down Expand Up @@ -124,7 +123,7 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.6}
s.summary = %q{rspec-expectations-2.0.0.beta.8}
s.test_files = [
"spec/rspec/expectations/differs/default_spec.rb",
"spec/rspec/expectations/differ_spec.rb",
"spec/rspec/expectations/extensions/kernel_spec.rb",
"spec/rspec/expectations/fail_with_spec.rb",
"spec/rspec/expectations/handler_spec.rb",
Expand Down Expand Up @@ -162,17 +161,20 @@ Gem::Specification.new do |s|
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<diff-lcs>, [">= 1.1.2"])
s.add_development_dependency(%q<cucumber>, [">= 0.6.2"])
s.add_development_dependency(%q<aruba>, [">= 0.1.1"])
s.add_development_dependency(%q<rspec-core>, [">= 2.0.0.beta.8"])
s.add_development_dependency(%q<rspec-mocks>, [">= 2.0.0.beta.8"])
else
s.add_dependency(%q<diff-lcs>, [">= 1.1.2"])
s.add_dependency(%q<cucumber>, [">= 0.6.2"])
s.add_dependency(%q<aruba>, [">= 0.1.1"])
s.add_dependency(%q<rspec-core>, [">= 2.0.0.beta.8"])
s.add_dependency(%q<rspec-mocks>, [">= 2.0.0.beta.8"])
end
else
s.add_dependency(%q<diff-lcs>, [">= 1.1.2"])
s.add_dependency(%q<cucumber>, [">= 0.6.2"])
s.add_dependency(%q<aruba>, [">= 0.1.1"])
s.add_dependency(%q<rspec-core>, [">= 2.0.0.beta.8"])
Expand Down
Expand Up @@ -23,7 +23,7 @@ def inspect
describe "Diff" do
before(:each) do
@options = OpenStruct.new(:diff_format => :unified, :context_lines => 3)
@differ = RSpec::Expectations::Differs::Default.new(@options)
@differ = RSpec::Expectations::Differ.new(@options)
end

it "should output unified diff of two strings" do
Expand Down Expand Up @@ -94,35 +94,3 @@ def inspect
end

end


describe "Diff in context format" do
before(:each) do
@options = OpenStruct.new(:diff_format => :unified, :context_lines => 3)
@options.diff_format = :context
@differ = RSpec::Expectations::Differs::Default.new(@options)
end

it "should output unified diff message of two objects" do
expected = RSpec::Fixtures::Animal.new "bob", "giraffe"
actual = RSpec::Fixtures::Animal.new "bob", "tortoise"

expected_diff = <<'EOD'
***************
*** 1,5 ****
<Animal
name=bob,
! species=tortoise
>
--- 1,5 ----
<Animal
name=bob,
! species=giraffe
>
EOD

diff = @differ.diff_as_object(expected,actual)
diff.should == expected_diff
end
end
46 changes: 1 addition & 45 deletions spec/rspec/expectations/fail_with_spec.rb
@@ -1,33 +1,9 @@
require 'spec_helper'

describe RSpec::Expectations, "#fail_with with no diff" do
before(:each) do
@old_differ = RSpec::Expectations.differ
RSpec::Expectations.differ = nil
end

it "should handle just a message" do
lambda {
RSpec::Expectations.fail_with "the message"
}.should fail_with("the message")
end

after(:each) do
RSpec::Expectations.differ = @old_differ
end
end

describe RSpec::Expectations, "#fail_with with Array" do
before(:each) do
RSpec::Core.stub!(:warn)
end
end

describe RSpec::Expectations, "#fail_with with diff" do
before(:each) do
@old_differ = RSpec::Expectations.differ
@differ = mock("differ")
RSpec::Expectations.differ = @differ
RSpec::Expectations.stub(:differ) { @differ }
end

it "should not call differ if no expected/actual" do
Expand Down Expand Up @@ -57,25 +33,5 @@
RSpec::Expectations.fail_with "the message", lambda {}, lambda {}
}.should fail_with("the message")
end

after(:each) do
RSpec::Expectations.differ = @old_differ
end
end

describe RSpec::Expectations, "#fail_with with a nil message" do
before(:each) do
@old_differ = RSpec::Expectations.differ
RSpec::Expectations.differ = nil
end

it "should handle just a message" do
lambda {
RSpec::Expectations.fail_with nil
}.should raise_error(ArgumentError, /Failure message is nil\. Does your matcher define the appropriate failure_message_for_\* method to return a string\?/)
end

after(:each) do
RSpec::Expectations.differ = @old_differ
end
end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -13,7 +13,6 @@ def add_to_load_path(path, prepend=false)

require 'rspec/core'
require 'rspec/mocks'
require 'rspec/expectations/differs/default'

Dir['./spec/support/**/*'].each do |f|
require f
Expand Down

0 comments on commit 6a6d4c8

Please sign in to comment.