Class: Cucumber::RbSupport::RbLanguage

Inherits:
Object
  • Object
show all
Includes:
LanguageSupport::LanguageMethods
Defined in:
lib/cucumber/rb_support/rb_language.rb

Overview

The Ruby implementation of the programming language API.

Constant Summary collapse

ARGUMENT_PATTERNS =
['"([^"]*)"', '(\d+)']

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LanguageSupport::LanguageMethods

#add_hook, #add_transform, #after, #after_configuration, #around, #available_step_definition, #before, #clear_hooks, #create_step_match, #execute_after_step, #execute_transforms, #hooks_for, #invoked_step_definition, #unmatched_step_definitions

Constructor Details

#initialize(step_mother) ⇒ RbLanguage

Returns a new instance of RbLanguage.



41
42
43
44
45
46
47
# File 'lib/cucumber/rb_support/rb_language.rb', line 41

def initialize(step_mother)
  @step_mother = step_mother
  @step_definitions = []
  RbDsl.rb_language = self
  @world_proc = @world_modules = nil
  enable_rspec_expectations_if_available
end

Instance Attribute Details

#current_worldObject (readonly)

Returns the value of attribute current_world.



33
34
35
# File 'lib/cucumber/rb_support/rb_language.rb', line 33

def current_world
  @current_world
end

#step_definitionsObject (readonly)

Returns the value of attribute step_definitions.



33
34
35
# File 'lib/cucumber/rb_support/rb_language.rb', line 33

def step_definitions
  @step_definitions
end

Instance Method Details

#begin_rb_scenario(scenario) ⇒ Object



113
114
115
116
117
# File 'lib/cucumber/rb_support/rb_language.rb', line 113

def begin_rb_scenario(scenario)
  create_world
  extend_world
  connect_world(scenario)
end

#build_rb_world_factory(world_modules, proc) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/cucumber/rb_support/rb_language.rb', line 133

def build_rb_world_factory(world_modules, proc)
  if(proc)
    raise MultipleWorld.new(@world_proc, proc) if @world_proc
    @world_proc = proc
  end
  @world_modules ||= []
  @world_modules += world_modules
end

#enable_rspec_expectations_if_availableObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cucumber/rb_support/rb_language.rb', line 49

def enable_rspec_expectations_if_available
  begin
    # RSpec >=2.0
    require 'rspec/expectations'
    @rspec_matchers = ::RSpec::Matchers
  rescue LoadError => try_rspec_1_2_4_or_higher
    begin
      require 'spec/expectations'
      require 'spec/runner/differs/default'
      require 'ostruct'
      options = OpenStruct.new(:diff_format => :unified, :context_lines => 3)
      Spec::Expectations.differ = Spec::Expectations::Differs::Default.new(options)
      @rspec_matchers = ::Spec::Matchers
    rescue LoadError => give_up
      @rspec_matchers = Module.new{}
    end
  end
end

#load_code_file(code_file) ⇒ Object



142
143
144
# File 'lib/cucumber/rb_support/rb_language.rb', line 142

def load_code_file(code_file)
  load File.expand_path(code_file) # This will cause self.add_step_definition, self.add_hook, and self.add_transform to be called from RbDsl
end

#register_rb_hook(phase, tag_expressions, proc) ⇒ Object



119
120
121
# File 'lib/cucumber/rb_support/rb_language.rb', line 119

def register_rb_hook(phase, tag_expressions, proc)
  add_hook(phase, RbHook.new(self, tag_expressions, proc))
end

#register_rb_step_definition(regexp, proc) ⇒ Object



127
128
129
130
131
# File 'lib/cucumber/rb_support/rb_language.rb', line 127

def register_rb_step_definition(regexp, proc)
  step_definition = RbStepDefinition.new(self, regexp, proc)
  @step_definitions << step_definition
  step_definition
end

#register_rb_transform(regexp, proc) ⇒ Object



123
124
125
# File 'lib/cucumber/rb_support/rb_language.rb', line 123

def register_rb_transform(regexp, proc)
  add_transform(RbTransform.new(self, regexp, proc))
end

#snippet_text(code_keyword, step_name, multiline_arg_class) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cucumber/rb_support/rb_language.rb', line 94

def snippet_text(code_keyword, step_name, multiline_arg_class)
  snippet_pattern = Regexp.escape(step_name).gsub('\ ', ' ').gsub('/', '\/')
  arg_count = 0
  ARGUMENT_PATTERNS.each do |pattern|
    snippet_pattern = snippet_pattern.gsub(Regexp.new(pattern), pattern)
    arg_count += snippet_pattern.scan(pattern).length
  end

  block_args = (0...arg_count).map {|n| "arg#{n+1}"}
  block_args << multiline_arg_class.default_arg_name unless multiline_arg_class.nil?
  block_arg_string = block_args.empty? ? "" : " |#{block_args.join(", ")}|"
  multiline_class_comment = ""
  if(multiline_arg_class == Ast::Table)
    multiline_class_comment = "# #{multiline_arg_class.default_arg_name} is a #{multiline_arg_class.to_s}\n  "
  end

  "#{code_keyword} /^#{snippet_pattern}$/ do#{block_arg_string}\n  #{multiline_class_comment}pending # express the regexp above with the code you wish you had\nend"
end

#step_definitions_for(rb_file) ⇒ Object

Gets called for each file under features (or whatever is overridden with –require).



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cucumber/rb_support/rb_language.rb', line 70

def step_definitions_for(rb_file) # Looks Unused - Delete?
  begin
    require rb_file # This will cause self.add_step_definition and self.add_hook to be called from RbDsl
    step_definitions
  rescue LoadError => e
    e.message << "\nFailed to load #{code_file}"
    raise e
  ensure
    @step_definitions = nil
  end
end

#step_matches(name_to_match, name_to_format) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/cucumber/rb_support/rb_language.rb', line 82

def step_matches(name_to_match, name_to_format)
  @step_definitions.map do |step_definition|
    if(arguments = step_definition.arguments_from(name_to_match))
      StepMatch.new(step_definition, name_to_match, name_to_format, arguments)
    else
      nil
    end
  end.compact
end