Skip to content

Commit

Permalink
Cleaning up logging and cleaning up StepDefinitionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Webber committed Feb 16, 2013
1 parent ab080bc commit bbd6ac2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 71 deletions.
100 changes: 49 additions & 51 deletions lib/cucumber/city_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module Cucumber
module Parser
class CityBuilder
include Gherkin::Rubify

#
# The Gherkin Parser is going to call the various methods within this
# class as it finds items. This is similar to how Cucumber generates
# it's Abstract Syntax Tree (AST). Here instead this generates the
# various YARD::CodeObjects defined within this template.
#
#
# A namespace is specified and that is the place in the YARD namespacing
# where all cucumber features generated will reside. The namespace specified
# is the root namespaces.
#
#
# @param [String] file the name of the file which the content belongs
#
#
def initialize(file)
@namespace = YARD::CodeObjects::Cucumber::CUCUMBER_NAMESPACE
find_or_create_namespace(file)
Expand All @@ -24,29 +24,29 @@ def initialize(file)
# Return the feature that has been defined. This method is the final
# method that is called when all the work is done. It is called by
# the feature parser to return the complete Feature object that was created
#
#
# @return [YARD::CodeObject::Cucumber::Feature] the completed feature
#
#
# @see YARD::Parser::Cucumber::FeatureParser
def ast
@feature
end

#
# Feature that are found in sub-directories are considered, in the way
# that I chose to implement it, in another namespace. This is because
# when you execute a cucumber test run on a directory any sub-directories
# of features will be executed with that directory so the file is split
# and then namespaces are generated if they have not already have been.
#
#
# The other duty that this does is look for a README.md file within the
# specified directory of the file and loads it as the description for the
# namespace. This is useful if you want to give a particular directory
# some flavor or text to describe what is going on.
#
#
def find_or_create_namespace(file)
@namespace = YARD::CodeObjects::Cucumber::CUCUMBER_NAMESPACE

File.dirname(file).split('/').each do |directory|
@namespace = @namespace.children.find {|child| child.is_a?(YARD::CodeObjects::Cucumber::FeatureDirectory) && child.name.to_s == directory } ||
@namespace = YARD::CodeObjects::Cucumber::FeatureDirectory.new(@namespace,directory) {|dir| dir.add_file(directory)}
Expand All @@ -56,20 +56,20 @@ def find_or_create_namespace(file)
@namespace.description = File.read("#{File.dirname(file)}/README.md")
end
end

#
# Find the tag if it exists within the YARD Registry, if it doesn' t then
# create it.
#
# create it.
#
# We note that the tag was used in this file at the current line.
#
#
# Then we add the tag to the current scenario or feature. We also add the
# feature or scenario to the tag.
#
#
# @param [String] tag_name the name of the tag
# @param [parent] parent the scenario or feature that is going to adopt
# this tag.
#
#
def find_or_create_tag(tag_name,parent)
#log.debug "Processing tag #{tag_name}"
tag_code_object = YARD::Registry.all(:tag).find {|tag| tag.value == tag_name } ||
Expand All @@ -80,15 +80,15 @@ def find_or_create_tag(tag_name,parent)
parent.tags << tag_code_object unless parent.tags.find {|tag| tag == tag_code_object }
tag_code_object.owners << parent unless tag_code_object.owners.find {|owner| owner == parent}
end

#
# Each feature found will call this method, generating the feature object.
# This is once, as the gherking parser does not like multiple feature per
# file.
#
#
def feature(feature)
#log.debug "FEATURE"

@feature = YARD::CodeObjects::Cucumber::Feature.new(@namespace,File.basename(@file.gsub('.feature','').gsub('.','_'))) do |f|
f.comments = feature.comments.map{|comment| comment.value}.join("\n")
f.description = feature.description
Expand All @@ -100,14 +100,14 @@ def feature(feature)
feature.tags.each {|feature_tag| find_or_create_tag(feature_tag.name,f) }
end
end

