SusSourceSusBeWithin

class BeWithin

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

Nested

Definitions

def initialize(tolerance)

Initialize a new BeWithin predicate.

Signature

parameter tolerance Numeric

The tolerance value.

Implementation

def initialize(tolerance)
	@tolerance = tolerance
end

def of(value)

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

Signature

parameter value Numeric

The value to check against.

returns Bounded

A new Bounded predicate.

Implementation

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

def percent_of(value)

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

Signature

parameter value Numeric

The value to check against.

returns Bounded

A new Bounded predicate.

Implementation

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

def print(output)

Print a representation of this predicate.

Signature

parameter output Output

The output target.

Implementation

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

def call(assertions, subject)

Evaluate this predicate against a subject.

Signature

parameter assertions Assertions

The assertions instance to use.

parameter subject Object

The subject to evaluate.

Implementation

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