1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
require 'benchmark/ips'
require 'rspec/expectations'
include RSpec::Matchers
module RSpec
module Matchers
module BuiltIn
class OldInclude < BaseMatcher
def initialize(*expected)
@expected = expected
end
def matches?(actual)
@actual = actual
perform_match(:all?, :all?)
end
def does_not_match?(actual)
@actual = actual
perform_match(:none?, :any?)
end
def description
described_items = surface_descriptions_in(expected)
item_list = EnglishPhrasing.list(described_items)
improve_hash_formatting "include#{item_list}"
end
def failure_message
improve_hash_formatting(super) + invalid_object_message
end
def failure_message_when_negated
improve_hash_formatting(super) + invalid_object_message
end
def diffable?
!diff_would_wrongly_highlight_matched_item?
end
private
def invalid_object_message
return '' if actual.respond_to?(:include?)
", but it does not respond to `include?`"
end
def perform_match(predicate, hash_subset_predicate)
return false unless actual.respond_to?(:include?)
expected.__send__(predicate) do |expected_item|
if comparing_hash_to_a_subset?(expected_item)
expected_item.__send__(hash_subset_predicate) do |(key, value)|
actual_hash_includes?(key, value)
end
elsif comparing_hash_keys?(expected_item)
actual_hash_has_key?(expected_item)
else
actual_collection_includes?(expected_item)
end
end
end
def comparing_hash_to_a_subset?(expected_item)
actual.is_a?(Hash) && expected_item.is_a?(Hash)
end
def actual_hash_includes?(expected_key, expected_value)
actual_value = actual.fetch(expected_key) { return false }
values_match?(expected_value, actual_value)
end
def comparing_hash_keys?(expected_item)
actual.is_a?(Hash) && !expected_item.is_a?(Hash)
end
def actual_hash_has_key?(expected_key)
actual.key?(expected_key) ||
actual.keys.any? { |key| values_match?(expected_key, key) }
end
def actual_collection_includes?(expected_item)
return true if actual.include?(expected_item)
return false unless actual.respond_to?(:any?)
actual.any? { |value| values_match?(expected_item, value) }
end
def diff_would_wrongly_highlight_matched_item?
return false unless actual.is_a?(String) && expected.is_a?(Array)
lines = actual.split("\n")
expected.any? do |str|
actual.include?(str) && lines.none? { |line| line == str }
end
end
end
end
def old_include(*expected)
BuiltIn::OldInclude.new(*expected)
end
end
end
array_sizes = [10, 50, 100, 500]
# *maniacal laugh*
class << self; alias_method :inc, :include; remove_method :include; end
Benchmark.ips do |x|
x.report("Old `to include` successes") do
array_sizes.each do |n|
expect([*1..n]).to old_include(*n/2..n)
end
end
x.report("New `to include` successes") do
array_sizes.each do |n|
expect([*1..n]).to include(*n/2..n)
end
end
x.compare!
end
Benchmark.ips do |x|
x.report("Old `to include` failures") do
array_sizes.each do |n|
begin
expect([*1..n]).to old_include(*n+1..n*1.5)
rescue RSpec::Expectations::ExpectationNotMetError
end
end
end
x.report("New `to include` failures") do
array_sizes.each do |n|
begin
expect([*1..n]).to include(*n+1..n*1.5)
rescue RSpec::Expectations::ExpectationNotMetError
end
end
end
x.compare!
end
Benchmark.ips do |x|
x.report("Old `to not include` successes") do
array_sizes.each do |n|
expect([*1..n]).to_not old_include(*n+1..n*1.5)
end
end
x.report("New `to not include` successes") do
array_sizes.each do |n|
expect([*1..n]).to_not include(*n+1..n*1.5)
end
end
x.compare!
end
Benchmark.ips do |x|
x.report("Old `to not include` failures") do
array_sizes.each do |n|
begin
expect([*1..n]).to_not old_include(*n/2..n)
rescue RSpec::Expectations::ExpectationNotMetError
end
end
end
x.report("New `to not include` failures") do
array_sizes.each do |n|
begin
expect([*1..n]).to_not include(*n/2..n)
rescue RSpec::Expectations::ExpectationNotMetError
end
end
end
x.compare!
end
__END__
Calculating -------------------------------------
Old `to include` successes
30.000 i/100ms
New `to include` successes
28.000 i/100ms
-------------------------------------------------
Old `to include` successes
307.740 (± 4.2%) i/s - 1.560k
New `to include` successes
299.321 (± 2.7%) i/s - 1.512k
Comparison:
Old `to include` successes: 307.7 i/s
New `to include` successes: 299.3 i/s - 1.03x slower
Calculating -------------------------------------
Old `to include` failures
2.000 i/100ms
New `to include` failures
1.000 i/100ms
-------------------------------------------------
Old `to include` failures
20.611 (± 4.9%) i/s - 104.000
New `to include` failures
2.990 (± 0.0%) i/s - 15.000
Comparison:
Old `to include` failures: 20.6 i/s
New `to include` failures: 3.0 i/s - 6.89x slower
Calculating -------------------------------------
Old `to not include` successes
1.000 i/100ms
New `to not include` successes
1.000 i/100ms
-------------------------------------------------
Old `to not include` successes
3.505 (± 0.0%) i/s - 18.000
New `to not include` successes
3.475 (± 0.0%) i/s - 18.000
Comparison:
Old `to not include` successes: 3.5 i/s
New `to not include` successes: 3.5 i/s - 1.01x slower
Calculating -------------------------------------
Old `to not include` failures
2.000 i/100ms
New `to include` failures
1.000 i/100ms
-------------------------------------------------
Old `to not include` failures
21.187 (± 4.7%) i/s - 106.000
New `to include` failures
19.899 (± 5.0%) i/s - 100.000
Comparison:
Old `to not include` failures: 21.2 i/s
New `to include` failures: 19.9 i/s - 1.06x slower
|