Module: Teresa::Parser

Extended by:
Parser
Included in:
Parser
Defined in:
lib/teresa/parser.rb,
lib/teresa/parser/tap.rb,
lib/teresa/parser/xunit.rb,
lib/teresa/parser/generic.rb

Defined Under Namespace

Classes: Generic, TAP, XUnit

Instance Method Summary collapse

Instance Method Details

#detect(string) ⇒ Object



8
9
10
11
# File 'lib/teresa/parser.rb', line 8

def detect(string)
  constant = constants.sort.reverse.map { |c| const_get(c) }.detect { |c| c.matches? string }
  constant or raise Teresa::Error, "cannot detect test format"
end

#get(type) ⇒ Object

Raises:

  • (Teresa::Error)


13
14
15
16
17
# File 'lib/teresa/parser.rb', line 13

def get(type)
  return type if type.respond_to? :new
  raise Teresa::Error, "unknown test format #{type}" unless name = constants.detect { |c| c.to_s.downcase == type.to_s }
  const_get(name)
end

#parse(string, options = {}, &block) ⇒ Object



19
20
21
22
# File 'lib/teresa/parser.rb', line 19

def parse(string, options = {}, &block)
  parser = get(options[:type] || detect(string)).new(string)
  block  ? parser.parse(&block) : parser.enum_for(:parse).to_a
end

#parse_file(file, options = {}, &block) ⇒ Object



24
25
26
27
# File 'lib/teresa/parser.rb', line 24

def parse_file(file, options = {}, &block)
  encoding = options.fetch(:encoding) { Encoding::UTF_8 }
  parse(File.read(file, encoding: encoding), options)
end