Class: Lucid::SpecFile

Inherits:
Object show all
Defined in:
lib/lucid/spec_file.rb

Constant Summary collapse

SPEC_PATTERN =

:nodoc:

/^([\w\W]*?):([\d:]+)$/
DEFAULT_ENCODING =

:nodoc:

'UTF-8'
NON_EXEC_PATTERN =

:nodoc:

/^\s*#|^\s*$/
ENCODING_PATTERN =

:nodoc:

/^\s*#\s*encoding\s*:\s*([^\s]+)/

Instance Method Summary collapse

Constructor Details

#initialize(uri, source = nil) ⇒ SpecFile

The uri argument is the location of the source. It can be a path or a path:line1:line2 etc.



15
16
17
18
19
20
21
22
23
# File 'lib/lucid/spec_file.rb', line 15

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

Instance Method Details

#parse(specified_filters, tag_counts) ⇒ Object

The parse action will parse a specific spec source and will return a the high level construct of the spec. If any filters are passed in, the result will be filtered accordingly.

See Also:

  • Runtime::SpecsLoader.load


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lucid/spec_file.rb', line 29

def parse(specified_filters, tag_counts)
  filters = @lines || specified_filters

  spec_builder         = Lucid::Parser::SpecBuilder.new(@path)
  filter_formatter    = filters.empty? ? spec_builder : Gherkin::Formatter::FilterFormatter.new(spec_builder, filters)
  tag_count_formatter = Gherkin::Formatter::TagCountFormatter.new(filter_formatter, tag_counts)

  # Gherkin Parser parameters:
  # formatter, raise_on_error, machine_name, force_ruby
  # The machine name refers to a state machine table.
  parser              = Gherkin::Parser::Parser.new(tag_count_formatter, true, 'root', false)

  begin
    # parse parameters:
    # gherkin, feature_uri, line_offset
    parser.parse(source, @path, 0)
    spec_builder.language = parser.i18n_language
    spec_builder.result
    rescue Gherkin::Lexer::LexingError => e
    handle_lexing_error(e)
  rescue Gherkin::Parser::ParseError => e
    handle_parsing_error(e)
  end
end

#sourceObject

The source method is used to return a properly encoded spec file. If the spec source read in declares a different encoding, then this method will make sure to use the default encoding of Lucid.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/lucid/spec_file.rb', line 57

def source
  @source ||= if @path =~ /^http/
    require 'open-uri'
    open(@path).read
  else
    begin
      source = File.open(@path, Lucid.file_mode('r', DEFAULT_ENCODING)).read
      encoding = encoding_for(source)
      if(DEFAULT_ENCODING.downcase != encoding.downcase)
        source = File.open(@path, Lucid.file_mode('r', encoding)).read
        source = to_default_encoding(source, encoding)
      end
      source
    rescue Errno::EACCES => e
      e.message << "\nLucid was unable to access #{File.expand_path(@path)}."
      raise e
    rescue Errno::ENOENT => e
      if @path == 'specs'
        STDOUT.puts ["\nYou don't have a 'specs' directory. This is the default specification",
                     'directory that Lucid will use if one is not specified. So either create',
                     "that directory or specify where your test repository is located.\n\n"].join("\n")
      else
        STDOUT.puts ["\nThere is no '#{@path}' directory. Since that is what you specified as",
                     'your spec repository, this directory must be present.'].join("\n")
      end
      Kernel.exit(1)
    end
  end
end