SusSourceSusHaveDuration

class HaveDuration

Represents a predicate that measures the duration of a block execution.

Definitions

def initialize(predicate)

Initialize a new HaveDuration predicate.

Signature

parameter predicate Object

The predicate to apply to the measured duration.

Implementation

def initialize(predicate)
	@predicate = predicate
end

def print(output)

Print a representation of this predicate.

Signature

parameter output Output

The output target.

Implementation

def print(output)
	output.write("have duration ")
	@predicate.print(output)
end

def call(assertions, subject)

Evaluate this predicate against a subject (block).

Signature

parameter assertions Assertions

The assertions instance to use.

parameter subject Proc

The block to measure.

Implementation

def call(assertions, subject)
	assertions.nested(self) do |assertions|
		Expect.new(assertions, measure(subject)).to(@predicate)
	end
end

def measure(subject)

Measure the duration of executing a block.

Signature

parameter subject Proc

The block to measure.

returns Float

The duration in seconds.

Implementation

def measure(subject)
	start_time = now
	
	subject.call
	
	return now - start_time
end

def now

Get the current monotonic time.

Signature

returns Float

The current time in seconds.

Implementation

def now
	::Process.clock_gettime(Process::CLOCK_MONOTONIC)
end