Module: Sus::Context

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#identityObject

Returns the value of attribute identity.



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

def identity
  @identity
end

Class Method Details

.extended(base) ⇒ Object



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

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

Instance Method Details

#add(child) ⇒ Object



37
38
39
# File 'lib/sus/context.rb', line 37

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

#after(&hook) ⇒ Object

Include an around method to the context class, that invokes the given block after running the test.

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



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

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.



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

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

#before(&hook) ⇒ Object

Include an around method to the context class, that invokes the given block before running the test.

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



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

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

#call(assertions) ⇒ Object



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

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



41
42
43
# File 'lib/sus/describe.rb', line 41

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

#each(&block) ⇒ Object



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

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)


41
42
43
# File 'lib/sus/context.rb', line 41

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

#file(path) ⇒ Object



109
110
111
# File 'lib/sus/file.rb', line 109

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

#full_nameObject



53
54
55
56
57
# File 'lib/sus/context.rb', line 53

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

#inspectObject



28
29
30
31
32
33
34
# File 'lib/sus/context.rb', line 28

def inspect
	if description = self.description
		"\#<#{self.name || "Context"} #{self.description}>"
	else
		self.name
	end
end

#itObject



56
57
58
# File 'lib/sus/it.rb', line 56

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

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



38
39
40
# File 'lib/sus/it_behaves_like.rb', line 38

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

#leaf?Boolean

Returns:

  • (Boolean)


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

def leaf?
	false
end

#let(name, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sus/let.rb', line 10

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


49
50
51
# File 'lib/sus/context.rb', line 49

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

#set_temporary_name(name) ⇒ Object



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

def set_temporary_name(name)
	# No-op.
end

#to_sObject



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

def to_s
	(self.description || self.name).to_s
end

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



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

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