Skip to content

Commit

Permalink
Merge pull request cucumber#888 from cucumber/remove-core-ext-proc
Browse files Browse the repository at this point in the history
Remove core_ext/proc.rb and use Proc#source_location instead
  • Loading branch information
mattwynne committed Aug 7, 2015
2 parents 65f5023 + 962d3bd commit cb25dcc
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 157 deletions.
36 changes: 0 additions & 36 deletions lib/cucumber/core_ext/proc.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/cucumber/filters/prepare_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def test_case
around_hooks = [init_scenario] + @original_test_case.around_hooks

empty_hook = proc {} #no op - legacy format adapter expects a before hooks
file, line = *empty_hook.source_location
default_hook = Cucumber::Hooks.before_hook(@original_test_case.source, Cucumber::Core::Ast::Location.new(file, line), &empty_hook)
empty_hook_location = Cucumber::Core::Ast::Location.from_source_location(*empty_hook.source_location)
default_hook = Cucumber::Hooks.before_hook(@original_test_case.source, empty_hook_location, &empty_hook)
steps = [default_hook] + @original_test_case.test_steps

@original_test_case.with_around_hooks(around_hooks).with_steps(steps)
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/formatter/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Console

def format_step(keyword, step_match, status, source_indent)
comment = if source_indent
c = ('# ' + step_match.file_colon_line).indent(source_indent)
c = ('# ' + step_match.location.to_s).indent(source_indent)
format_string(c, :comment)
else
''
Expand Down
12 changes: 6 additions & 6 deletions lib/cucumber/formatter/usage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def after_test_step(test_step, result)

step_match = @runtime.step_match(test_step.source.last.name)
step_definition = step_match.step_definition
stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.file_colon_line)
unless @stepdef_to_match[stepdef_key].map { |key| key[:file_colon_line] }.include? test_step.location
stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.location)
unless @stepdef_to_match[stepdef_key].map { |key| key[:location] }.include? test_step.location
duration = DurationExtractor.new(result).result_duration

@stepdef_to_match[stepdef_key] << {
keyword: test_step.source.last.keyword,
step_match: step_match,
status: result.to_sym,
file_colon_line: test_step.location,
location: test_step.location,
duration: duration
}
end
Expand Down Expand Up @@ -68,7 +68,7 @@ def print_step_definition(stepdef_key)
@io.print format_string(stepdef_key.regexp_source, stepdef_key.status)
if @options[:source]
indent = max_length - stepdef_key.regexp_source.unpack('U*').length
line_comment = " # #{stepdef_key.file_colon_line}".indent(indent)
line_comment = " # #{stepdef_key.location}".indent(indent)
@io.print(format_string(line_comment, :comment))
end
@io.puts
Expand All @@ -81,7 +81,7 @@ def print_steps(stepdef_key)
@io.print format_step(step[:keyword], step[:step_match], step[:status], nil)
if @options[:source]
indent = max_length - (step[:keyword].unpack('U*').length + step[:step_match].format_args.unpack('U*').length)
line_comment = " # #{step[:file_colon_line]}".indent(indent)
line_comment = " # #{step[:location]}".indent(indent)
@io.print(format_string(line_comment, :comment))
end
@io.puts
Expand Down Expand Up @@ -123,7 +123,7 @@ def worst_status(statuses)

def add_unused_stepdefs
@runtime.unmatched_step_definitions.each do |step_definition|
stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.file_colon_line)
stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.location)
@stepdef_to_match[stepdef_key] = []
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/cucumber/language_support/language_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def unmatched_step_definitions
available_step_definition_hash.keys - invoked_step_definition_hash.keys
end

def available_step_definition(regexp_source, file_colon_line)
available_step_definition_hash[StepDefinitionLight.new(regexp_source, file_colon_line)] = nil
def available_step_definition(regexp_source, location)
available_step_definition_hash[StepDefinitionLight.new(regexp_source, location)] = nil
end

