Class: Cucumber::Ast::Background

Inherits:
Object
  • Object
show all
Includes:
FeatureElement
Defined in:
lib/cucumber/ast/background.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes included from FeatureElement

#feature, #gherkin_statement, #raw_steps

Instance Method Summary collapse

Methods included from FeatureElement

#add_step, #attach_steps, #backtrace_line, #file_colon_line, #first_line_length, #language, #matches_scenario_names?, #max_line_length, #name_line_lengths, #source_indent, #source_tag_names, #text_length

Constructor Details

#initialize(comment, line, keyword, name, raw_steps) ⇒ Background

Returns a new instance of Background.



9
10
11
12
# File 'lib/cucumber/ast/background.rb', line 9

def initialize(comment, line, keyword, name, raw_steps)
  @comment, @line, @keyword, @name, @raw_steps = comment, line, keyword, name, raw_steps
  @feature_elements = []
end

Instance Attribute Details

#feature_elementsObject (readonly)

Returns the value of attribute feature_elements.



7
8
9
# File 'lib/cucumber/ast/background.rb', line 7

def feature_elements
  @feature_elements
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cucumber/ast/background.rb', line 7

def name
  @name
end

Instance Method Details

#accept(visitor) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cucumber/ast/background.rb', line 31

def accept(visitor)
  return if Cucumber.wants_to_quit
  init
  visitor.visit_comment(@comment) unless @comment.empty?
  visitor.visit_background_name(@keyword, @name, file_colon_line(@line), source_indent(first_line_length))
  with_visitor(hook_context, visitor) do
    visitor.step_mother.before(hook_context)
    skip_invoke! if failed?
    visitor.visit_steps(@step_invocations)
    @failed = @step_invocations.detect{|step_invocation| step_invocation.exception || step_invocation.status != :passed }
    visitor.step_mother.after(hook_context) if @failed || @feature_elements.empty?
  end
end

#accept_hook?(hook) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
# File 'lib/cucumber/ast/background.rb', line 57

def accept_hook?(hook)
  init
  if hook_context != self
    hook_context.accept_hook?(hook)
  else
    # We have no scenarios, just ask our feature
    @feature.accept_hook?(hook)
  end
end

#fail!(exception) ⇒ Object



90
91
92
93
94
# File 'lib/cucumber/ast/background.rb', line 90

def fail!(exception)
  @failed = true
  @exception = exception
  @current_visitor.visit_exception(@exception, :failed)        
end

#failed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/cucumber/ast/background.rb', line 71

def failed?
  @failed
end

#hook_contextObject



75
76
77
# File 'lib/cucumber/ast/background.rb', line 75

def hook_context
  @feature_elements.first || self
end

#initObject



14
15
16
17
18
19
# File 'lib/cucumber/ast/background.rb', line 14

def init
  return if @steps
  attach_steps(@raw_steps)
  @steps = StepCollection.new(@raw_steps)
  @step_invocations = @steps.step_invocations(true)
end

#skip_invoke!Object



67
68
69
# File 'lib/cucumber/ast/background.rb', line 67

def skip_invoke!
  @step_invocations.each{|step_invocation| step_invocation.skip_invoke!}
end

#step_collection(step_invocations) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/cucumber/ast/background.rb', line 21

def step_collection(step_invocations)
  init
  unless(@first_collection_created)
    @first_collection_created = true
    @step_invocations.dup(step_invocations)
  else
    @steps.step_invocations(true).dup(step_invocations)
  end
end

#to_sexpObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/cucumber/ast/background.rb', line 79

def to_sexp
  init
  sexp = [:background, @line, @keyword]
  sexp += [@name] unless @name.empty?
  comment = @comment.to_sexp
  sexp += [comment] if comment
  steps = @steps.to_sexp
  sexp += steps if steps.any?
  sexp
end

#with_visitor(scenario, visitor) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cucumber/ast/background.rb', line 45

def with_visitor(scenario, visitor)
  @current_visitor = visitor
  init
  if self != scenario && scenario.respond_to?(:with_visitor)
    scenario.with_visitor(visitor) do
      yield
    end
  else
    yield
  end
end