From bb64ff99acac365b9232032ea3a70b3430be935e Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Thu, 8 Aug 2019 11:00:10 +0200 Subject: [PATCH] Initial commit. --- shard.yml | 10 ++++++++ src/tap.cr | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.cr | 5 ++++ test.tap | 5 ++++ 4 files changed, 88 insertions(+) create mode 100644 shard.yml create mode 100644 src/tap.cr create mode 100644 test.cr create mode 100644 test.tap diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..cfb6011 --- /dev/null +++ b/shard.yml @@ -0,0 +1,10 @@ +name: tap +version: 0.1.0 + +authors: + - Luka Vandervelden + +description: | + A small library to parse TAP outputs. + +license: MIT diff --git a/src/tap.cr b/src/tap.cr new file mode 100644 index 0000000..e540b3d --- /dev/null +++ b/src/tap.cr @@ -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 + diff --git a/test.cr b/test.cr new file mode 100644 index 0000000..df88820 --- /dev/null +++ b/test.cr @@ -0,0 +1,5 @@ + +require "./src/tap.cr" + +pp! Tap.parse File.read "test.tap" + diff --git a/test.tap b/test.tap new file mode 100644 index 0000000..a0e46dd --- /dev/null +++ b/test.tap @@ -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