def invoked_step_definition(regexp_source, file_colon_line)
invoked_step_definition_hash[StepDefinitionLight.new(regexp_source, file_colon_line)] = nil
def invoked_step_definition(regexp_source, location)
invoked_step_definition_hash[StepDefinitionLight.new(regexp_source, location)] = nil
end

private
Expand Down
7 changes: 1 addition & 6 deletions lib/cucumber/rb_support/rb_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ def initialize(rb_language, tag_expressions, proc)
@rb_language = rb_language
@tag_expressions = tag_expressions
@proc = proc
file, line = @proc.file_colon_line.match(/(.*):(\d+)/)[1..2]
@location = Core::Ast::Location.new(file, line)
end

def source_location
@proc.source_location
@location = Cucumber::Core::Ast::Location.from_source_location(*@proc.source_location)
end

def invoke(pseudo_method, arguments, &block)
Expand Down
11 changes: 8 additions & 3 deletions lib/cucumber/rb_support/rb_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class MultipleWorld < StandardError
def initialize(first_proc, second_proc)
message = "You can only pass a proc to #World once, but it's happening\n"
message << "in 2 places:\n\n"
message << first_proc.backtrace_line('World') << "\n"
message << second_proc.backtrace_line('World') << "\n\n"
message << RbSupport.backtrace_line(first_proc, 'World') << "\n"
message << RbSupport.backtrace_line(second_proc, 'World') << "\n\n"
message << "Use Ruby modules instead to extend your worlds. See the Cucumber::RbSupport::RbDsl#World RDoc\n"
message << "or http://wiki.github.com/cucumber/cucumber/a-whole-new-world.\n\n"
super(message)
Expand Down Expand Up @@ -132,7 +132,7 @@ def check_nil(o, proc)
raise NilWorld.new
rescue NilWorld => e
e.backtrace.clear
e.backtrace.push(proc.backtrace_line("World"))
e.backtrace.push(RbSupport.backtrace_line(proc, "World"))
raise e
end
else
Expand All @@ -156,5 +156,10 @@ def self.cli_snippet_type_options
end
end
end

def self.backtrace_line(proc, name)
location = Cucumber::Core::Ast::Location.from_source_location(*proc.source_location)
"#{location.to_s}:in `#{name}'"
end
end
end
28 changes: 11 additions & 17 deletions lib/cucumber/rb_support/rb_step_definition.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'cucumber/step_match'
require 'cucumber/core_ext/string'
require 'cucumber/core_ext/proc'
require 'cucumber/rb_support/regexp_argument_matcher'

module Cucumber
Expand Down Expand Up @@ -51,17 +50,8 @@ def create_proc(proc_or_sym, options)
end

def patch_location_onto(block)
file, line = caller[5].match(/(.*):(\d+)/)[1..2]
file = File.expand_path(file)
pwd = File.expand_path(Dir.pwd)
pwd.force_encoding(file.encoding)
if file.index(pwd)
file = file[pwd.length+1..-1]
elsif file =~ /.*\/gems\/(.*\.rb)$/
file = $1
end
location = Core::Ast::Location.new(file, line)
block.define_singleton_method(:file_colon_line) { location.to_s }
location = Core::Ast::Location.of_caller(5)
block.define_singleton_method(:source_location) { [location.file, location.line] }
block
end

Expand All @@ -81,7 +71,7 @@ def parse_target_proc_from(options)

def initialize(rb_language, regexp, proc)
@rb_language, @regexp, @proc = rb_language, regexp, proc
@rb_language.available_step_definition(regexp_source, file_colon_line)
@rb_language.available_step_definition(regexp_source, location)
end

def regexp_source
Expand All @@ -102,7 +92,7 @@ def ==(step_definition)

def arguments_from(step_name)
args = RegexpArgumentMatcher.arguments_from(@regexp, step_name)
@rb_language.invoked_step_definition(regexp_source, file_colon_line) if args
@rb_language.invoked_step_definition(regexp_source, location) if args
args
end

Expand All @@ -117,20 +107,24 @@ def invoke(args)
end

