Class: Diddy::Scenario

Inherits:
Object
  • Object
show all
Defined in:
lib/diddy/scenario.rb

Overview

A scenario contains several steps. This is where the real work is done.

Defined Under Namespace

Classes: ScenarioAborted

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scenario

Creates a scenario



11
12
13
# File 'lib/diddy/scenario.rb', line 11

def initialize(options = {})
  options.each { |k,v| send("#{k}=", v) }
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/diddy/scenario.rb', line 6

def context
  @context
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/diddy/scenario.rb', line 6

def description
  @description
end

#run_resultObject

Returns the value of attribute run_result.



6
7
8
# File 'lib/diddy/scenario.rb', line 6

def run_result
  @run_result
end

#scriptObject

Returns the value of attribute script.



6
7
8
# File 'lib/diddy/scenario.rb', line 6

def script
  @script
end

#stepsObject

Returns the value of attribute steps.



6
7
8
# File 'lib/diddy/scenario.rb', line 6

def steps
  @steps
end

Class Method Details

.run(run_result, scenario, options = {}) ⇒ Object

Runs a given scenario



139
140
141
142
143
144
# File 'lib/diddy/scenario.rb', line 139

def self.run(run_result, scenario, options = {})
  scenario.reset!
  scenario.context = Context.new(options)
  scenario.run_result = run_result
  scenario.run
end

Instance Method Details

#find_steps_instance_for(description) ⇒ Object

Finds the instance of the steps definition by step name



122
123
124
125
126
127
128
129
130
# File 'lib/diddy/scenario.rb', line 122

def find_steps_instance_for(description)
  @steps_instances.each do |instance|
    if instance.class.steps && instance.class.steps.has_key?(description)
      return instance
    end
  end

  nil
end

Prints exception thrown by step, on screen



110
111
112
113
114
115
116
117
# File 'lib/diddy/scenario.rb', line 110

def print_exception(exception)
  print("\n")

  # print backtrace
  puts("- #{exception.message}")
  puts("  #{exception.backtrace.join("\n  ")}")
  puts("\n")
end

#reset!Object

Reset me



18
19
20
21
22
23
24
# File 'lib/diddy/scenario.rb', line 18

def reset!
  instance_variables.each do |var|
    unless %w(@script @context @description @steps @run_result).include?(var.to_s)
      remove_instance_variable(var)
    end
  end
end

#runObject

Runs all the steps in the script



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/diddy/scenario.rb', line 58

def run
  begin
    @resetted = {}

    # also log
    puts("Scenario: #{description}")
    run_result.run_scenario("Scenario: #{description}")

    @steps.each do |step|
      run_step(step)
    end

    true

  rescue ScenarioAborted
    false

  end
end

#run_step(step) ⇒ Object

Runs one step



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/diddy/scenario.rb', line 81

def run_step(step)
  # run proc on this instance as scope
  begin
    # check if we need to reset the step instance
    unless @resetted[step.steps_instance.object_id]
      step.steps_instance.reset!
      @resetted[step.steps_instance.object_id] = true
    end

    step.run_result = run_result
    step.context = context

    result = step.run

  rescue Exception => exception
    print_exception(exception)
    run_result.set_step_exception(exception)

    raise ScenarioAborted.new
  end

  unless result
    raise ScenarioAborted.new
  end
end

#shared_scopeObject



132
133
134
# File 'lib/diddy/scenario.rb', line 132

def shared_scope
  @shared_scope ||= SharedScope.new
end

#step(description) ⇒ Object

Describes which step should be run



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/diddy/scenario.rb', line 37

def step(description)
  @steps ||= []

  # find step klass
  steps_instance = find_steps_instance_for(description)

  # check if step exists
  if steps_instance && steps_instance.class.has_step?(description)
    @steps << Diddy::Step.new(
      description:      description,
      steps_instance:   steps_instance,
      definition:       steps_instance.class.definition(description)
    )
  else
    raise "Step '#{description}' not defined"
  end
end

#uses(klass) ⇒ Object

Determines which step classes should be used



29
30
31
32
# File 'lib/diddy/scenario.rb', line 29

def uses(klass)
  @steps_instances ||= []
  @steps_instances << klass.new(shared_scope)
end