Class: RSpec::Matchers::BuiltIn::BeWithin

Inherits:
Object
  • Object
show all
Defined in:
lib/nmatrix/rspec.rb

Instance Method Summary collapse

Instance Method Details

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nmatrix/rspec.rb', line 60

def matches?(actual)
  @actual = actual
  raise needs_expected     unless defined? @expected
  raise needs_subtractable unless @actual.respond_to? :-
  res = (@actual - @expected).abs <= @tolerance

  #if res.is_a?(NMatrix)
  #  require 'pry'
  #  binding.pry
  #end

  res.is_a?(NMatrix) ? !res.any? { |x| !x } : res
end

#of(expected) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nmatrix/rspec.rb', line 35

def of(expected)
  @expected = expected
  @unit     = ''
  if expected.is_a?(NMatrix)
    @tolerance = if @delta.is_a?(NMatrix)
                   @delta.abs
                 elsif @delta.is_a?(Array)
                   NMatrix.new(:dense, expected.shape, @delta, :object).abs.cast(:dtype => expected.abs_dtype)
                 else
                   (NMatrix.ones_like(expected) * @delta).abs
                 end
  else
    @tolerance = @delta
  end

  self
end

#percent_of(expected) ⇒ Object



53
54
55
56
57
58
# File 'lib/nmatrix/rspec.rb', line 53

def percent_of(expected)
  @expected  = expected
  @unit      = '%'
  @tolerance = @expected.abs * @delta / 100.0 # <- only change is to reverse abs and @delta
  self
end