Class: Tapout::PerlAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/tapout/adapters/perl.rb

Overview

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ PerlAdapter

Returns a new instance of PerlAdapter.



13
14
15
16
# File 'lib/tapout/adapters/perl.rb', line 13

def initialize(input)
  @input = input
  reset
end

Instance Attribute Details

#entriesObject (readonly)

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



28
29
30
# File 'lib/tapout/adapters/perl.rb', line 28

def entries
  @entries
end

Instance Method Details

#<<(line) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#convert_not_ok(number, label, data) ⇒ Object (private)

TODO: Any way to distinguish fail vs error?



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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

#finishObject



122
123
124
125
126
127
128
# File 'lib/tapout/adapters/perl.rb', line 122

def finish
  output(make_footer)
  case @out
  when String, IO
    @out << '...'
  end
end


155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/tapout/adapters/perl.rb', line 155

def make_footer
  groups = @entries.group_by{ |e| e['status'] }

  entry = {}
  entry['type'] = 'final'
  entry['counts'] = {
    'total' => @count,
    'pass'  => (groups['pass'] || []).size,
    'fail'  => (groups['fail'] || []).size
  }
  entry
end

#output(entry) ⇒ Object



111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#resetObject

Reset state.



19
20
21
22
23
24
# File 'lib/tapout/adapters/perl.rb', line 19

def reset
  @head    = false
  @state   = nil
  @entries = []
  @cache   = []
end

#to_aObject

Route stream to an array of entires.



36
37
38
# File 'lib/tapout/adapters/perl.rb', line 36

def to_a
  self | []
end

#to_sObject

Convert input stream to TAP-Y string.



31
32
33
# File 'lib/tapout/adapters/perl.rb', line 31

def to_s
  self | ""
end

#unindent(yaml) ⇒ Object (private)



169
170
171
172
173
174
175
176
# 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

#|(output) ⇒ Object

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



41
42
43
44
45
46
47
48
49
# 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