class RunForComparedTo

Attributes

actual[R]
config[R]
expected[R]
operator[R]

Public Class Methods

new(operator, expected) click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 14
def initialize(operator, expected)
  @operator, @expected = operator, expected.to_f

  @config = {
    :average_over => 10.times,
    :drop_first => 1,
  }
end

Public Instance Methods

description() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 49
def description
  "run for #{operator} #{expected_str}"
end
failure_message() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 41
def failure_message
  failure_message_for_operator(operator)
end
failure_message_when_negated() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 45
def failure_message_when_negated
  failure_message_for_operator(opposite_operator)
end
matches?(block) click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 23
def matches?(block)
  raise ArgumentError, 'Expecting a block to `expect`' if !block.is_a?(Proc)

  begin
    warmup_runs.each { block.call }
    benchmark = Benchmark.measure { runs.each { block.call } }

    @actual = benchmark.total / runs.count
    @actual.in_milliseconds.send(operator, expected.in_milliseconds)
  rescue ArgumentError
    false
  end
end
supports_block_expectations?() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 37
def supports_block_expectations?
  true
end

Private Instance Methods

actual_str() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 76
def actual_str
  "#{actual.in_milliseconds} ms"
end
expected_str() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 72
def expected_str
  "#{expected.in_milliseconds} ms"
end
failure_message_for_operator(operator) click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 63
def failure_message_for_operator(operator)
  "expected: #{operator} #{expected_str}\n" \
  "     got: #{operator.to_s.gsub(/./, ' ')} #{actual_str}"
end
opposite_operator() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 68
def opposite_operator
  { :< => :>=, :<= => :>, :> => :<=, :>= => :< }[operator]
end
runs() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 55
def runs
  config[:average_over]
end
warmup_runs() click to toggle source
# File lib/rspec/stopwatch/matchers/run_for_matcher.rb, line 59
def warmup_runs
  config[:drop_first].times
end