Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rspec/rspec-expectations
...
head fork: rspec/rspec-expectations
  • 19 commits
  • 12 files changed
  • 0 commit comments
  • 3 contributors
Commits on Mar 17, 2012
@dchelimsky dchelimsky dev: add a script to download ruby-debug-base19-0.11.26.gem and linec…
…ache19-0.5.13.gem to a local cache

[ci skip]
564487b
@dchelimsky dchelimsky Require kernel extensions (should) from block_aliases in case it is
loaded first.
e290fe3
@dchelimsky dchelimsky fix bug diffing single strings with multiline strings 07dd19c
@dchelimsky dchelimsky Revert "fix bug diffing single strings with multiline strings"
This reverts commit 07dd19c.
2c23be3
@dchelimsky dchelimsky better fix for bug when diffing multiline strings e4d8dcb
@dchelimsky dchelimsky formatting tweaks in the changelog 0492d87
Commits on Mar 22, 2012
@justinko justinko Remove unnecessary "else" clause. 584e764
Commits on Mar 23, 2012
@justinko justinko Provide a helpful message if the diff between two objects is empty. F…
…ixes #123
e12a2d9
Commits on Mar 25, 2012
@dchelimsky dchelimsky Changelog for #125 [ci skip] 0458ac1
Commits on Mar 29, 2012
mirasrael Fix error with using custom matchers inside other custom matcher rspe… 77243d9
@dchelimsky dchelimsky Merge pull request #126 from mirasrael/master
Fix for error with using custom matchers inside other custom matchers
6fe6c9f
@dchelimsky dchelimsky changelog for #126 8f822b0
@dchelimsky dchelimsky tweak docstring #126 [ci skip] e5cd722
Commits on Mar 30, 2012
mirasrael Fix using execution context methods in nested DSL matchers
- Fixes #127.
abba439
Commits on Mar 31, 2012
mirasrael Fix RSpec::Matchers::DSL#define for compatibility with 1.8.x 21752df
mirasrael Fix naming 70dd175
Commits on Apr 01, 2012
@justinko justinko Merge pull request #128 from mirasrael/master
Fix using execution context methods in nested DSL matchers
5ffc547
Commits on Apr 02, 2012
@dchelimsky dchelimsky Changelog for #128
[ci skip]
ca5cc09
Commits on Apr 03, 2012
@dchelimsky dchelimsky bump to 2.9.1 b110213
View
13 Changelog.md
@@ -1,3 +1,14 @@
+### 2.9.1 / 2012-04-03
+[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.9.0...2.9.1)
+
+Bug fixes
+
+* Provide a helpful message if the diff between two objects is empty.
+* Fix bug diffing single strings with multiline strings.
+* Fix for error with using custom matchers inside other custom matchers
+ (mirasrael)
+* Fix using execution context methods in nested DSL matchers (mirasrael)
+
### 2.9.0 / 2012-03-17
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0)
@@ -9,7 +20,7 @@ Enhancements
Bug fixes
-* Align respond_to? and method_missing in DSL-defined matchers.
+* Align `respond_to?` and `method_missing` in DSL-defined matchers.
* Clear out user-defined instance variables between invocations of DSL-defined
matchers.
* Dup the instance of a DSL generated matcher so its state is not changed by
View
15 Gemfile-custom.sample
@@ -1,10 +1,10 @@
group :development do
gem 'interactive_rspec'
- gem 'yard'
+ gem 'yard', "~> 0.7.4"
gem "relish", "~> 0.5.0"
gem "guard-rspec", "0.5.0"
gem "growl", "1.0.3"
- gem "spork", "0.9.0.rc9"
+ gem "spork", "0.9.0"
platforms :mri_18, :jruby do
gem "rcov", "0.9.10"
@@ -21,9 +21,16 @@ group :development do
gem 'ruby-debug-base19', '0.11.25'
gem 'linecache19', '0.5.12'
when '1.9.3'
- # NOTE - see http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug
- # for help setting up ruby-debug19 on ruby 1.9.3
gem 'ruby-debug19', '0.11.6'
+ # NOTE - as of 2012-03-17 the following two gems have not been released,
+ # so if you see either of these errors when trying to install the bundle:
+ #
+ # Could not find gem 'ruby-debug-base19 (= 0.11.26) ruby' in the gems available on this machine.
+ # Could not find gem 'linecache19 (= 0.5.13) ruby' in the gems available on this machine.
+ #
+ # ... run 'script/download-ruby-debug-19-dependencies' and try again
+ #
+ # See http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug for more info.
gem 'ruby-debug-base19', '0.11.26'
gem 'linecache19', '0.5.13'
end
View
21 lib/rspec/expectations/differ.rb
@@ -5,15 +5,12 @@
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)
+ output = ""
return output if diffs.empty?
oldhunk = hunk = nil
file_length_difference = 0
@@ -41,10 +38,18 @@ def diff_as_string(data_new, data_old)
output << oldhunk.diff(format) << "\n"
end
- def diff_as_object(actual,expected)
- actual = object_to_string(actual)
- expected = object_to_string(expected)
- diff_as_string(actual, expected)
+ def diff_as_object(actual, expected)
+ actual_as_string = object_to_string(actual)
+ expected_as_string = object_to_string(expected)
+ diff = diff_as_string(actual_as_string, expected_as_string)
+
+ if diff.empty?
+ "#{actual}.==(#{expected}) returned false even though the diff " \
+ "between #{actual} and #{expected} is empty. Check the " \
+ "implementation of #{actual}.==."
+ else
+ diff
+ end
end
protected
View
3  lib/rspec/expectations/fail_with.rb
@@ -5,7 +5,7 @@ class << self
def differ
@differ ||= Differ.new
end
-
+
# Raises an RSpec::Expectations::ExpectationNotMetError with message.
# @param [String] message
# @param [Object] expected
@@ -22,6 +22,7 @@ def fail_with(message, expected=nil, actual=nil)
if actual && expected
if all_strings?(actual, expected)
if any_multiline_strings?(actual, expected)
+ expected = expected.join(',') if Array === expected
message << "\nDiff:" << differ.diff_as_string(actual, expected)
end
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
View
2  lib/rspec/expectations/version.rb
@@ -2,7 +2,7 @@ module RSpec
module Expectations
# @private
module Version
- STRING = '2.9.0'
+ STRING = '2.9.1'
end
end
end
View
1  lib/rspec/matchers/block_aliases.rb
@@ -1,3 +1,4 @@
+require 'rspec/expectations/extensions/kernel'
module RSpec
module Matchers
module BlockAliases
View
7 lib/rspec/matchers/dsl.rb
@@ -4,10 +4,11 @@ module DSL
# Defines a custom matcher.
# @see RSpec::Matchers
def define(name, &declarations)
- matcher = RSpec::Matchers::DSL::Matcher.new(name, &declarations)
+ matcher_template = RSpec::Matchers::DSL::Matcher.new(name, &declarations)
define_method name do |*expected|
- $matcher_execution_context = self
- matcher.for_expected(*expected)
+ matcher = matcher_template.for_expected(*expected)
+ matcher.matcher_execution_context = @matcher_execution_context || self
+ matcher
end
end
View
11 lib/rspec/matchers/matcher.rb
@@ -11,6 +11,7 @@ class Matcher
include RSpec::Matchers
attr_reader :expected, :actual, :rescued_exception
+ attr_accessor :matcher_execution_context
# @api private
def initialize(name, &declarations)
@@ -23,7 +24,7 @@ def initialize(name, &declarations)
@messages = {}
end
- PERSISENT_INSTANCE_VARIABLES = [
+ PERSISTENT_INSTANCE_VARIABLES = [
:@name, :@declarations, :@diffable, :@messages,
:@match_block, :@match_for_should_not_block,
:@expected_exception
@@ -34,7 +35,7 @@ def for_expected(*expected)
@expected = expected
dup.instance_eval do
instance_variables.map {|ivar| ivar.intern}.each do |ivar|
- instance_variable_set(ivar, nil) unless (PERSISENT_INSTANCE_VARIABLES + [:@expected]).include?(ivar)
+ instance_variable_set(ivar, nil) unless (PERSISTENT_INSTANCE_VARIABLES + [:@expected]).include?(ivar)
end
making_declared_methods_public do
instance_eval_with_args(*@expected, &@declarations)
@@ -222,14 +223,14 @@ def does_not_match?(actual)
end
def respond_to?(method, include_private=false)
- $matcher_execution_context.respond_to?(method, include_private) || super
+ super || @matcher_execution_context.respond_to?(method, include_private)
end
private
def method_missing(method, *args, &block)
- if $matcher_execution_context.respond_to?(method)
- $matcher_execution_context.send method, *args, &block
+ if @matcher_execution_context.respond_to?(method)
+ @matcher_execution_context.send method, *args, &block
else
super(method, *args, &block)
end
View
4 script/download-ruby-debug-19-dependencies
@@ -0,0 +1,4 @@
+mkdir -p vendor/cache
+cd vendor/cache
+wget http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
+wget http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
View
159 spec/rspec/expectations/differ_spec.rb
@@ -2,34 +2,15 @@
require 'ostruct'
module RSpec
- module Fixtures
- class Animal
- def initialize(name,species)
- @name,@species = name,species
- end
+ module Expectations
+ describe Differ do
+ let(:differ) { RSpec::Expectations::Differ.new }
- def inspect
- <<-EOA
-<Animal
- name=#{@name},
- species=#{@species}
->
- EOA
- end
- end
- end
-end
-
-describe "Diff" do
- before(:each) do
- @options = OpenStruct.new(:diff_format => :unified, :context_lines => 3)
- @differ = RSpec::Expectations::Differ.new(@options)
- end
-
- it "outputs unified diff of two strings" do
- expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
- actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
- expected_diff= <<'EOD'
+ describe '#diff_as_string' do
+ it "outputs unified diff of two strings" do
+ expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
+ actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
+ expected_diff= <<'EOD'
@@ -1,6 +1,6 @@
@@ -48,15 +29,58 @@ def inspect
line
EOD
- diff = @differ.diff_as_string(expected, actual)
- diff.should eql(expected_diff)
- end
+ diff = differ.diff_as_string(expected, actual)
+ diff.should eql(expected_diff)
+ end
+ end
+
+ describe '#diff_as_object' do
+ it "outputs unified diff message of two objects" do
+ animal_class = Class.new do
+ def initialize(name, species)
+ @name, @species = name, species
+ end
- it "outputs unified diff message of two arrays" do
- expected = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'charlie', :width, 'quite wide' ]
- actual = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'tango' , :width, 'very wide' ]
+ def inspect
+ <<-EOA
+<Animal
+ name=#{@name},
+ species=#{@species}
+>
+ EOA
+ end
+ end
- expected_diff = <<EOD
+ expected = animal_class.new "bob", "giraffe"
+ actual = animal_class.new "bob", "tortoise"
+
+ expected_diff = <<'EOD'
+
+@@ -1,5 +1,5 @@
+ <Animal
+ name=bob,
+- species=tortoise
++ species=giraffe
+ >
+EOD
+
+ diff = differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
+
+ it "outputs a message if the diff is empty" do
+ diff = differ.diff_as_object('foo', 'foo')
+ diff.should eq(
+ "foo.==(foo) returned false even though the diff between " \
+ "foo and foo is empty. Check the implementation of foo.==."
+ )
+ end
+
+ it "outputs unified diff message of two arrays" do
+ expected = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'charlie', :width, 'quite wide' ]
+ actual = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'tango' , :width, 'very wide' ]
+
+ expected_diff = <<'EOD'
@@ -5,7 +5,7 @@
@@ -70,33 +94,15 @@ def inspect
+ "quite wide"]
EOD
- diff = @differ.diff_as_object(expected,actual)
- diff.should == expected_diff
- end
-
- it "outputs 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 +1,5 @@
- <Animal
- name=bob,
-- species=tortoise
-+ species=giraffe
- >
-EOD
-
- diff = @differ.diff_as_object(expected,actual)
- diff.should == expected_diff
- end
+ diff = differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
- it "outputs unified diff message of two hashes" do
- expected = { :foo => 'bar', :baz => 'quux', :metasyntactic => 'variable', :delta => 'charlie', :width =>'quite wide' }
- actual = { :foo => 'bar', :metasyntactic => 'variable', :delta => 'charlotte', :width =>'quite wide' }
+ it "outputs unified diff message of two hashes" do
+ expected = { :foo => 'bar', :baz => 'quux', :metasyntactic => 'variable', :delta => 'charlie', :width =>'quite wide' }
+ actual = { :foo => 'bar', :metasyntactic => 'variable', :delta => 'charlotte', :width =>'quite wide' }
- expected_diff = <<'EOD'
+ expected_diff = <<'EOD'
@@ -1,4 +1,5 @@
-:delta => "charlotte",
@@ -107,30 +113,30 @@ def inspect
:width => "quite wide"
EOD
- diff = @differ.diff_as_object(expected,actual)
- diff.should == expected_diff
- end
+ diff = differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
- it "outputs unified diff of single line strings" do
- expected = "this is one string"
- actual = "this is another string"
+ it "outputs unified diff of single line strings" do
+ expected = "this is one string"
+ actual = "this is another string"
- expected_diff = <<EOD
+ expected_diff = <<'EOD'
@@ -1,2 +1,2 @@
-"this is another string"
+"this is one string"
EOD
- diff = @differ.diff_as_object(expected,actual)
- diff.should == expected_diff
- end
+ diff = differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
- it "outputs unified diff of multi line strings" do
- expected = "this is:\n one string"
- actual = "this is:\n another string"
+ it "outputs unified diff of multi line strings" do
+ expected = "this is:\n one string"
+ actual = "this is:\n another string"
- expected_diff = <<EOD
+ expected_diff = <<'EOD'
@@ -1,3 +1,3 @@
this is:
@@ -138,7 +144,10 @@ def inspect
+ one string
EOD
- diff = @differ.diff_as_object(expected,actual)
- diff.should == expected_diff
+ diff = differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
+ end
+ end
end
end
View
16 spec/rspec/matchers/dsl_spec.rb
@@ -5,6 +5,22 @@ def question?
:answer
end
+ def ok
+ "ok"
+ end
+
+ it "supports calling custom matchers from within other custom matchers" do
+ RSpec::Matchers.define :be_ok do
+ match { |actual| actual == ok }
+ end
+
+ RSpec::Matchers.define :be_well do
+ match { |actual| actual.should be_ok }
+ end
+
+ ok.should be_well
+ end
+
it "has access to methods available in the scope of the example" do
RSpec::Matchers::define(:ignore) {}
ignore.question?.should eq(:answer)
View
12 spec/rspec/matchers/include_spec.rb
@@ -16,6 +16,18 @@
"abc".should include("d")
}.should fail_matching("expected \"abc\" to include \"d\"")
end
+
+ it "includes a diff when actual is multiline" do
+ lambda {
+ "abc\ndef".should include("g")
+ }.should fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff")
+ end
+
+ it "includes a diff when actual is multiline and there are multiple expecteds" do
+ lambda {
+ "abc\ndef".should include("g", "h")
+ }.should fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff")
+ end
end
context "for an array target" do

No commit comments for this range

Something went wrong with that request. Please try again.