Class: Strut::ScenarioBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/strut/scenario_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(document_builder, command_factory) ⇒ ScenarioBuilder

Returns a new instance of ScenarioBuilder.



3
4
5
6
# File 'lib/strut/scenario_builder.rb', line 3

def initialize(document_builder, command_factory)
  @document_builder = document_builder
  @command_factory = command_factory
end

Instance Method Details

#append_execute_command(scenario_number, line, instance) ⇒ Object



74
75
76
77
78
# File 'lib/strut/scenario_builder.rb', line 74

def append_execute_command(scenario_number, line, instance)
   = .new(scenario_number, line)
  execute_command = @command_factory.make_call_command(, instance, "execute")
  @document_builder.append_command(execute_command)
end

#append_given_commands(scenario_number, stages, instance) ⇒ Object



69
70
71
72
# File 'lib/strut/scenario_builder.rb', line 69

def append_given_commands(scenario_number, stages, instance)
  given_stages = stages_with_names(stages, ["given", "when"])
  parse_stages(given_stages) { |k, v| make_given_command(scenario_number, k, v, instance) }
end

#append_make_command(scenario_number, line, instance, class_name) ⇒ Object



34
35
36
37
38
# File 'lib/strut/scenario_builder.rb', line 34

def append_make_command(scenario_number, line, instance, class_name)
   = .new(scenario_number, line)
  make_command = @command_factory.make_make_command(, instance, class_name)
  @document_builder.append_command(make_command)
end

#append_method_command(scenario_number, line, instance, method) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/strut/scenario_builder.rb', line 49

def append_method_command(scenario_number, line, instance, method)
  if method
     = .new(scenario_number, line)
    method_command = make_set_command(scenario_number, line, instance, "method", method)
    @document_builder.append_command(method_command)
  end
end

#append_status_command(scenario_number, line, instance, statusCode) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/strut/scenario_builder.rb', line 85

def append_status_command(scenario_number, line, instance, statusCode)
  if statusCode
     = .new(scenario_number, line, statusCode)
    status_command = @command_factory.make_call_command(, instance, "statusCode")
    @document_builder.append_command(status_command)
  end
end

#append_then_commands(scenario_number, stages, instance) ⇒ Object



80
81
82
83
# File 'lib/strut/scenario_builder.rb', line 80

def append_then_commands(scenario_number, stages, instance)
  then_stages = stages_with_names(stages, ["then"])
  parse_stages(then_stages) { |k, v| make_then_command(scenario_number, k, v, instance) }
end

#append_uri_command(scenario_number, line, instance, uri, scenario_stages) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/strut/scenario_builder.rb', line 40

def append_uri_command(scenario_number, line, instance, uri, scenario_stages)
  if uri
     = .new(scenario_number, line)
    combined_uri = combine_uri_with_parameters(scenario_stages, uri)
    path_command = make_set_command(scenario_number, line, instance, "uri", combined_uri)
    @document_builder.append_command(path_command)
  end
end

#combine_uri_with_parameters(stages, uri) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strut/scenario_builder.rb', line 57

def combine_uri_with_parameters(stages, uri)
  combined_uri = uri.dup
  when_stages = stages_with_names(stages, ["when"])
  when_stages.each do |stage|
    stage.each do |k, v|
      argument = v["value"].to_s
      combined_uri.gsub!(/\{#{k}\}/, argument)
    end
  end
  combined_uri
end

#extract_scenarios_for_interaction(scenario_number, interaction, fixture, node) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/strut/scenario_builder.rb', line 8

def extract_scenarios_for_interaction(scenario_number, interaction, fixture, node)
  instance = "instance-#{scenario_number}"
  line = node["line"]
  scenario_definitions_for_node(node).each do |scenario_stages|
    make_scenario(scenario_number, line, instance, fixture, interaction, scenario_stages)
    scenario_number += 1
  end
  scenario_number
end

#make_given_command(scenario_number, property_name, value_container, instance) ⇒ Object



93
94
95
96
97
# File 'lib/strut/scenario_builder.rb', line 93

def make_given_command(scenario_number, property_name, value_container, instance)
  line = value_container["line"]
  value = value_container["value"]
  make_set_command(scenario_number, line, instance, property_name, value)
end

#make_scenario(scenario_number, line, instance, fixture, interaction, scenario_stages) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/strut/scenario_builder.rb', line 24

def make_scenario(scenario_number, line, instance, fixture, interaction, scenario_stages)
  append_make_command(scenario_number, line, instance, fixture)
  append_uri_command(scenario_number, line, instance, interaction.uri, scenario_stages)
  append_method_command(scenario_number, line, instance, interaction.method)
  append_given_commands(scenario_number, scenario_stages, instance)
  append_execute_command(scenario_number, line, instance)
  append_then_commands(scenario_number, scenario_stages, instance)
  append_status_command(scenario_number, line, instance, interaction.statusCode)
end

#make_set_command(scenario_number, line, instance, name, value) ⇒ Object



99
100
101
102
# File 'lib/strut/scenario_builder.rb', line 99

def make_set_command(scenario_number, line, instance, name, value)
   = .new(scenario_number, line)
  @command_factory.make_call_command(, instance, "set_#{name}", value)
end

#make_then_command(scenario_number, property_name, value_container, instance) ⇒ Object



104
105
106
107
108
109
# File 'lib/strut/scenario_builder.rb', line 104

def make_then_command(scenario_number, property_name, value_container, instance)
  line = value_container["line"]
  value = value_container["value"]
   = .new(scenario_number, line, value)
  @command_factory.make_call_command(, instance, property_name)
end

#parse_stages(stages, &block) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/strut/scenario_builder.rb', line 115

def parse_stages(stages, &block)
  stages.each do |stage|
    stage.each do |k, v|
      command = block.call(k, v)
      @document_builder.append_command(command)
    end
  end
end

#scenario_definitions_for_node(node) ⇒ Object



18
19
20
21
22
# File 'lib/strut/scenario_builder.rb', line 18

def scenario_definitions_for_node(node)
  raw_scenarios = node["value"]
  raw_scenarios = [raw_scenarios] if raw_scenarios.respond_to?(:each_pair)
  raw_scenarios
end

#stages_with_names(stages, names) ⇒ Object



111
112
113
# File 'lib/strut/scenario_builder.rb', line 111

def stages_with_names(stages, names)
  names.map { |name| stages[name] }.reject { |stage| stage.nil? }.map { |stage| stage["value"] }
end