Class: Teresa::Parser::TAP

Inherits:
Generic
  • Object
show all
Defined in:
lib/teresa/parser/tap.rb

Overview

Implements Test Anything Protocol and Knock test output format.

Instance Method Summary collapse

Methods inherited from Generic

matches, matches?

Constructor Details

#initialize(string) ⇒ TAP

Returns a new instance of TAP.



10
11
12
# File 'lib/teresa/parser/tap.rb', line 10

def initialize(string)
  @string  = string
end

Instance Method Details

#parseObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/teresa/parser/tap.rb', line 14

def parse
  scanner  = StringScanner.new(@string)
  version  = scanner.scan(/TAP version (\d+)\n/) ? scanner[1].to_i : 12
  maximum  = scanner.scan(/1\.\.(\d+)\n/) && scanner[1]
  expected = 0

  raise "TAP #{version} is not supported" unless version.between? 12, 13

  while scanner.scan_until(/^(not )?ok\b/)
    expected    += 1
    test         = Test.new
    test.state   = :failed if scanner[1]
    test.id      = scanner.scan(/ +(\d+)/) ? scanner[1].to_i : expected
    test.message = scanner.scan(/ +.+/).to_s.gsub(/^\s*-|-\s*$/, '').strip
    test.name    = test.message.gsub(/(\: FAILED)?(#.*)?$/, '')
    test.state   = :skipped if test.message =~ /# *(SKIP|TODO)/i
    expected     = test.id
    test.name    = "Test ##{test.id}" if test.name.empty?
    test.message = nil if test.message.empty?

    if version >= 13 and scanner.scan(/^([ \t]+)---\n$/)
      test.output = "meta data:\n\n\t---\n"
      prefix      = scanner[1]

      while scanner.scan(/^#{prefix}(.*)\n/)
        test.output << scanner[1]
        test.message = scanner[1][/message: *"?(.*)"?/, 1] if scanner[1].start_with? "message:"
      end

      test.output << "\n"
    end

    while scanner.scan(/^# ?(.*\n)/)
      test.output ||= ""
      test.output << scanner[1]
    end

    yield test
  end

  unless scanner.scan_until(/^Bail out!\b/)
    maximum ||= scanner.scan_until(/1\.\.(\d+)\n/) && scanner[1]
    expected.upto(maximum.to_i) { |i| yield Test.new(state: :failed, id: i, name: "Test ##{i}") }
  end
end