class Tapout::PerlAdapter

The TAP Legacy Adapter transforms traditional TAP format to modern TAP-Y format.

IMPORTANT: This is still very much a work in progress.

Attributes

entries[R]

We need to keep an internal list of all entries in order to create a proper footer.

Public Class Methods

new(input) click to toggle source
# File lib/tapout/adapters/perl.rb, line 13
def initialize(input)
  @input = input
  reset
end

Public Instance Methods

<<(line) click to toggle source
# File lib/tapout/adapters/perl.rb, line 52
def <<(line)
  case line
  when nil
    parse(@cache)
    @state = :footer
    finish
  when /^\d+\.\.\d+/
    #parse(@cache) if @state != :plan
    return if @head
    @head  = true
    @state = :plan
    @cache << line
  when /^ok/
    parse(@cache) #if @state != :ok
    @state = :ok
    @cache << line
  when /^not\ ok/
    parse(@cache) #if @state != :not_ok
    @state = :not_ok
    @cache << line
  when /^\#/
    parse(@cache) if @state != :comment
    @state = :comment
    @cache << line
  else
    @cache << line
  end
end
finish() click to toggle source
# File lib/tapout/adapters/perl.rb, line 122
def finish
  output(make_footer)
  case @out
  when String, IO
    @out << '...'
  end
end
output(entry) click to toggle source
# File lib/tapout/adapters/perl.rb, line 111
def output(entry)
  @entries << entry
  case @out
  when String, IO
    @out << entry.to_yaml
  else
    @out << entry
  end
end
parse(cache) click to toggle source
# File lib/tapout/adapters/perl.rb, line 82
def parse(cache)
  case @state
  when nil
    return
  when :plan
    line = cache[0]
    md = /^(\d+)\.\.(\d+)\s*$/.match(line)
    count = md[2].to_i - md[1].to_i + 1
    entry = {'count'=>count, 'type'=>'suite', 'rev'=>REVISION}
  when :ok
    line = cache[0]
    md = /^ok\s+(\d+)\s*\-?\s*(.*?)($|#)/.match(line)
    entry = {'type'=>'test','status'=>'pass','index'=>md[1].to_i, 'label'=>md[2]}
  when :not_ok
    line = cache[0]
    yaml = cache[1..-2].join('')
    yaml = unindent(yaml)
    data = YAML.load(yaml)
    md = /^not ok\s+(\d+)\s*\-?\s*(.*?)($|#)/.match(line)
    entry = convert_not_ok(md[1], md[2], data)
  when :comment
    desc = cache.map{ |c| c.sub(/^\#\s{0,1}/, '') }.join("\n")
    entry = {'type'=>'note', 'text'=>desc.rstrip}
  end
  output(entry)
  @cache = []
end
reset() click to toggle source

Reset state.

# File lib/tapout/adapters/perl.rb, line 19
def reset
  @head    = false
  @state   = nil
  @entries = []
  @cache   = []
end
to_a() click to toggle source

Route stream to an array of entires.

# File lib/tapout/adapters/perl.rb, line 36
def to_a
  self | []
end
to_s() click to toggle source

Convert input stream to TAP-Y string.

# File lib/tapout/adapters/perl.rb, line 31
def to_s
  self | ""
end
|(output) click to toggle source

Convert input stream to TAP-Y and pipe to output stream.

# File lib/tapout/adapters/perl.rb, line 41
def |(output)
  @out = output
  reset
  while line = @input.gets
    self << line
  end
  self << nil
  return @out
end

Private Instance Methods

convert_not_ok(number, label, data) click to toggle source

TODO: Any way to distinguish fail vs error?

# File lib/tapout/adapters/perl.rb, line 134
def convert_not_ok(number, label, data)
  entry = {}
  entry['type']   = 'test'
  entry['status'] = 'fail'
  entry['index']  = number.to_i
  entry['label']  = label
  if data
    entry['exception'] = {}
    entry['exception']['file']    = data['file']
    entry['exception']['line']    = data['line']
    entry['exception']['message'] = data['description']

    entry['expected'] = data['wanted']
    entry['returned'] = data['found']
    entry['source']   = data['raw_test']
    entry['extra']    = data['extensions']
  end
  entry
end
unindent(yaml) click to toggle source
# File lib/tapout/adapters/perl.rb, line 169
def unindent(yaml)
  if md = /^\s+/.match(yaml)
    re = Regexp.escape(md[0])
    yaml.gsub(/^#{re}/, '')
  else
    yaml
  end
end