Initial commit.

This commit is contained in:
Luka Vandervelden 2019-08-08 11:00:10 +02:00
commit bb64ff99ac
4 changed files with 88 additions and 0 deletions

10
shard.yml Normal file
View File

@ -0,0 +1,10 @@
name: tap
version: 0.1.0
authors:
- Luka Vandervelden <lukc@upyum.com>
description: |
A small library to parse TAP outputs.
license: MIT

68
src/tap.cr Normal file
View File

@ -0,0 +1,68 @@
class Tap::ParserError < Exception
end
class Tap::Entry
enum Status
Ok
NotOk
def self.new(s : String)
if s == "ok"
Ok
elsif s == "not ok"
NotOk
else
raise ParserError.new "Invalid TAP status: '#{s}'"
end
end
end
getter id : Int32
getter status : Status
getter title : String
getter comment : String?
def initialize(@status, @id, @title, @comment = nil)
end
end
class Tap::Suite < Array(Tap::Entry)
end
module Tap
def self.parse(text : String)
tap : Tap::Suite? = nil
text.lines.each do |line|
md = line.match(/^([0-9]+)\.\.([0-9]+)$/)
if md
unless tap
tap = Tap::Suite.new md[2].to_i
else
if (tap.size+1) != md[2].to_i
raise ParserError.new "Number of tests parsed does not match number of tests written in suite."
end
end
next
end
md = line.match(/^(not ok|ok) ([0-9]+) *- *([^#]*)(#.*)?$/)
if md
unless tap
tap = Tap::Suite.new
end
tap << Tap::Entry.new Entry::Status.new(md[1]), md[2].to_i, md[3], md[4]?.try(&.gsub /^# */, "")
next
end
raise "oh no"
end
tap
end
end

5
test.cr Normal file
View File

@ -0,0 +1,5 @@
require "./src/tap.cr"
pp! Tap.parse File.read "test.tap"

5
test.tap Normal file
View File

@ -0,0 +1,5 @@
1..4
ok 1 - Input file opened
not ok 2 - First line of the input valid
ok 3 - Read the rest of the file
not ok 4 - Summarized correctly # TODO Not written yet