Class: Cucumber::Ast::Background

Inherits:
Object
  • Object
show all
Includes:
HasLocation, HasSteps, Names
Defined in:
lib/cucumber/ast/background.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes included from Names

#description, #title

Attributes included from HasSteps

#description, #gherkin_statement, #raw_steps, #title

Instance Method Summary collapse

Methods included from HasLocation

#file, #file_colon_line, #line, #location

Methods included from Names

#name

Methods included from HasSteps

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

Constructor Details

#initialize(language, location, comment, keyword, title, description, raw_steps) ⇒ Background

Returns a new instance of Background.



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

def initialize(language, location, comment, keyword, title, description, raw_steps)
  @language, @location, @comment, @keyword, @title, @description, @raw_steps = language, location, comment, keyword, title, description, raw_steps
  @failed = nil
  @first_collection_created = false
  attach_steps(@raw_steps)
end

Instance Attribute Details

#featureObject

Returns the value of attribute feature.



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

def feature
  @feature
end

Instance Method Details

#accept(visitor) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cucumber/ast/background.rb', line 37

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

#accept_hook?(hook) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/cucumber/ast/background.rb', line 61

def accept_hook?(hook)
  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



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

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

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  !!@failed
end

#feature_elementsObject



20
21
22
# File 'lib/cucumber/ast/background.rb', line 20

def feature_elements
  feature.feature_elements
end

#hook_contextObject



78
79
80
# File 'lib/cucumber/ast/background.rb', line 78

def hook_context
  feature_elements.first || self
end

#skip_invoke!Object



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

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

#source_tag_namesObject



104
105
106
# File 'lib/cucumber/ast/background.rb', line 104

def source_tag_names
  source_tags.map { |tag| tag.name }
end

#source_tagsObject

Override this method, as there are situations where the background wind up being the one called fore Before scenarios, and backgrounds don’t have tags.



100
101
102
# File 'lib/cucumber/ast/background.rb', line 100

def source_tags
  []
end

#step_collection(scenario_step_invocations) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/cucumber/ast/background.rb', line 28

def step_collection(scenario_step_invocations)
  if(@first_collection_created)
    steps.step_invocations(true).dup(scenario_step_invocations)
  else
    @first_collection_created = true
    step_invocations.dup(scenario_step_invocations)
  end
end

#step_invocationsObject



24
25
26
# File 'lib/cucumber/ast/background.rb', line 24

def step_invocations
  @step_invocations ||= steps.step_invocations(true)
end

#to_sexpObject



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

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

#with_visitor(scenario, visitor) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/cucumber/ast/background.rb', line 50

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