def backtrace_line
@proc.backtrace_line(regexp_source)
"#{location.to_s}:in `#{regexp_source}'"
end

def file_colon_line
case @proc
when Proc
@proc.file_colon_line
location.to_s
when Symbol
":#{@proc}"
end
end

def location
@location ||= Cucumber::Core::Ast::Location.from_source_location(*@proc.source_location)
end

def file
@file ||= file_colon_line.split(':')[0]
@file ||= location.file
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/cucumber/rb_support/rb_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def step(name, raw_multiline_arg=nil)
# }
# @param [String] steps_text The Gherkin snippet to run
def steps(steps_text)
@__cucumber_runtime.invoke_dynamic_steps(steps_text, @__natural_language, caller[0])
location = Core::Ast::Location.of_caller
@__cucumber_runtime.invoke_dynamic_steps(steps_text, @__natural_language, location)
end

# Parse Gherkin into a {Cucumber::Ast::Table} object.
Expand Down
5 changes: 2 additions & 3 deletions lib/cucumber/runtime/support_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ def configure(new_configuration)
# Given I have 8 cukes in my belly
# Then I should not be thirsty
# })
def invoke_dynamic_steps(steps_text, i18n, file_colon_line)
file, line = file_colon_line.split(':')
def invoke_dynamic_steps(steps_text, i18n, location)
parser = Gherkin::Parser::Parser.new(StepInvoker.new(self), true, 'steps', false, i18n.iso_code)
parser.parse(steps_text, file, line.to_i)
parser.parse(steps_text, location.file, location.line)
end

# @api private
Expand Down
10 changes: 5 additions & 5 deletions lib/cucumber/step_definition_light.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ module Cucumber
# in a way that also works for other programming languages (i.e. cuke4duke)
# Used for reporting purposes only (usage formatter).
class StepDefinitionLight
attr_reader :regexp_source, :file_colon_line
attr_reader :regexp_source, :location

def initialize(regexp_source, file_colon_line)
@regexp_source, @file_colon_line = regexp_source, file_colon_line
def initialize(regexp_source, location)
@regexp_source, @location = regexp_source, location
end

def eql?(o)
regexp_source == o.regexp_source && file_colon_line == o.file_colon_line
regexp_source == o.regexp_source && location == o.location
end

def hash
regexp_source.hash + 31*file_colon_line.hash
regexp_source.hash + 31*location.to_s.hash
end
end
end
13 changes: 11 additions & 2 deletions lib/cucumber/step_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def name
end

def activate(test_step)
test_step.with_action(@step_definition.file_colon_line) do
test_step.with_action(@step_definition.location) do
invoke(MultilineArgument.from_core(test_step.source.last.multiline_arg))
end
end
Expand Down Expand Up @@ -52,8 +52,12 @@ def format_args(format = lambda{|a| a}, &proc)
@name_to_report || replace_arguments(@name_to_match, @step_arguments, format, &proc)
end

def location
@step_definition.location
end

def file_colon_line
@step_definition.file_colon_line
location.to_s
end

def backtrace_line
Expand Down Expand Up @@ -113,6 +117,11 @@ def format_args(*args)
@name
end

def location
raise "No location for #{@step}" unless @step.location
@step.location
end

def file_colon_line
raise "No file:line for #{@step}" unless @step.file_colon_line
@step.file_colon_line
Expand Down
6 changes: 4 additions & 2 deletions lib/cucumber/wire_support/wire_step_definition.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require 'cucumber/core/ast/location'

module Cucumber
module WireSupport
class WireStepDefinition
attr_reader :regexp_source, :file_colon_line
attr_reader :regexp_source, :location

def initialize(connection, data)
@connection = connection
@id = data['id']
@regexp_source = data['regexp'] || "Unknown"
@file_colon_line = data['source'] || "Unknown"
@location = data['source'] ? Cucumber::Core::Ast::Location.from_file_colon_line(data['source']) : Cucumber::Core::Ast::Location.new("Unknown")
end

def invoke(args)
Expand Down
Loading

0 comments on commit cb25dcc

Please sign in to comment.