SusSourceSusWith

module With

Represents a test context with specific conditions or variables.

Definitions

attr_accessor :subject

Signature

attribute String

The subject description of this context.

attr_accessor :variables

Signature

attribute Hash

The variables available in this context.

def self.build(parent, subject, variables, unique: true, &block)

Build a new with block class.

Signature

parameter parent Class

The parent context class.

parameter subject String

The subject description.

parameter variables Hash

Variables to make available in the context.

parameter unique Boolean

Whether the identity should be unique.

yields {...}

Optional block containing nested tests.

returns Class

A new with block class.

Implementation

def self.build(parent, subject, variables, unique: true, &block)
	base = Class.new(parent)
	base.singleton_class.prepend(With)
	base.children = Hash.new
	base.subject = subject
	base.description = subject
	base.identity = Identity.nested(parent.identity, base.description, unique: unique)
	base.set_temporary_name("#{self}[#{base.description}]")
	
	base.variables = variables
	
	base.define_method(:description, ->{subject})
	
	variables.each do |key, value|
		base.define_method(key, ->{value})
	end
	
	if block_given?
		base.class_exec(&block)
	end
	
	return base
end

def print(output)

Print a representation of this with block.

Signature

parameter output Output

The output target.

Implementation

def print(output)
	self.superclass.print(output)
	
	output.write(
		" with ", :with, self.description, :reset,
		# " ", :variables, self.variables.inspect
	)
end