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

#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



10
11
12
# File 'lib/sus/include_context.rb', line 10

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