SusSourceSusHaveValue

class Value

Represents a predicate that checks if a collection has a value matching a predicate.

Definitions

def initialize(predicate)

Initialize a new Value predicate.

Signature

parameter predicate Object, nil

The predicate to apply to each value in the collection.

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("value ", @predicate, :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 (should be enumerable).

Implementation

def call(assertions, subject)
	index = 0
	
	subject.each do |value|
		# Capture the label (index and value) as a buffer of formatting
		# instructions, so the value is truncated/styled consistently:
		label = Output::Buffered.new
		label.write("[#{index}] = ")
		label.variable(value)
		
		assertions.nested(label, distinct: true) do |assertions|
			@predicate&.call(assertions, value)
		end
		
		index += 1
	end
end