Class: Sus::BeWithin

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/be_within.rb

Overview

Represents a predicate that checks if the subject is within a tolerance of a value.

Defined Under Namespace

Classes: Bounded

Instance Method Summary collapse

Constructor Details

#initialize(tolerance) ⇒ BeWithin

Initialize a new BeWithin predicate.



35
36
37
# File 'lib/sus/be_within.rb', line 35

def initialize(tolerance)
	@tolerance = tolerance
end

Instance Method Details

#call(assertions, subject) ⇒ Object

Evaluate this predicate against a subject.



66
67
68
69
70
# File 'lib/sus/be_within.rb', line 66

def call(assertions, subject)
	assertions.nested(self) do |assertions|
		assertions.assert(subject < @tolerance, self)
	end
end

#of(value) ⇒ Object

Create a bounded predicate that checks if the subject is within tolerance of a value.



42
43
44
45
46
# File 'lib/sus/be_within.rb', line 42

def of(value)
	tolerance = @tolerance.abs
	
	return Bounded.new(Range.new(value - tolerance, value + tolerance))
end

#percent_of(value) ⇒ Object

Create a bounded predicate that checks if the subject is within a percentage tolerance of a value.



51
52
53
54
55
# File 'lib/sus/be_within.rb', line 51

def percent_of(value)
	tolerance = Rational(@tolerance, 100)
	
	return Bounded.new(Range.new(value - value * tolerance, value + value * tolerance))
end

Print a representation of this predicate.



59
60
61
# File 'lib/sus/be_within.rb', line 59

def print(output)
	output.write("be within ", :variable, @tolerance, :reset)
end