Class: Nutrasuite::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/nutrasuite/contexts.rb

Overview

Internal: The Context class represents each context that can go on the context stack. Each context is responsible for tracking its specific information (mostly its name and its setup and teardown methods). Contexts should only be created using one of the article methods in ContextHelpers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Context

Internal: Create a new Context object.

name - The name of the context, which will be prepended to any nested

contexts/tests in the final test name

&block - The block that defines the contents of the context; should

consist of:
- test definitions
- sub-context definitions
- setup and teardown declarations


133
134
135
136
137
138
139
# File 'lib/nutrasuite/contexts.rb', line 133

def initialize(name, &block)
  @name = name
  @block = block

  @setups = []
  @teardowns = []
end

Instance Attribute Details

#nameObject (readonly)

Internal: Get the name, setup methods, and teardown methods for this Context.



121
122
123
# File 'lib/nutrasuite/contexts.rb', line 121

def name
  @name
end

#setupsObject (readonly)

Internal: Get the name, setup methods, and teardown methods for this Context.



121
122
123
# File 'lib/nutrasuite/contexts.rb', line 121

def setups
  @setups
end

#teardownsObject (readonly)

Internal: Get the name, setup methods, and teardown methods for this Context.



121
122
123
# File 'lib/nutrasuite/contexts.rb', line 121

def teardowns
  @teardowns
end

Class Method Details

.build_test_name(name = "") ⇒ Object

Internal: builds a test name based on the current state of the context stack.

Examples

# Assuming the context stack looks like "a user","that is an admin":
Context.build_test_name("has admin privileges") 
#=> "test a user that is an admin has admin privileges"

Returns a string name for a method that will automatically be executed by MiniTest.



160
161
162
163
164
165
166
167
168
# File 'lib/nutrasuite/contexts.rb', line 160

def self.build_test_name(name="")
  full_name = "test "
  context_stack.each do |context|
    unless context.name.nil?
      full_name << context.name << " "
    end
  end
  full_name << name
end

.context_stackObject

Internal: get the context stack, or initialize it to an empty list if necessary.

Returns: an Array representing the context stack.



190
191
192
# File 'lib/nutrasuite/contexts.rb', line 190

def self.context_stack
  @context_stack ||= [Context.new(nil)]
end

.current_contextObject

Internal: get the current context.

Returns: the context currently at the top of the stack, or nil if there are no contexts in the stack at the moment.



198
199
200
# File 'lib/nutrasuite/contexts.rb', line 198

def self.current_context
  context_stack.last
end

.push(name, &block) ⇒ Object

Internal: pushes a new context onto the stack, builds that context out, and then removes it from the stack.

This method should be the only means by which contexts get built, as it ensures that the context is removed from the context stack when it’s done building itself.



177
178
179
180
181
182
183
184
# File 'lib/nutrasuite/contexts.rb', line 177

def self.push(name, &block)
  context = Context.new(name, &block)
  context_stack.push(context)

  context.build

  context_stack.pop
end

Instance Method Details

#buildObject

Internal: build runs the block that defines the context’s contents, thus setting up any nested contexts and tests.



144
145
146
# File 'lib/nutrasuite/contexts.rb', line 144

def build
  @block.call
end