#
# Called when a background has been found
#
#
# @see #scenario
def background(background)
#log.debug "BACKGROUND"

@background = YARD::CodeObjects::Cucumber::Scenario.new(@feature,"background") do |b|
b.comments = background.comments.map{|comment| comment.value}.join("\n")
b.description = background.description
Expand All @@ -120,20 +120,20 @@ def background(background)
@background.feature = @feature
@step_container = @background
end

#
# Called when a scenario has been found
# - create a scenario
# - assign the scenario to the feature
# - assign the feature to the scenario
# - find or create tags associated with the scenario
#
#
# The scenario is set as the @step_container, which means that any steps
# found before another scenario is defined belong to this scenario
#
#
# @param [Scenario] statement is a scenario object returned from Gherkin
# @see #find_or_create_tag
#
#
def scenario(statement)
#log.debug "SCENARIO"

Expand All @@ -151,17 +151,17 @@ def scenario(statement)
@feature.scenarios << scenario
@step_container = scenario
end

#
# Called when a scenario outline is found. Very similar to a scenario,
# the ScenarioOutline is still a distinct object as it can contain
# multiple different example groups that can contain different values.
#
#
# @see #scenario
#
#
def scenario_outline(statement)
#log.debug "SCENARIO OUTLINE"

outline = YARD::CodeObjects::Cucumber::ScenarioOutline.new(@feature,"scenario_#{@feature.scenarios.length + 1}") do |s|
s.comments = statement.comments.map{|comment| comment.value}.join("\n")
s.description = statement.description
Expand All @@ -182,18 +182,18 @@ def scenario_outline(statement)
# from the Cucumber parser because here each of the examples are exploded
# out here as individual scenarios and step definitions. This is so that
# later we can ensure that we have all the variations of the scenario
# outline defined to be displayed.
#
# outline defined to be displayed.
#
def examples(examples)
#log.debug "EXAMPLES"

example = YARD::CodeObjects::Cucumber::ScenarioOutline::Examples.new(:keyword => examples.keyword,
:name => examples.name,
:line => examples.line,
:comments => examples.comments.map{|comment| comment.value}.join("\n"),
:rows => matrix(examples.rows))


# add the example to the step containers list of examples

@step_container.examples << example
Expand All @@ -202,7 +202,7 @@ def examples(examples)
# current scenario as the template.

example.data.length.times do |row_index|

# Generate a copy of the scenario.

scenario = YARD::CodeObjects::Cucumber::Scenario.new(@step_container,"example_#{@step_container.scenarios.length + 1}") do |s|
Expand Down Expand Up @@ -234,32 +234,32 @@ def examples(examples)
step_instance.text.gsub!("<#{key}>",text) if step_instance.has_text?
step_instance.table.each{|row| row.each{|col| col.gsub!("<#{key}>",text)}} if step_instance.has_table?
end

# Connect these steps that we created to the scenario we created
# and then add the steps to the scenario created.

step_instance.scenario = scenario
scenario.steps << step_instance
end

# Add the scenario to the list of scenarios maintained by the feature
# Add the scenario to the list of scenarios maintained by the feature
# and add the feature to the scenario

scenario.feature = @feature
@step_container.scenarios << scenario

end

end

#
#
# Called when a step is found. The step is refered to a table owner, though
# not all steps have a table or multliline arguments associated with them.
#
#
# If a multiline string is present with the step it is included as the text
# of the step. If the step has a table it is added to the step using the
# same method used by the Cucumber Gherkin model.
#
#
def step(step)
#log.debug "STEP"

Expand All @@ -279,16 +279,14 @@ def step(step)
rubify(step.doc_string)
end

log.debug "Step: #{multiline_arg}"

case(multiline_arg)
when gherkin_multiline_string_class
@table_owner.text = multiline_arg.value
when Array
#log.info "Matrix: #{matrix(multiline_arg).collect{|row| row.collect{|cell| cell.class } }.flatten.join("\n")}"
@table_owner.table = matrix(multiline_arg)
end

