class CukeModeler::Step

A class modeling a single step of a background, scenario, or outline.

Attributes

block[RW]

The step's passed block

keyword[RW]

The step's keyword

text[RW]

The base text of the step

Public Class Methods

new(source_text = nil) click to toggle source

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

Calls superclass method CukeModeler::Model::new
# File lib/cuke_modeler/models/step.rb, line 23
def initialize(source_text = nil)
  super(source_text)

  return unless source_text

  parsed_step_data = parse_source(source_text)
  populate_step(self, parsed_step_data)
end

Public Instance Methods

==(other) click to toggle source

Returns true if the two steps have the same base text (i.e. minus any keyword, table, or doc string and false otherwise.

# File lib/cuke_modeler/models/step.rb, line 34
def ==(other)
  return false unless other.is_a?(CukeModeler::Step)

  text_matches?(other) &&
    table_matches?(other) &&
    doc_string_matches?(other)
end
children() click to toggle source

Returns the model objects that belong to this model.

# File lib/cuke_modeler/models/step.rb, line 43
def children
  block ? [block] : []
end
to_s() click to toggle source

Returns a string representation of this model. For a step model, this will be Gherkin text that is equivalent to the step being modeled.

# File lib/cuke_modeler/models/step.rb, line 49
def to_s
  text = "#{keyword} #{self.text}"
  text << "\n#{block.to_s.split("\n").collect { |line| "  #{line}" }.join("\n")}" if block

  text
end

Private Instance Methods

doc_string_matches?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 85
def doc_string_matches?(other_step)
  return false if only_one_step_has_doc_string?(other_step)
  return true if neither_step_has_doc_string?(other_step)

  first_content       = block.content
  first_content_type  = block.content_type
  second_content      = other_step.block.content
  second_content_type = other_step.block.content_type

  (first_content == second_content) &&
    (first_content_type == second_content_type)
end
neither_step_has_doc_string?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 113
def neither_step_has_doc_string?(other_step)
  !block.is_a?(CukeModeler::DocString) &&
    !other_step.block.is_a?(CukeModeler::DocString)
end
neither_step_has_table?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 103
def neither_step_has_table?(other_step)
  !block.is_a?(CukeModeler::Table) &&
    !other_step.block.is_a?(CukeModeler::Table)
end
only_one_step_has_doc_string?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 108
def only_one_step_has_doc_string?(other_step)
  (!block.is_a?(CukeModeler::DocString) || !other_step.block.is_a?(CukeModeler::DocString)) &&
    (block.is_a?(CukeModeler::DocString) || other_step.block.is_a?(CukeModeler::DocString))
end
only_one_step_has_table?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 98
def only_one_step_has_table?(other_step)
  (!block.is_a?(CukeModeler::Table) || !other_step.block.is_a?(CukeModeler::Table)) &&
    (block.is_a?(CukeModeler::Table) || other_step.block.is_a?(CukeModeler::Table))
end
parse_source(source_text) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 60
def parse_source(source_text)
  base_file_string = "# language: #{Parsing.dialect}
  #{dialect_feature_keyword}: Fake feature to parse
                        #{dialect_scenario_keyword}:\n"
  source_text = base_file_string + source_text

  parsed_file = Parsing.parse_text(source_text, 'cuke_modeler_stand_alone_step.feature')

  parsed_file['feature']['elements'].first['steps'].first
end
table_matches?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 75
def table_matches?(other_step)
  return false if only_one_step_has_table?(other_step)
  return true if neither_step_has_table?(other_step)

  first_step_values = block.rows.collect { |table_row| table_row.cells.map(&:value) }
  second_step_values = other_step.block.rows.collect { |table_row| table_row.cells.map(&:value) }

  first_step_values == second_step_values
end
text_matches?(other_step) click to toggle source
# File lib/cuke_modeler/models/step.rb, line 71
def text_matches?(other_step)
  text == other_step.text
end