-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow chained comparisons #21
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,15 +9,17 @@ module ComparisonMatcher | |
# | ||
# @api private | ||
class Matcher | ||
DEFAULT_EXPECTED_COUNT_MATCHERS = { at_least: 1 }.freeze | ||
|
||
def initialize(expected, comparison_type, **options) | ||
check_comparison(comparison_type) | ||
@expected = expected | ||
@comparison_type = comparison_type | ||
@count = 1 | ||
@count_type = :at_least | ||
@time = options.fetch(:time) { 0.2 } | ||
@warmup = options.fetch(:warmup) { 0.1 } | ||
@bench = ::Benchmark::Perf::Iteration | ||
@expected_count_matchers = {} | ||
end | ||
|
||
# Indicates this matcher matches against a block | ||
|
@@ -45,14 +47,7 @@ def matches?(block) | |
|
||
@ratio = @actual_ips / @expected_ips.to_f | ||
|
||
case @count_type | ||
when :at_most | ||
at_most_comparison | ||
when :exactly | ||
exact_comparison | ||
else | ||
default_comparison | ||
end | ||
expected_count_matchers.all? { |type, count| matches_comparison?(type, count) } | ||
end | ||
|
||
# The time before measurements are taken | ||
|
@@ -139,12 +134,11 @@ def failure_message_when_negated | |
|
||
# @api private | ||
def description | ||
if @count == 1 | ||
"perform #{@comparison_type} than comparison block" | ||
else | ||
"perform #{@comparison_type} than comparison block " \ | ||
"by #{@count_type} #{@count} times" | ||
end | ||
main_description = "perform #{@comparison_type} than comparison block" | ||
description_extra = comparison_description | ||
return main_description if !description_extra || description_extra.empty? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [83/80] |
||
|
||
[main_description, comparison_description].compact.join(' ') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
|
||
# @api private | ||
|
@@ -162,6 +156,13 @@ def failure_reason | |
|
||
private | ||
|
||
def comparison_description | ||
expected_count_matchers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/InverseMethods: Use reject instead of inverting select. |
||
.select { |_, count| count != 1 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/DotPosition: Place the . on the previous line, together with the method call receiver. |
||
.map { |type, count| "by #{type} #{count} times" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/DotPosition: Place the . on the previous line, together with the method call receiver. |
||
.join(' and ') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/DotPosition: Place the . on the previous line, together with the method call receiver. |
||
end | ||
|
||
def convert_count(n) | ||
case n | ||
when Numeric then n | ||
|
@@ -175,8 +176,23 @@ def convert_count(n) | |
end | ||
|
||
def set_expected_times_count(type, n) | ||
@count_type = type | ||
@count = convert_count(n) | ||
@expected_count_matchers ||= {} | ||
@expected_count_matchers[type] = convert_count(n) | ||
end | ||
|
||
def expected_count_matchers | ||
@expected_count_matchers.empty? ? DEFAULT_EXPECTED_COUNT_MATCHERS : @expected_count_matchers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [102/80] |
||
end | ||
|
||
def matches_comparison?(count_type, count) | ||
case count_type | ||
when :at_most | ||
at_most_comparison(count) | ||
when :exactly | ||
exact_comparison(count) | ||
else | ||
default_comparison(count) | ||
end | ||
end | ||
|
||
# At most comparison | ||
|
@@ -201,11 +217,11 @@ def set_expected_times_count(type, n) | |
# @return [Boolean] | ||
# | ||
# @api private | ||
def at_most_comparison | ||
def at_most_comparison(count) | ||
if @comparison_type == :faster | ||
1 < @ratio && @ratio < @count | ||
1 < @ratio && @ratio < count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/YodaCondition: Reverse the order of the operands 1 < @ratio. |
||
else | ||
@count**-1 < @ratio && @ratio < 1 | ||
count**-1 < @ratio && @ratio < 1 | ||
end | ||
end | ||
|
||
|
@@ -224,11 +240,11 @@ def at_most_comparison | |
# @return [Boolean] | ||
# | ||
# @api private | ||
def exact_comparison | ||
def exact_comparison(count) | ||
if @comparison_type == :faster | ||
@count == @ratio.round | ||
count == @ratio.round | ||
else | ||
@count == (1.0 / @ratio).round | ||
count == (1.0 / @ratio).round | ||
end | ||
end | ||
|
||
|
@@ -254,11 +270,11 @@ def exact_comparison | |
# @return [Boolean] | ||
# | ||
# @api private | ||
def default_comparison | ||
def default_comparison(count) | ||
if @comparison_type == :faster | ||
@ratio > @count | ||
@ratio > count | ||
else | ||
@ratio < (1.0 / @count) | ||
@ratio < (1.0 / count) | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,28 @@ def clamp_fast(num, min, max) | |
}.to raise_error(/expected given block to perform faster than comparison block by at_most \d+ times, but performed faster by \d+.\d+ times/) | ||
end | ||
end | ||
|
||
context 'with both at_least and at_most count' do | ||
it "passes if the block performance falls in to the range" do | ||
expect { 1 << 1 }.to perform_faster_than { 'x' * 10 * 1024 }.at_least(2).at_most(125) | ||
end | ||
|
||
it "fails if the block performance ratio is higher then indicated by at_least" do | ||
expect { | ||
expect { 1 << 1 } | ||
.to perform_faster_than { 'x' * 10 * 1024 } | ||
.at_least(2).at_most(15).times | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/DotPosition: Place the . on the previous line, together with the method call receiver. |
||
}.to raise_error(/expected given block to perform faster than comparison block by at_least \d+ times and by at_most \d+ times, but performed faster by \d+.\d+ times/) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [174/80] |
||
end | ||
|
||
it "fails if the block performance ratio is lower then indicated by at_most" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [85/80] |
||
expect { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/BlockDelimiters: Avoid using {...} for multi-line blocks. |
||
expect { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/BlockDelimiters: Avoid using {...} for multi-line blocks. |
||
1 << 1 | ||
}.to perform_faster_than { 'x' * 10 * 1024 }.at_least(200).at_most(300).times | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
}.to raise_error(/expected given block to perform faster than comparison block by at_least \d+ times and by at_most \d+ times, but performed faster by \d+.\d+ times/) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [174/80] |
||
end | ||
end | ||
end | ||
|
||
context "expect { ... }.not_to perform_faster_than(...)" do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [89/80]