Class: Marso::Scenario

Inherits:
Object show all
Includes:
ScenarioPublish
Defined in:
lib/marso/domain/scenario/scenario.rb

Constant Summary collapse

ISSUES_LIST =
[:error, :failed, :cancelled]
@@color_options =
nil
@@color_options_size =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ScenarioPublish

#colorized_text

Methods included from TextHelper

#colorized_text, #indented_colorized_text

Constructor Details

#initialize(description, ctx = {}) ⇒ Scenario

description: Hash defined as follow

:id => Arbitrary number or string. Default is randomly generated
       (hex string (length 8))
:name => Scenario's name
:steps => array of Step objects
:color_theme => color from gem 'colorize'(e.g. :blue). That allows
                to visually group all steps from the same scenario.
                Default is randomly choosen from the available set
:cancel_steps_upon_issues => Boolean. If true, all steps defined after
                             a broken step(i.e. step in status :failed,
                             :error, or :cancelled) will not be
                             executed, and will all be set so their
                             status is :cancelled.

                             If defined, it overrides the
                             Config.cancel_steps_upon_issues setting.
:realtime_step_stdout => Boolean. If true, the result of each step is
                         output to the console in realtime rather than
                         waiting for the entire scenario to finish and
                         then display all the steps all at once. Setting
                         that config to true may make reading the output
                         harder when multiple scenarios are executed in
                         paralell.Steps of different scenarios may
                         indeed be intertwined in the console.

                         If defined, it overrides the
                         Config.realtime_step_stdout setting.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/marso/domain/scenario/scenario.rb', line 101

def initialize(description, ctx={})
  validate_arguments(description, ctx)

  if @@color_options.nil?
    @@color_options = String.colors
    @@color_options_size = @@color_options.size
  end

  @name = description[:name]
  @fname = description[:name].downcase.gsub(' ', '_')
  @ctx = ctx.clone

  @tree_position = 0
  @tree_position+=1 unless ctx[:story_id].nil?
  @tree_position+=1 unless ctx[:feature_id].nil?

  @story_id = ctx[:story_id]
  @feature_id = ctx[:feature_id]

  @id =
    description.key?(:id) ?
    description[:id] :
    SecureRandom.hex(4)

  @status =
    description.key?(:status) ?
    description[:status] :
    :none

  @color_theme =
    description.key?(:color_theme) ?
    description[:color_theme] :
    @@color_options[rand(@@color_options_size)]

  @steps =
    description.key?(:steps) ?
    description[:steps].map { |s| Step.new(s.text, @id, @color_theme, s.status, &s.block) } :
    []

  @header = get_header(@id, @ctx)

  @cancel_steps_upon_issues =
    description.key?(:cancel_steps_upon_issues) ?
    description[:cancel_steps_upon_issues] :
    Marso::Config.get(:cancel_steps_upon_issues)

  @realtime_step_stdout =
    description.key?(:realtime_step_stdout) ?
    description[:realtime_step_stdout] :
    Marso::Config.get(:realtime_step_stdout)
end

Instance Attribute Details

#cancel_steps_upon_issuesObject (readonly)

Returns the value of attribute cancel_steps_upon_issues.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def cancel_steps_upon_issues
  @cancel_steps_upon_issues
end

#color_themeObject (readonly)

Returns the value of attribute color_theme.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def color_theme
  @color_theme
end

#ctxObject (readonly)

Returns the value of attribute ctx.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def ctx
  @ctx
end

#feature_idObject (readonly)

Returns the value of attribute feature_id.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def feature_id
  @feature_id
end

#fnameObject (readonly)

Returns the value of attribute fname.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def fname
  @fname
end

#headerObject (readonly)

Returns the value of attribute header.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def header
  @header
end

#idObject (readonly)

Returns the value of attribute id.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def name
  @name
end

#realtime_step_stdoutObject (readonly)

Returns the value of attribute realtime_step_stdout.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def realtime_step_stdout
  @realtime_step_stdout
end

#statusObject (readonly)

Returns the value of attribute status.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def status
  @status
end

#stepsObject (readonly)

Returns the value of attribute steps.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def steps
  @steps
end

#story_idObject (readonly)

Returns the value of attribute story_id.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def story_id
  @story_id
end

#tree_positionObject (readonly)

Returns the value of attribute tree_position.



70
71
72
# File 'lib/marso/domain/scenario/scenario.rb', line 70

def tree_position
  @tree_position
end

Instance Method Details

#and(assumption_text, *args, &block) ⇒ Object



157
158
159
# File 'lib/marso/domain/scenario/scenario.rb', line 157

def and(assumption_text, *args, &block)
  return add_step(:and, assumption_text, *args, &block)
end

#but(assumption_text, *args, &block) ⇒ Object



169
170
171
# File 'lib/marso/domain/scenario/scenario.rb', line 169

def but(assumption_text, *args, &block)
  return add_step(:but, assumption_text, *args, &block)
end

#given(assumption_text, *args, &block) ⇒ Object



153
154
155
# File 'lib/marso/domain/scenario/scenario.rb', line 153

def given(assumption_text, *args, &block)
  return add_step(:given, assumption_text, *args, &block)
end

#runObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/marso/domain/scenario/scenario.rb', line 185

def run
  previous_step_status = nil
  scenario_status = :passed
  no_issues = true

  processed_steps = @steps.map { |s|
    runned_step = run_step(s, previous_step_status)

    print_indented(runned_step.print_description) if @realtime_step_stdout

    previous_step_status = runned_step.status

    if no_issues
      case previous_step_status
      when :error
        no_issues = false
        scenario_status = :error
      when :failed
        no_issues = false
        scenario_status = :failed
      when :cancelled
        no_issues = false
        scenario_status = :failed
      end
    end

    runned_step
  }

  updated_scenario = Scenario.new(
    {
      :id => @id,
      :name => @name,
      :steps => processed_steps,
      :status => scenario_status,
      :color_theme => @color_theme
    },
    @ctx)

  return updated_scenario
end

#text(include_id = false) ⇒ Object

include_id will prepend the scenario id to the step’s description. This can be useful in the case where each step is being output to the console in realtime. In that situation multiple steps from multiple scenarios may be intertwined if they are executed concurently. Without the scenario id, it may be difficult to identify which step belongs to which scenario



179
180
181
182
183
# File 'lib/marso/domain/scenario/scenario.rb', line 179

def text(include_id=false)
  return
    "{@header}: #{name}\n" +
    (@steps.any? ? @steps.map { |s| s.text(include_id) }.join("\n") : "")
end

#then(assumption_text, *args, &block) ⇒ Object



165
166
167
# File 'lib/marso/domain/scenario/scenario.rb', line 165

def then(assumption_text, *args, &block)
  return add_step(:then, assumption_text, *args, &block)
end

#when(assumption_text, *args, &block) ⇒ Object



161
162
163
# File 'lib/marso/domain/scenario/scenario.rb', line 161

def when(assumption_text, *args, &block)
  return add_step(:when, assumption_text, *args, &block)
end