Module: Sus::Context

Included in:
Describe, File, ItBehavesLike, With
Defined in:
lib/sus/context.rb,
lib/sus/it.rb,
lib/sus/let.rb,
lib/sus/file.rb,
lib/sus/with.rb,
lib/sus/describe.rb,
lib/sus/include_context.rb,
lib/sus/it_behaves_like.rb

Overview

Represents a test context that can contain nested tests and other contexts.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



19
20
21
# File 'lib/sus/context.rb', line 19

def children
  @children
end

#descriptionObject

Returns the value of attribute description.



16
17
18
# File 'lib/sus/context.rb', line 16

def description
  @description
end

#identityObject

Returns the value of attribute identity.



13
14
15
# File 'lib/sus/context.rb', line 13

def identity
  @identity
end

#The description of this context.(descriptionofthiscontext.) ⇒ Object (readonly)



16
# File 'lib/sus/context.rb', line 16

attr_accessor :description

Class Method Details

.extended(base) ⇒ Object

Called when this module is extended.



23
24
25
# File 'lib/sus/context.rb', line 23

def self.extended(base)
	base.children = Hash.new
end

Instance Method Details

#add(child) ⇒ Object

Add a child context or test to this context.



29
30
31
# File 'lib/sus/context.rb', line 29

def add(child)
	@children[child.identity] = child
end

#after(&hook) ⇒ Object

Include an after hook to the context class, that invokes the given block after running each test.

After hooks are usually invoked in the reverse order they are defined, i.e. the last defined hook is invoked first.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sus/context.rb', line 102

def after(&hook)
	wrapper = Module.new
	
	wrapper.define_method(:after) do |error|
		instance_exec(error, &hook)
	rescue => error
		raise
	ensure
		super(error)
	end
	
	self.include(wrapper)
end

#around(&block) ⇒ Object

Add an around hook to the context class.

Around hooks are called in the reverse order they are defined.

The top level ‘around` implementation invokes before and after hooks.



123
124
125
126
127
128
129
# File 'lib/sus/context.rb', line 123

def around(&block)
	wrapper = Module.new
	
	wrapper.define_method(:around, &block)
	
	self.include(wrapper)
end

#before(&hook) ⇒ Object

Include a before hook to the context class, that invokes the given block before running each test.

Before hooks are usually invoked in the order they are defined, i.e. the first defined hook is invoked first.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sus/context.rb', line 85

def before(&hook)
	wrapper = Module.new
	
	wrapper.define_method(:before) do
		super()
		
		instance_exec(&hook)
	end
	
	self.include(wrapper)
end

#call(assertions) ⇒ Object

Execute all child contexts and tests.



58
59
60
61
62
63
64
65
66
# File 'lib/sus/context.rb', line 58

def call(assertions)
	return if self.empty?
	
	assertions.nested(self) do |assertions|
		self.children.each do |identity, child|
			child.call(assertions)
		end
	end
end

#describe(subject, **options, &block) ⇒ Object

Define a new test group describing a subject.



55
56
57
# File 'lib/sus/describe.rb', line 55

def describe(subject, **options, &block)
	add Describe.build(self, subject, **options, &block)
end

#each(&block) ⇒ Object

Iterate over all leaf nodes (test cases) in this context.



70
71
72
73
74
75
76
77
78
# File 'lib/sus/context.rb', line 70

def each(&block)
	self.children.each do |identity, child|
		if child.leaf?
			yield child
		else
			child.each(&block)
		end
	end
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sus/context.rb', line 34

def empty?
	@children.nil? || @children.empty?
end

#file(path) ⇒ Object

Load a test file as a child context.



147
148
149
# File 'lib/sus/file.rb', line 147

def file(path)
	add File.build(self, path)
end

#full_nameObject



50
51
52
53
54
# File 'lib/sus/context.rb', line 50

def full_name
	output = Output::Buffered.new
	print(output)
	return output.string
end

#include_context(shared, *arguments, **options) ⇒ Object

Include a shared context into the current context, along with any arguments or options.



15
16
17
# File 'lib/sus/include_context.rb', line 15

def include_context(shared, *arguments, **options)
	self.class_exec(*arguments, **options, &shared.block)
end

#itObject

Define a new test case.



77
78
79
# File 'lib/sus/it.rb', line 77

def it(...)
	add It.build(self, ...)
end

#it_behaves_like(shared, *arguments, **options, &block) ⇒ Object

Define a test context that behaves like a shared context.



54
55
56
# File 'lib/sus/it_behaves_like.rb', line 54

def it_behaves_like(shared, *arguments, **options, &block)
	add ItBehavesLike.build(self, shared, arguments, **options, &block)
end

#leaf?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/sus/context.rb', line 39

def leaf?
	false
end

#let(name, &block) ⇒ Object

Define a lazy variable that is evaluated when first accessed.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sus/let.rb', line 13

def let(name, &block)
	instance_variable = :"@#{name}"
	
	self.define_method(name) do
		if self.instance_variable_defined?(instance_variable)
			return self.instance_variable_get(instance_variable)
		else
			self.instance_variable_set(instance_variable, self.instance_exec(&block))
		end
	end
end

Print a representation of this context.



45
46
47
# File 'lib/sus/context.rb', line 45

def print(output)
	output.write("context ", :context, self.description, :reset)
end

#The child contexts and tests.=(childcontexts) ⇒ Object



19
# File 'lib/sus/context.rb', line 19

attr_accessor :children

#The identity of this context.=(identityofthiscontext. = (value)) ⇒ Object



13
# File 'lib/sus/context.rb', line 13

attr_accessor :identity

#with(subject = nil, unique: true, **variables, &block) ⇒ Object

Define a new test context with specific conditions or variables.



68
69
70
71
72
# File 'lib/sus/with.rb', line 68

def with(subject = nil, unique: true, **variables, &block)
	subject ||= variables.inspect
	
	add With.build(self, subject, variables, unique: unique, &block)
end