Class: CucumberJunitToJson::Models::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber_junit_to_json/models/step.rb

Overview

Abstract representation of a cucumber step attribute

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStep

Returns a new instance of Step.



16
17
18
# File 'lib/cucumber_junit_to_json/models/step.rb', line 16

def initialize
  @table = CucumberJunitToJson::Models::Table.new
end

Instance Attribute Details

#keywordObject

Returns the value of attribute keyword.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def keyword
  @keyword
end

#lineObject

Returns the value of attribute line.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def line
  @line
end

#matchObject

Returns the value of attribute match.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def match
  @match
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def name
  @name
end

#resultObject

Returns the value of attribute result.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def result
  @result
end

#rowsObject

Returns the value of attribute rows.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def rows
  @rows
end

#tableObject

Returns the value of attribute table.



15
16
17
# File 'lib/cucumber_junit_to_json/models/step.rb', line 15

def table
  @table
end

Class Method Details

.get_scenario_step_matching(scenario, file, step) ⇒ Object

Raises:



77
78
79
80
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
106
# File 'lib/cucumber_junit_to_json/models/step.rb', line 77

def self.get_scenario_step_matching(scenario, file, step)
  count = 0
  found_scenario = false
  File.open(file, 'r') do |f|
    f.each_line do |line|
      count += 1
      # Check if we got a match anchor in line.
      # If there is, use a similar matcher
      if line =~ /<\S+>/
        # A match percentage greater than 80 is an indication
        # of a good match for scenarios
        found_scenario = true if line.similar(scenario) >= 73
      elsif line =~ /#{scenario}/
        found_scenario = true
      end
      if found_scenario
        # Check if we got a match anchor in line.
        # If there is, use a similar matcher
        if line =~ /<\S+>/
          # A match percentage greater than 67 is an indication
          # of a good match for steps
          return line, count if line.similar(step) >= 67
        elsif line =~ /#{step}/
          return line, count
        end
      end
    end
  end
  raise Error, "Could not find step '#{step}' in '#{scenario}' looking into #{file}"
end

.get_steps_for(scenario_title, scenario_str, feature_file_path, failing_step = nil, failure_message = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cucumber_junit_to_json/models/step.rb', line 20

def self.get_steps_for(scenario_title, scenario_str, feature_file_path, failing_step = nil, failure_message = nil)
  steps = []
  table = []
  prev_step_has_table = false
  keywords = %w[Given When Then And But]
  scenario_steps = scenario_str.split("\n").reject(&:empty?)
  scenario_steps.each do |scenario_step|
    step = Step.new
    scenario_step = scenario_step.to_s.strip
    if scenario_step.start_with?(*keywords)
      # If we are still processing scenarios
      # and the last step had tables, let us
      # process that table
      if prev_step_has_table == true
        steps.last.table = CucumberJunitToJson::Models::Table.parse(table)
        rows = []
        rows.push(steps.last.table.headings)
        steps.last.table.rows.each do |r|
          rows.push(r)
        end
        steps.last.rows = CucumberJunitToJson::Models::Rows.parse(rows)
        prev_step_has_table = false
        table = []
      end
      step.keyword = scenario_step.split(' ').first.strip
      step.name = scenario_step.split('...').first.split(' ').drop(1).join(' ').strip
      step.match = CucumberJunitToJson::Models::Match.new
      result_duration_str = scenario_step.split('...').last
      status, duration = result_duration_str.split('in')
      step.result = CucumberJunitToJson::Models::Result.new(status, duration)
      if failing_step && failing_step == "#{step.keyword} #{step.name}"
        step.result.error_message = failure_message
      end
      step.line = get_scenario_step_matching(scenario_title, feature_file_path, scenario_step.split('...').first.strip).last
      steps.push(step)
    elsif scenario_step.start_with?('|')
      prev_step_has_table = true
      table.push(scenario_step.to_s.strip)
    end
  end
  # if the last final step had a table
  # it means we would have not had the chance to
  # process it, hence lets do it before we exit this method
  if prev_step_has_table == true
    steps.last.table = CucumberJunitToJson::Models::Table.parse(table)
    rows = []
    rows.push(steps.last.table.headings)
    steps.last.table.rows.each do |r|
      rows.push(r)
    end
    steps.last.rows = CucumberJunitToJson::Models::Rows.parse(rows)
    prev_step_has_table = false
    table = []
  end
  steps
end