SusSourceSusAssertions

class Assertions

Nested

Definitions

def initialize(identity: nil, target: nil, output: Output.buffered, inverted: false, orientation: true, isolated: false, distinct: false, measure: false, verbose: false)

Signature

parameter orientation Boolean

Whether the assertions are positive or negative in general.

parameter inverted Boolean

Whether the assertions are inverted with respect to the parent.

Implementation

def initialize(identity: nil, target: nil, output: Output.buffered, inverted: false, orientation: true, isolated: false, distinct: false, measure: false, verbose: false)
	# In theory, the target could carry the identity of the assertion group, but it's not really necessary, so we just handle it explicitly and pass it into any nested assertions.
	@identity = identity
	@target = target
	@output = output
	@inverted = inverted
	@orientation = orientation
	@isolated = isolated
	@distinct = distinct
	@verbose = verbose
	
	if measure
		@clock = Clock.start!
	else
		@clock = nil
	end
	
	@passed = Array.new
	@failed = Array.new
	@deferred = Array.new
	@skipped = Array.new
	@errored = Array.new
	
	@count = 0
end

attr :identity

The identity that is used to identify this set of assertions.

attr :target

The specific target of the assertions, e.g. the test case or nested test assertions.

attr :output

The output buffer used to capture output from the assertions.

attr :level

The nesting level of this set of assertions.

attr :inverted

Whether this aset of assertions is inverted, i.e. the assertions are expected to fail relative to the parent. Used for grouping assertions and ensuring they are added to the parent passed/failed array correctly.

attr :orientation

The absolute orientation of this set of assertions, i.e. whether the assertions are expected to pass or fail regardless of the parent. Used for correctly formatting the output.

attr :isolated

Whether this set of assertions is isolated from the parent. This is used to ensure that any deferred assertions are competed before the parent is completed. This is used by receive assertions which are deferred until the user code of the test has completed.

attr :distinct

Distinct is used to identify a set of assertions as a single statement for the purpose of user feedback. It's used by top level ensure statements to ensure that error messages are captured and reported on those statements.

attr :passed

Nested assertions that have passed.

attr :failed

Nested assertions that have failed.

attr :deferred

Nested assertions have been deferred.

attr :count

The total number of assertions performed:

def defer(&block)

Add deferred assertions.

Implementation

def defer(&block)
	@deferred << block
end

def deferred?

Whether there are any deferred assertions.

Implementation

def deferred?
	@deferred.any?
end

def resolve!

This resolves all deferred assertions in order.

Implementation

def resolve!
	@output.indented do
		while block = @deferred.shift
			block.call(self)
		end
	end
end

def add(assertions)

Add the child assertions which were nested to this instance.

Implementation

def add(assertions)
	# All child assertions should be resolved by this point:
	raise "Nested assertions must be fully resolved!" if assertions.deferred?
	
	if assertions.append?
		# If we are isolated, we merge all child assertions into the parent as a single entity:
		append!(assertions)
	else
		# Otherwise, we append all child assertions into the parent assertions:
		merge!(assertions)
	end
end

def append?

Whether the child assertions should be merged into the parent assertions.

Implementation

def append?
	@isolated || @inverted || @distinct
end

def merge!(assertions)

Concatenate the child assertions into this instance.

Implementation

def merge!(assertions)
	@count += assertions.count
	@passed.concat(assertions.passed)
	@failed.concat(assertions.failed)
	@deferred.concat(assertions.deferred)
	@skipped.concat(assertions.skipped)
	@errored.concat(assertions.errored)
	
	# if @verbose
	# 	@output.write(:indent)
	# 	self.print(@output, verbose: false)
	# 	@output.puts
	# end
end