Class: Riot::Context
- Includes:
- ContextClassOverrides, ContextOptions
- Defined in:
- lib/riot/context.rb
Overview
You make your assertions within a Context. The context stores setup and teardown blocks, and allows for nesting and refactoring into helpers. Extension developers may also configure ContextMiddleware objects in order to extend the functionality of a Context.
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
The description of the context.
-
#parent ⇒ Riot::Context
readonly
The parent context.
Class Method Summary collapse
-
.middlewares ⇒ Array
The set of middleware helpers configured for the current test space.
Instance Method Summary collapse
-
#asserts(what, &definition) ⇒ Object
Makes an assertion.
-
#asserts_topic(what = "that it") ⇒ Object
Makes an assertion on the topic itself, e.g.
-
#context(description, &definition) ⇒ Object
(also: #describe)
Create a new test context.
-
#detailed_description ⇒ Object
Prints the full description from the context tree.
-
#helper(name, &block) ⇒ Object
Helpers are essentially methods accessible within a situation.
-
#hookup(&definition) ⇒ Object
A setup shortcut that returns the original topic so you don’t have to.
-
#initialize(description, parent = nil, &definition) ⇒ Context
constructor
A new instance of Context.
- #local_run(reporter, situation) ⇒ Object
- #run(reporter) ⇒ Object
-
#setup(premium = false, &definition) ⇒ Object
Add a setup block.
-
#setups ⇒ Array[Riot::RunnableBlock]
Returns an ordered list of the setup blocks for the context.
-
#should(what, &definition) ⇒ Object
Same as #asserts, except it uses the phrase “should” in the report output.
-
#teardown(&definition) ⇒ Object
Add a teardown block.
-
#teardowns ⇒ Array[Riot::RunnableBlock]
Returns an ordered list of the teardown blocks for the context.
Methods included from ContextOptions
Methods included from ContextClassOverrides
#assertion_class, #situation_class
Constructor Details
#initialize(description, parent = nil, &definition) ⇒ Context
Returns a new instance of Context.
52 53 54 55 56 57 58 |
# File 'lib/riot/context.rb', line 52 def initialize(description, parent=nil, &definition) @parent = parent || RootContext.new([],[], "", {}) @description = description @contexts, @setups, @assertions, @teardowns = [], [], [], [] @options = @parent. prepare_middleware(&definition) end |
Instance Attribute Details
#description ⇒ String (readonly)
The description of the context.
45 46 47 |
# File 'lib/riot/context.rb', line 45 def description @description end |
#parent ⇒ Riot::Context (readonly)
The parent context.
50 51 52 |
# File 'lib/riot/context.rb', line 50 def parent @parent end |
Class Method Details
.middlewares ⇒ Array
The set of middleware helpers configured for the current test space.
40 |
# File 'lib/riot/context.rb', line 40 def self.middlewares; @middlewares ||= []; end |
Instance Method Details
#asserts(what, &definition) ⇒ Object
Makes an assertion.
In the most basic form, an assertion requires a descriptive name and a block.
asserts("#size is equals to 2") { topic.size == 2 }
However, several shortcuts are available. Assertion macros can be added to the end, automating a number of common assertion patterns, e.g.
asserts("#size") { topic.size }.equals(2)
Furthermore, the pattern of testing an attribute on the topic is codified as
asserts(:size).equals(2)
Passing a Symbol to asserts
enables this behaviour. For more information on assertion macros, see AssertionMacro.
155 156 157 |
# File 'lib/riot/context.rb', line 155 def asserts(what, &definition) new_assertion("asserts", what, &definition) end |
#asserts_topic(what = "that it") ⇒ Object
Makes an assertion on the topic itself, e.g.
asserts_topic.matches(/^ab+/)
172 173 174 |
# File 'lib/riot/context.rb', line 172 def asserts_topic(what="that it") asserts(what) { topic } end |
#context(description, &definition) ⇒ Object Also known as: describe
Create a new test context.
63 64 65 |
# File 'lib/riot/context.rb', line 63 def context(description, &definition) new_context(description, self.class, &definition) end |
#detailed_description ⇒ Object
Prints the full description from the context tree
188 189 190 |
# File 'lib/riot/context.rb', line 188 def detailed_description "#{parent.detailed_description} #{description}".strip end |
#helper(name, &block) ⇒ Object
Helpers are essentially methods accessible within a situation.
They’re not setup methods, but can be called from setups or from assetions. Each time called, the helper will be evaluated. It’s not currently memoized.
context "A string" do
helper(:foo) { "bar" }
asserts("a foo") { foo }.equals("bar")
end
110 111 112 |
# File 'lib/riot/context.rb', line 110 def helper(name, &block) (@setups << Helper.new(name, &block)).last end |
#hookup(&definition) ⇒ Object
A setup shortcut that returns the original topic so you don’t have to. Good for nested setups. Instead of doing this in your context:
setup do
topic.do_something
topic
end
You would do this:
hookup { topic.do_something } # Yay!
125 126 127 |
# File 'lib/riot/context.rb', line 125 def hookup(&definition) setup { self.instance_eval(&definition); topic } end |
#local_run(reporter, situation) ⇒ Object
183 184 185 |
# File 'lib/riot/context.rb', line 183 def local_run(reporter, situation) runnables.each { |runnable| reporter.report(runnable.to_s, runnable.run(situation)) } end |
#run(reporter) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/riot/context.rb', line 176 def run(reporter) reporter.describe_context(self) unless @assertions.empty? local_run(reporter, situation_class.new) run_sub_contexts(reporter) reporter end |
#setup(premium = false, &definition) ⇒ Object
Add a setup block.
A setup block defines the topic of the context. There can be multiple setup blocks; each can access the previous topic through the topic
attribute.
context "A string" do
setup { "foo" }
setup { topic * 2 }
asserts(:length).equals(6)
end
If you provide true
as the first argument, the setup will be unshifted onto the list of setups, ensuring it will be run before any other setups. This is really only useful for context middlewares.
95 96 97 98 99 |
# File 'lib/riot/context.rb', line 95 def setup(premium=false, &definition) setup = Setup.new(&definition) premium ? @setups.unshift(setup) : @setups.push(setup) setup end |
#setups ⇒ Array[Riot::RunnableBlock]
Returns an ordered list of the setup blocks for the context.
71 72 73 |
# File 'lib/riot/context.rb', line 71 def setups @parent.setups + @setups end |
#should(what, &definition) ⇒ Object
Same as #asserts, except it uses the phrase “should” in the report output. Sometimes you feel like a nut, sometimes you don’t.
should("ensure expected") { "bar" }.equals("bar")
165 166 167 |
# File 'lib/riot/context.rb', line 165 def should(what, &definition) new_assertion("should", what, &definition) end |
#teardown(&definition) ⇒ Object
Add a teardown block. You may define multiple of these as well.
teardown { Bombs.drop! }
132 133 134 |
# File 'lib/riot/context.rb', line 132 def teardown(&definition) (@teardowns << Setup.new(&definition)).last end |
#teardowns ⇒ Array[Riot::RunnableBlock]
Returns an ordered list of the teardown blocks for the context.
78 79 80 |
# File 'lib/riot/context.rb', line 78 def teardowns @parent.teardowns + @teardowns end |