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
-
#children ⇒ Object
Returns the value of attribute children.
-
#description ⇒ Object
Returns the value of attribute description.
-
#identity ⇒ Object
Returns the value of attribute identity.
- #The description of this context.(descriptionofthiscontext.) ⇒ Object readonly
Class Method Summary collapse
-
.extended(base) ⇒ Object
Called when this module is extended.
Instance Method Summary collapse
-
#add(child) ⇒ Object
Add a child context or test to this context.
-
#after(&hook) ⇒ Object
Include an after hook to the context class, that invokes the given block after running each test.
-
#around(&block) ⇒ Object
Add an around hook to the context class.
-
#before(&hook) ⇒ Object
Include a before hook to the context class, that invokes the given block before running each test.
-
#call(assertions) ⇒ Object
Execute all child contexts and tests.
-
#describe(subject, **options, &block) ⇒ Object
Define a new test group describing a subject.
-
#each(&block) ⇒ Object
Iterate over all leaf nodes (test cases) in this context.
- #empty? ⇒ Boolean
-
#file(path) ⇒ Object
Load a test file as a child context.
- #full_name ⇒ Object
-
#include_context(shared, *arguments, **options) ⇒ Object
Include a shared context into the current context, along with any arguments or options.
- #inspect ⇒ Object
-
#it ⇒ Object
Define a new test case.
-
#it_behaves_like(shared, *arguments, **options, &block) ⇒ Object
Define a test context that behaves like a shared context.
- #leaf? ⇒ Boolean
-
#let(name, &block) ⇒ Object
Define a lazy variable that is evaluated when first accessed.
-
#print(output) ⇒ Object
Print a representation of this context.
-
#set_temporary_name(name) ⇒ Object
Set a temporary name for this context.
- #The child contexts and tests.=(childcontexts) ⇒ Object
- #The identity of this context.=(identityofthiscontext. = (value)) ⇒ Object
- #to_s ⇒ Object
-
#with(subject = nil, unique: true, **variables, &block) ⇒ Object
Define a new test context with specific conditions or variables.
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
19 20 21 |
# File 'lib/sus/context.rb', line 19 def children @children end |
#description ⇒ Object
Returns the value of attribute description.
16 17 18 |
# File 'lib/sus/context.rb', line 16 def description @description end |
#identity ⇒ Object
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.
51 52 53 |
# File 'lib/sus/context.rb', line 51 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.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sus/context.rb', line 124 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.
145 146 147 148 149 150 151 |
# File 'lib/sus/context.rb', line 145 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.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sus/context.rb', line 107 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.
80 81 82 83 84 85 86 87 88 |
# File 'lib/sus/context.rb', line 80 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, **, &block) add Describe.build(self, subject, **, &block) end |
#each(&block) ⇒ Object
Iterate over all leaf nodes (test cases) in this context.
92 93 94 95 96 97 98 99 100 |
# File 'lib/sus/context.rb', line 92 def each(&block) self.children.each do |identity, child| if child.leaf? yield child else child.each(&block) end end end |
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/sus/context.rb', line 56 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_name ⇒ Object
72 73 74 75 76 |
# File 'lib/sus/context.rb', line 72 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, **) self.class_exec(*arguments, **, &shared.block) end |
#inspect ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/sus/context.rb', line 40 def inspect if description = self.description "\#<#{self.name || "Context"} #{self.description}>" else self.name end end |
#it ⇒ Object
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, **, &block) add ItBehavesLike.build(self, shared, arguments, **, &block) end |
#leaf? ⇒ Boolean
61 62 63 |
# File 'lib/sus/context.rb', line 61 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(output) ⇒ Object
Print a representation of this context.
67 68 69 |
# File 'lib/sus/context.rb', line 67 def print(output) output.write("context ", :context, self.description, :reset) end |
#set_temporary_name(name) ⇒ Object
Set a temporary name for this context.
30 31 32 |
# File 'lib/sus/context.rb', line 30 def set_temporary_name(name) # No-op. 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 |
#to_s ⇒ Object
35 36 37 |
# File 'lib/sus/context.rb', line 35 def to_s (self.description || self.name).to_s end |
#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 |