From 17325de1a59f61fbf686b372a4297627f3fd97be Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Sat, 28 Jan 2012 09:53:01 -0600 Subject: [PATCH] Remove relative links from README, since they can't work correctly on github and rubydoc.info. - Fixes #113. --- README.md | 15 ++--------- .../default_messages_as_methods_v_blocks.rb | 27 +++++++++++++++++++ lib/rspec/matchers/matcher.rb | 25 ++++++++++++----- 3 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 benchmarks/default_messages_as_methods_v_blocks.rb diff --git a/README.md b/README.md index e355f7ca5..29d347e23 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # RSpec Expectations -[RSpec::Expectations](../RSpec/Expectations) lets you express expected outcomes -on an object in an example. +RSpec::Expectations lets you express expected outcomes on an object in an +example. account.balance.should eq(Money.new(37.42, :USD)) @@ -119,8 +119,6 @@ actual.should be_xxx # passes if actual.xxx? actual.should have_xxx(:arg) # passes if actual.has_xxx?(:arg) ``` -See [RSpec::Matchers](../RSpec/Matchers) for more about predicate matchers. - ### Ranges (Ruby >= 1.9 only) ```ruby @@ -142,15 +140,6 @@ actual.should include(expected) "this string".should include("is str") ``` -## Learn more - -See [RSpec::Expectations](../RSpec/Expectations) for more information about -`should` and `should_not` and how they work. - -See [RSpec::Matchers](../RSpec/Matchers) for more information about the -built-in matchers that ship with rspec-expectations, and how to write your own -custom matchers. - ## Also see * [http://github.com/rspec/rspec](http://github.com/rspec/rspec) diff --git a/benchmarks/default_messages_as_methods_v_blocks.rb b/benchmarks/default_messages_as_methods_v_blocks.rb new file mode 100644 index 000000000..4e5d182b6 --- /dev/null +++ b/benchmarks/default_messages_as_methods_v_blocks.rb @@ -0,0 +1,27 @@ +require 'benchmark' +require 'rspec/expectations' + +include RSpec::Expectations +include RSpec::Matchers + +RSpec::Matchers.define :eq_using_dsl do |expected| + match do |actual| + actual == expected + end +end + +n = 10000 + +Benchmark.benchmark do |bm| + 3.times do + bm.report do + n.times do + eq_using_dsl(5).tap do |m| + m.description + m.failure_message_for_should + m.failure_message_for_should_not + end + end + end + end +end diff --git a/lib/rspec/matchers/matcher.rb b/lib/rspec/matchers/matcher.rb index d59ede9c7..a3cc13979 100644 --- a/lib/rspec/matchers/matcher.rb +++ b/lib/rspec/matchers/matcher.rb @@ -20,12 +20,7 @@ def initialize(name, &declarations) @diffable = false @expected_exception, @rescued_exception = nil, nil @match_for_should_not_block = nil - - @messages = { - :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"}, - :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"}, - :failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"} - } + @messages = {} end PERSISENT_INSTANCE_VARIABLES = [ @@ -273,7 +268,23 @@ def cache(key, &block) end def call_cached(key) - @messages[key].arity == 1 ? @messages[key].call(@actual) : @messages[key].call + if @messages.has_key?(key) + @messages[key].arity == 1 ? @messages[key].call(@actual) : @messages[key].call + else + send("default_#{key}") + end + end + + def default_description + "#{name_to_sentence}#{expected_to_sentence}" + end + + def default_failure_message_for_should + "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}" + end + + def default_failure_message_for_should_not + "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}" end unless method_defined?(:singleton_class)