Class: CucumberAnalytics::Step

Inherits:
Object
  • Object
show all
Includes:
Containing, Nested, Raw, Sourceable
Defined in:
lib/cucumber_analytics/step.rb

Overview

A class modeling a Cucumber Feature.

Instance Attribute Summary collapse

Attributes included from Nested

#parent_element

Attributes included from Raw

#raw_element

Attributes included from Sourceable

#source_line

Instance Method Summary collapse

Methods included from Nested

#get_ancestor

Constructor Details

#initialize(source = nil) ⇒ Step

Creates a new Step object and, if source is provided, populates the object.



28
29
30
31
32
33
34
# File 'lib/cucumber_analytics/step.rb', line 28

def initialize(source = nil)
  @arguments = []

  parsed_step = process_source(source)

  build_step(parsed_step) if parsed_step
end

Instance Attribute Details

#argumentsObject

The step’s arguments



23
24
25
# File 'lib/cucumber_analytics/step.rb', line 23

def arguments
  @arguments
end

#baseObject

The base text of the step



17
18
19
# File 'lib/cucumber_analytics/step.rb', line 17

def base
  @base
end

#blockObject

The step’s passed block



20
21
22
# File 'lib/cucumber_analytics/step.rb', line 20

def block
  @block
end

#keywordObject

The step’s keyword



14
15
16
# File 'lib/cucumber_analytics/step.rb', line 14

def keyword
  @keyword
end

Instance Method Details

#==(other_step) ⇒ Object

Returns true if the two steps have the same text, minus any keywords and arguments, and false otherwise.



69
70
71
72
73
74
75
76
# File 'lib/cucumber_analytics/step.rb', line 69

def ==(other_step)
  return false unless other_step.respond_to?(:step_text)

  left_step = step_text(:with_keywords => false, :with_arguments => false)
  right_step = other_step.step_text(:with_keywords => false, :with_arguments => false)

  left_step == right_step
end

#delimiter=(new_delimiter) ⇒ Object

Sets the delimiter that will be used by default when determining the boundaries of step arguments.



38
39
40
41
# File 'lib/cucumber_analytics/step.rb', line 38

def delimiter=(new_delimiter)
  self.left_delimiter = new_delimiter
  self.right_delimiter = new_delimiter
end

#left_delimiterObject

Returns the delimiter that is used to mark the beginning of a step argument.



45
46
47
# File 'lib/cucumber_analytics/step.rb', line 45

def left_delimiter
  @left_delimiter || World.left_delimiter
end

#left_delimiter=(new_delimiter) ⇒ Object

Sets the left delimiter that will be used by default when determining step arguments.



51
52
53
# File 'lib/cucumber_analytics/step.rb', line 51

def left_delimiter=(new_delimiter)
  @left_delimiter = new_delimiter
end

#right_delimiterObject

Returns the delimiter that is used to mark the end of a step argument.



57
58
59
# File 'lib/cucumber_analytics/step.rb', line 57

def right_delimiter
  @right_delimiter || World.right_delimiter
end

#right_delimiter=(new_delimiter) ⇒ Object

Sets the right delimiter that will be used by default when determining step arguments.



63
64
65
# File 'lib/cucumber_analytics/step.rb', line 63

def right_delimiter=(new_delimiter)
  @right_delimiter = new_delimiter
end

#scan_arguments(*how) ⇒ Object

Populates the step’s arguments based on the step’s text and some method of determining which parts of the text are arguments. Methods include using a regular expression and using the step’s delimiters.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cucumber_analytics/step.rb', line 121

def scan_arguments(*how)
  if how.count == 1
    pattern = how.first
  else
    left_delimiter = how[0] || self.left_delimiter
    right_delimiter = how[1] || self.right_delimiter

    return [] unless left_delimiter && right_delimiter

    pattern = Regexp.new(Regexp.escape(left_delimiter) + '(.*?)' + Regexp.escape(right_delimiter))
  end

  @arguments = @base.scan(pattern).flatten
end

#step_text(options = {}) ⇒ Object

Deprecated

Returns the entire text of the step. Options can be set to selectively exclude certain portions of the text. left_delimiter and right_delimiter are used to determine which parts of the step are arguments.

a_step = CucumberAnalytics::Step.new("Given *some* step with a block:\n|block line 1|\n|block line 2|")

a_step.step_text
#=> ['Given *some* step with a block:', '|block line 1|', '|block line 2|']
a_step.step_text(:with_keywords => false)
#=> ['*some* step with a block:', '|block line 1|', '|block line 2|']
a_step.step_text(:with_arguments => false, :left_delimiter => '*', :right_delimiter => '*')
#=> ['Given ** step with a block:']
a_step.step_text(:with_keywords => false, :with_arguments => false, :left_delimiter => '-', :right_delimiter => '-'))
#=> ['*some* step with a block:']


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cucumber_analytics/step.rb', line 95

def step_text(options = {})
  options = {:with_keywords => true,
             :with_arguments => true,
             :left_delimiter => self.left_delimiter,
             :right_delimiter => self.right_delimiter}.merge(options)

  final_step = []
  step_text = ''

  step_text += "#{@keyword} " if options[:with_keywords]

  if options[:with_arguments]
    step_text += @base
    final_step << step_text
    final_step.concat(rebuild_block_text(@block)) if @block
  else
    step_text += stripped_step(@base, options[:left_delimiter], options[:right_delimiter])
    final_step << step_text
  end

  final_step
end

#to_sObject

Returns a gherkin representation of the step.



137
138
139
140
141
142
# File 'lib/cucumber_analytics/step.rb', line 137

def to_s
  text = "#{keyword} #{base}"
  text << "\n" + block.to_s.split("\n").collect { |line| "  #{line}" }.join("\n") if block

  text
end