class CucumberAnalytics::Step

A class modeling a Cucumber Feature.

Attributes

arguments[RW]

The step's arguments

base[RW]

The base text of the step

block[RW]

The step's passed block

keyword[RW]

The step's keyword

Public Class Methods

new(source = nil) click to toggle source

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

# 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

Public Instance Methods

==(other_step) click to toggle source

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

# 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) click to toggle source

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

# File lib/cucumber_analytics/step.rb, line 38
def delimiter=(new_delimiter)
  self.left_delimiter = new_delimiter
  self.right_delimiter = new_delimiter
end
left_delimiter() click to toggle source

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

# File lib/cucumber_analytics/step.rb, line 45
def left_delimiter
  @left_delimiter || World.left_delimiter
end
left_delimiter=(new_delimiter) click to toggle source

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

# File lib/cucumber_analytics/step.rb, line 51
def left_delimiter=(new_delimiter)
  @left_delimiter = new_delimiter
end
right_delimiter() click to toggle source

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

# File lib/cucumber_analytics/step.rb, line 57
def right_delimiter
  @right_delimiter || World.right_delimiter
end
right_delimiter=(new_delimiter) click to toggle source

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

# File lib/cucumber_analytics/step.rb, line 63
def right_delimiter=(new_delimiter)
  @right_delimiter = new_delimiter
end
scan_arguments(*how) click to toggle source

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.

# 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 = {}) click to toggle source

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:']
# 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_s() click to toggle source

Returns a gherkin representation of the step.

# 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

Private Instance Methods

build_block(step) click to toggle source
# File lib/cucumber_analytics/step.rb, line 198
def build_block(step)
  case
    when step['rows']
      @block = build_child_element(Table, step['rows'])
    when step['doc_string']
      @block = build_child_element(DocString, step['doc_string'])
    else
      @block = nil
  end

  @block
end
build_step(step) click to toggle source
# File lib/cucumber_analytics/step.rb, line 166
def build_step(step)
  populate_base(step)
  populate_block(step)
  populate_keyword(step)
  populate_element_source_line(step)
  populate_raw_element(step)

  scan_arguments
end
parse_step(source_text) click to toggle source
# File lib/cucumber_analytics/step.rb, line 157
def parse_step(source_text)
  base_file_string = "Feature: Fake feature to parse\nScenario:\n"
  source_text = base_file_string + source_text

  parsed_file = Parsing::parse_text(source_text)

  parsed_file.first['elements'].first['steps'].first
end
populate_base(step) click to toggle source
# File lib/cucumber_analytics/step.rb, line 176
def populate_base(step)
  @base = step['name']
end
populate_block(step) click to toggle source
# File lib/cucumber_analytics/step.rb, line 180
def populate_block(step)
  @block = build_block(step)
end
populate_keyword(step) click to toggle source
# File lib/cucumber_analytics/step.rb, line 184
def populate_keyword(step)
  @keyword = step['keyword'].strip
end
process_source(source) click to toggle source
# File lib/cucumber_analytics/step.rb, line 148
def process_source(source)
  case
    when source.is_a?(String)
      parse_step(source)
    else
      source
  end
end
rebuild_block_text(blok) click to toggle source
# File lib/cucumber_analytics/step.rb, line 211
def rebuild_block_text(blok)
  blok.contents.collect { |row| "|#{row.join('|')}|" }
end
stripped_step(step, left_delimiter, right_delimiter) click to toggle source

Returns the step string minus any arguments based on the given delimiters.

# File lib/cucumber_analytics/step.rb, line 189
def stripped_step(step, left_delimiter, right_delimiter)
  unless left_delimiter.nil? || right_delimiter.nil?
    pattern = Regexp.new(Regexp.escape(left_delimiter) + '.*?' + Regexp.escape(right_delimiter))
    step = step.gsub(pattern, left_delimiter + right_delimiter)
  end

  step
end