Class: Scope::Context
- Inherits:
-
Object
- Object
- Scope::Context
- Defined in:
- lib/scope.rb
Overview
A context keeps track of the tests defined inside of it as well as its setup and teardown blocks.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent_context ⇒ Object
readonly
Returns the value of attribute parent_context.
-
#setup ⇒ Object
Returns the value of attribute setup.
-
#setup_once ⇒ Object
Returns the value of attribute setup_once.
-
#teardown ⇒ Object
Returns the value of attribute teardown.
-
#teardown_once ⇒ Object
Returns the value of attribute teardown_once.
-
#tests_and_subcontexts ⇒ Object
We keep both tests and subcontexts in the same array because we need to know what the very last thing to execute inside of this context is, for the purpose of calling teardown_once at the correct time.
Instance Method Summary collapse
- #ancestor_contexts ⇒ Object
-
#initialize(name, parent_context = nil) ⇒ Context
constructor
A new instance of Context.
-
#run_setup_and_teardown(test_case_instance, test_name) ⇒ Object
Runs the setup work for this context and any parent contexts, yields to the block (which should invoke the actual test method), and then completes the teardown work.
Constructor Details
#initialize(name, parent_context = nil) ⇒ Context
Returns a new instance of Context.
102 103 104 105 106 |
# File 'lib/scope.rb', line 102 def initialize(name, parent_context = nil) @name = name @parent_context = parent_context self.tests_and_subcontexts = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
96 97 98 |
# File 'lib/scope.rb', line 96 def name @name end |
#parent_context ⇒ Object (readonly)
Returns the value of attribute parent_context.
96 97 98 |
# File 'lib/scope.rb', line 96 def parent_context @parent_context end |
#setup ⇒ Object
Returns the value of attribute setup.
100 101 102 |
# File 'lib/scope.rb', line 100 def setup @setup end |
#setup_once ⇒ Object
Returns the value of attribute setup_once.
100 101 102 |
# File 'lib/scope.rb', line 100 def setup_once @setup_once end |
#teardown ⇒ Object
Returns the value of attribute teardown.
100 101 102 |
# File 'lib/scope.rb', line 100 def teardown @teardown end |
#teardown_once ⇒ Object
Returns the value of attribute teardown_once.
100 101 102 |
# File 'lib/scope.rb', line 100 def teardown_once @teardown_once end |
#tests_and_subcontexts ⇒ Object
We keep both tests and subcontexts in the same array because we need to know what the very last thing to execute inside of this context is, for the purpose of calling teardown_once at the correct time.
99 100 101 |
# File 'lib/scope.rb', line 99 def tests_and_subcontexts @tests_and_subcontexts end |
Instance Method Details
#ancestor_contexts ⇒ Object
131 132 133 134 135 136 |
# File 'lib/scope.rb', line 131 def ancestor_contexts ancestors = [] parent = self ancestors << parent while (parent = parent.parent_context) ancestors end |
#run_setup_and_teardown(test_case_instance, test_name) ⇒ Object
Runs the setup work for this context and any parent contexts, yields to the block (which should invoke the actual test method), and then completes the teardown work.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/scope.rb', line 110 def run_setup_and_teardown(test_case_instance, test_name) contexts = ([self] + ancestor_contexts).reverse # We're using instance_eval so that instance vars set by the block are created on the test_case_instance contexts.each { |context| test_case_instance.instance_eval(&context.setup_once) if context.setup_once } contexts.each { |context| test_case_instance.instance_eval(&context.setup) if context.setup } yield contexts.reverse! contexts.each { |context| test_case_instance.instance_eval(&context.teardown) if context.teardown } # If this is the last context being run in any parent contexts, run their teardown_once blocks. if tests_and_subcontexts.last == test_name test_case_instance.instance_eval(&teardown_once) if teardown_once descendant_context = self ancestor_contexts.each do |ancestor| break unless ancestor.tests_and_subcontexts.last == descendant_context test_case_instance.instance_eval(&ancestor.teardown_once) if ancestor.teardown_once descendant_context = ancestor end end end |