Class: Scenario

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

Direct Known Subclasses

ObjcScenario, RailsScenario

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Scenario

Returns a new instance of Scenario.



5
6
7
8
9
10
11
12
13
14
# File 'lib/scenario.rb', line 5

def initialize(hash)
  @title  = hash[:title]
  @body   = hash[:body]
  @parent = hash[:parent]
  @given_scenario_keyword = hash[:given_scenario_keyword] || "GivenScenario:"
  @follow_up_keyword = hash[:follow_up_keyword] || "And"
  
  raise "No title given" unless title
  raise "No body given" unless body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



2
3
4
# File 'lib/scenario.rb', line 2

def body
  @body
end

#follow_up_keywordObject (readonly)

Returns the value of attribute follow_up_keyword.



2
3
4
# File 'lib/scenario.rb', line 2

def follow_up_keyword
  @follow_up_keyword
end

#given_scenario_keywordObject (readonly)

Returns the value of attribute given_scenario_keyword.



2
3
4
# File 'lib/scenario.rb', line 2

def given_scenario_keyword
  @given_scenario_keyword
end

#linesObject (readonly)

Returns the value of attribute lines.



2
3
4
# File 'lib/scenario.rb', line 2

def lines
  @lines
end

#parentObject (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/scenario.rb', line 2

def parent
  @parent
end

#passedObject (readonly)

Returns the value of attribute passed.



2
3
4
# File 'lib/scenario.rb', line 2

def passed
  @passed
end

#stepsObject (readonly)

Returns the value of attribute steps.



2
3
4
# File 'lib/scenario.rb', line 2

def steps
  @steps
end

#titleObject (readonly)

Returns the value of attribute title.



2
3
4
# File 'lib/scenario.rb', line 2

def title
  @title
end

Instance Method Details

#collect_stepsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scenario.rb', line 33

def collect_steps
  if has_given_scenarios?
    @body = expand_given_scenarios_in_body.strip
  end

  if has_follow_ups?
    @body = expand_follow_ups_in_body.strip
  end
  
  
  @lines = body.split(/\n+/).map {|s| s.strip}
  raise "No Steps found" if lines.empty?
  
  @steps = parse_lines
  self
end

#expand_follow_ups_in_bodyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/scenario.rb', line 59

def expand_follow_ups_in_body
  lines = body.split("\n")
  new_lines = []
  step = ""
  for line in lines
    if line =~ follow_up_line_exp
      args = line.sub(/^\s*#{follow_up_keyword}\s*/, "").split("|").reject {|arg| arg =~ /\A\s*\Z/ }.map {|arg| "'" + arg.strip + "'"}
      arr  = step.split("'").reject {|elt| elt =~ /\A\s*\Z/ }
      offset = (step =~ /\A\s*'/) ? 0 : 1
      new_step = ""
      
      arr.each_with_index do |elt, index|
        if index % 2 == offset
          new_step << args[(index - offset) / 2]
        else
          new_step << elt
        end
      end
      new_lines << new_step 
    else
      new_lines << line
      step = line
    end
  end
  new_lines.join("\n")
end

#expand_given_scenarios_in_bodyObject



50
51
52
53
54
55
56
57
# File 'lib/scenario.rb', line 50

def expand_given_scenarios_in_body
  raise "No associated Feature" unless parent 
  raise "Associated Feature has no Scenarios" unless parent.scenarios
  body.gsub(/#{given_scenario_keyword}(.*)/) do |m|
    existing_scenario = parent.scenarios.detect {|s| s.title == $1.strip }
    existing_scenario ? existing_scenario.body.strip : ""
  end
end

#follow_up_line_expObject



94
95
96
# File 'lib/scenario.rb', line 94

def follow_up_line_exp
  /#{follow_up_keyword}\s*\|(.*)\s*\|\s*/
end

#given_scenario_line_expObject



86
87
88
# File 'lib/scenario.rb', line 86

def given_scenario_line_exp
  /#{given_scenario_keyword}(.*)/
end

#has_follow_ups?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/scenario.rb', line 98

def has_follow_ups?
  !!(body =~ follow_up_line_exp)
end

#has_given_scenarios?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/scenario.rb', line 90

def has_given_scenarios?
  !!(body =~ given_scenario_line_exp)
end

#keywordObject



16
17
18
# File 'lib/scenario.rb', line 16

def keyword
  parent.scenario_keyword
end

#passed?Boolean

Returns:

  • (Boolean)


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

def passed?
  !!@passed
end

#to_htmlObject



20
21
22
23
24
25
26
27
# File 'lib/scenario.rb', line 20

def to_html
  <<-END
  <div class="scenario #{passed? ? "passed" : "failed"}">
    <h2 class="scenario_title">#{keyword} #{title}</h2>
    #{steps.map {|s| s.to_html}.join(" \n")}
  </div>
  END
end