Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change BaseMatcher to a class.

It was a module, but it was really acting as a base class (it was even
in the name).

Very slightly more efficient (see benchmarks/include_v_superclass.rb)
  • Loading branch information...
commit 49dc4512db8f181cb2a1089eb19be7234b8e07ea 1 parent 4024344
@dchelimsky dchelimsky authored
View
39 benchmarks/include_v_superclass.rb
@@ -0,0 +1,39 @@
+require 'benchmark'
+
+n = 10_000
+
+class Foo; end
+module Bar; end
+
+
+Benchmark.benchmark do |bm|
+ puts "Class.new(Foo)"
+
+ 3.times do
+ bm.report do
+ n.times do
+ Class.new(Foo)
+ end
+ end
+ end
+
+ puts "Class.new { include Bar }"
+
+ 3.times do
+ bm.report do
+ n.times do
+ Class.new { include Bar }
+ end
+ end
+ end
+end
+
+# $ ruby benchmarks/include_v_superclass.rb
+# Class.new(Foo)
+# 0.030000 0.000000 0.030000 ( 0.033536)
+# 0.020000 0.000000 0.020000 ( 0.022077)
+# 0.040000 0.010000 0.050000 ( 0.035813)
+# Class.new { include Bar }
+# 0.040000 0.000000 0.040000 ( 0.041427)
+# 0.040000 0.000000 0.040000 ( 0.039019)
+# 0.030000 0.000000 0.030000 ( 0.037018)
View
2  lib/rspec/matchers/built_in/base_matcher.rb
@@ -11,7 +11,7 @@ module BuiltIn
# This class is for internal use, and subject to change without notice. We
# strongly recommend that you do not base your custom matchers on this
# class. If/when this changes, we will announce it and remove this warning.
- module BaseMatcher
+ class BaseMatcher
include RSpec::Matchers::Pretty
attr_reader :actual, :expected, :rescued_exception
View
16 lib/rspec/matchers/built_in/be.rb
@@ -3,9 +3,7 @@
module RSpec
module Matchers
module BuiltIn
- class BeTrue
- include BaseMatcher
-
+ class BeTrue < BaseMatcher
def match(_, actual)
!!actual
end
@@ -19,9 +17,7 @@ def failure_message_for_should_not
end
end
- class BeFalse
- include BaseMatcher
-
+ class BeFalse < BaseMatcher
def match(_, actual)
!actual
end
@@ -35,9 +31,7 @@ def failure_message_for_should_not
end
end
- class BeNil
- include BaseMatcher
-
+ class BeNil < BaseMatcher
def match(_, actual)
actual.nil?
end
@@ -51,9 +45,7 @@ def failure_message_for_should_not
end
end
- class Be
- include BaseMatcher
-
+ class Be < BaseMatcher
def initialize(*args, &block)
@args = args
end
View
4 lib/rspec/matchers/built_in/be_instance_of.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class BeAnInstanceOf
- include BaseMatcher
-
+ class BeAnInstanceOf < BaseMatcher
def match(expected, actual)
actual.instance_of? expected
end
View
4 lib/rspec/matchers/built_in/be_kind_of.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class BeAKindOf
- include BaseMatcher
-
+ class BeAKindOf < BaseMatcher
def match(expected, actual)
actual.kind_of? expected
end
View
4 lib/rspec/matchers/built_in/cover.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Cover
- include BaseMatcher
-
+ class Cover < BaseMatcher
def initialize(*expected)
@expected = expected
end
View
4 lib/rspec/matchers/built_in/eq.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Eq
- include BaseMatcher
-
+ class Eq < BaseMatcher
def match(expected, actual)
expected == actual
end
View
4 lib/rspec/matchers/built_in/eql.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Eql
- include BaseMatcher
-
+ class Eql < BaseMatcher
def match(expected, actual)
actual.eql? expected
end
View
4 lib/rspec/matchers/built_in/equal.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Equal
- include BaseMatcher
-
+ class Equal < BaseMatcher
def match(expected, actual)
actual.equal? expected
end
View
4 lib/rspec/matchers/built_in/exist.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Exist
- include BaseMatcher
-
+ class Exist < BaseMatcher
def initialize(*expected)
@expected = expected
end
View
4 lib/rspec/matchers/built_in/include.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Include
- include BaseMatcher
-
+ class Include < BaseMatcher
def initialize(*expected)
@expected = expected
end
View
3  lib/rspec/matchers/built_in/match.rb
@@ -1,8 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class Match
- include BaseMatcher
+ class Match < BaseMatcher
def match(expected, actual)
actual.match expected
View
4 lib/rspec/matchers/built_in/match_array.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class MatchArray
- include BaseMatcher
-
+ class MatchArray < BaseMatcher
def match(expected, actual)
@extra_items = difference_between_arrays(actual, expected)
@missing_items = difference_between_arrays(expected, actual)
View
4 lib/rspec/matchers/built_in/start_and_end_with.rb
@@ -1,9 +1,7 @@
module RSpec
module Matchers
module BuiltIn
- class StartAndEndWith
- include BaseMatcher
-
+ class StartAndEndWith < BaseMatcher
def initialize(*expected)
@expected = expected.length == 1 ? expected.first : expected
end
View
7 lib/rspec/matchers/built_in/yield.rb
@@ -64,9 +64,7 @@ def self.assert_valid_expect_block!(block)
end
end
- class YieldControl
- include BaseMatcher
-
+ class YieldControl < BaseMatcher
def matches?(block)
probe = YieldProbe.probe(block)
probe.yielded_once?(:yield_control)
@@ -81,8 +79,7 @@ def failure_message_for_should_not
end
end
- class YieldWithNoArgs
- include BaseMatcher
+ class YieldWithNoArgs < BaseMatcher
def matches?(block)
@probe = YieldProbe.probe(block)
View
8 spec/rspec/matchers/base_matcher_spec.rb
@@ -2,9 +2,7 @@ module RSpec::Matchers::BuiltIn
describe BaseMatcher do
describe "#match_unless_raises" do
let(:matcher) do
- Class.new do
- include BaseMatcher
- end.new
+ Class.new(BaseMatcher).new
end
it "returns true if there are no errors" do
@@ -41,9 +39,7 @@ module RSpec::Matchers::BuiltIn
describe "#==" do
it "responds the same way as matches?" do
- matcher = Class.new do
- include BaseMatcher
-
+ matcher = Class.new(BaseMatcher) do
def initialize(expected)
@expected = expected
end
Please sign in to comment.
Something went wrong with that request. Please try again.