@table_owner.scenario = @step_container
@step_container.steps << @table_owner
end
Expand All @@ -306,11 +304,11 @@ def syntax_error(state, event, legal_events, line)
def matrix(gherkin_table)
gherkin_table.map {|gherkin_row| gherkin_row.cells }
end

#
# This helper method is used to deteremine what class is the current
# Gherkin class.
#
#
# @return [Class] the class that is the current supported Gherkin Model
# for multiline strings. Prior to Gherkin 2.4.0 this was the PyString
# class. As of Gherkin 2.4.0 it is the DocString class.
Expand All @@ -327,7 +325,7 @@ def gherkin_multiline_string_class
def clone_table(base)
base.map {|row| row.map {|cell| cell.dup }}
end

end
end
end
40 changes: 21 additions & 19 deletions lib/yard/code_objects/step_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,55 @@ class StepTransformerObject < Base

include Cucumber::LocationHelper

attr_reader :constants, :keyword, :source, :value, :literal_value, :pending
attr_accessor :steps
attr_reader :constants, :keyword, :source, :value, :literal_value
attr_accessor :steps, :pending

# This defines an escape pattern within a string or regex:
# /^the first #{CONSTANT} step$/
#
#
# This is used below in the value to process it if there happen to be
# constants defined here.
#
#
# @note this does not handle the result of method calls
# @note this does not handle multiple constants within the same escaped area
#
ESCAPE_PATTERN = /#\{\s*(\w+)\s*\}/ unless defined?(ESCAPE_PATTERN)
#
def escape_pattern
/#\{\s*(\w+)\s*\}/
end

#
# When requesting a step tranformer object value, process it, if it hasn't
# alredy been processed, replacing any constants that may be lurking within
# the value.
#
#
# Processing it means looking for any escaped characters that happen to be
# CONSTANTS that could be matched and then replaced. This is done recursively
# as CONSTANTS can be defined with more CONSTANTS.
#
#
def value
unless @processed
@processed = true
until (nested = constants_from_value).empty?
nested.each {|n| @value.gsub!(value_regex(n),find_value_for_constant(n)) }
end
end

@value
end

#
# Set the literal value and the value of the step definition.
#
#
# The literal value is as it appears in the step definition file with any
# constants. The value, when retrieved will attempt to replace those
# constants. The value, when retrieved will attempt to replace those
# constants with their regex or string equivalents to hopefully match more
# steps and step definitions.
#
#
#
#
def value=(value)
@literal_value = format_source(value)
@value = format_source(value)

@steps = []
value
end
Expand All @@ -60,27 +62,27 @@ def value=(value)
def regex
@regex ||= /#{strip_regex_from(value)}/
end

# Look through the specified data for the escape pattern and return an array
# of those constants found. This defaults to the @value within step transformer
# as it is used internally, however, it can be called externally if it's
# needed somewhere.
def constants_from_value(data=@value)
data.scan(ESCAPE_PATTERN).flatten.collect { |value| value.strip }
data.scan(escape_pattern).flatten.collect { |value| value.strip }
end

protected

#
# Looking through all the constants in the registry and returning the value
# with the regex items replaced from the constnat if present
#
#
def find_value_for_constant(name)
constant = YARD::Registry.all(:constant).find{|c| c.name == name.to_sym }
log.warn "StepTransformer#find_value_for_constant : Could not find the CONSTANT [#{name}] using the string value." unless constant
constant ? strip_regex_from(constant.value) : name
end

# Return a regex of the value
def value_regex(value)
/#\{\s*#{value}\s*\}/
Expand Down
1 change: 0 additions & 1 deletion lib/yard/handlers/step_definition_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def pending_keyword
end

def pending_command_statement?(line)
puts "#{line.type} #{line.source}"
(line.type == :command || line.type == :vcall) && line.first.source == pending_keyword
end

Expand Down

0 comments on commit bbd6ac2

Please sign in to comment.