Class: Cucumber::FeatureFile

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/feature_file.rb

Constant Summary collapse

FILE_COLON_LINE_PATTERN =
/^([\w\W]*?):([\d:]+)$/
LANGUAGE_PATTERN =
/language:\s*(.*)/

Instance Method Summary collapse

Constructor Details

#initialize(uri, source = nil) ⇒ FeatureFile

The uri argument can ba a path or a path:line1:line2 etc.



10
11
12
13
14
15
16
17
18
# File 'lib/cucumber/feature_file.rb', line 10

def initialize(uri, source=nil)
  @source = source
  _, @path, @lines = *FILE_COLON_LINE_PATTERN.match(uri)
  if @path
    @lines = @lines.split(':').map { |line| line.to_i }
  else
    @path = uri
  end
end

Instance Method Details

#langObject



44
45
46
47
48
49
50
51
# File 'lib/cucumber/feature_file.rb', line 44

def lang
  line_one = source.split(/\n/)[0]
  if line_one =~ LANGUAGE_PATTERN
    $1.strip
  else
    nil
  end
end

#parse(options = {}) ⇒ Object

Parses a file and returns a Cucumber::Ast If options contains tags, the result will be filtered.



23
24
25
26
27
# File 'lib/cucumber/feature_file.rb', line 23

def parse(options={})
  filter = Filter.new(@lines, options)
  language = Parser::I18n::Language[lang || options[:lang] || 'en']
  language.parse(source, @path, filter)
end

#sourceObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cucumber/feature_file.rb', line 29

def source
  @source ||= if @path =~ /^http/
    require 'open-uri'
    open(@path).read
  else
    begin
      File.open(@path, Cucumber.file_mode('r')).read 
    rescue Errno::EACCES => e
      p = File.expand_path(@path)
      e.message << "\nCouldn't open #{p}"
      raise e
    end
  end
end