Permalink
Browse files

A formatter which dumps out JSON.

Potentially very handy for debugging or for IPC.  Could replace Test::Harness.
  • Loading branch information...
1 parent 0b093ee commit cf8b0873d1c8749c87e4ff0ab7e455f3ff07c283 @schwern schwern committed Apr 23, 2013
Showing with 125 additions and 0 deletions.
  1. +84 −0 lib/TB2/Formatter/JSON.pm
  2. +41 −0 t/Formatter/JSON.t
View
@@ -0,0 +1,84 @@
+package TB2::Formatter::JSON;
+
+use TB2::Mouse;
+extends "TB2::Formatter";
+
+
+=head1 NAME
+
+TB2::Formatter::JSON - Output event objects as a JSON list
+
+=head1 DESCRIPTION
+
+This formatter outputs all events as a list of JSON items.
+
+This is useful for debugging or as an interprocess communication
+mechanism. The reader of the JSON stream will have all the same
+information as an event handler does.
+
+Set the TB2_FORMATTER_CLASS environment variable to
+TB2::Formatter::JSON.
+
+=head1 NOTES
+
+Requires JSON::PP which is not a requirement of Test::More. This
+module will likely be split out of the Test-Simple distribution. If
+you use it, be sure to declare it.
+
+=cut
+
+{
+ my $json;
+ sub json {
+ require JSON::PP;
+ $json ||= JSON::PP->new
+ ->utf8
+ ->pretty
+ ->allow_unknown
+ ->allow_blessed;
@AndyA

AndyA Apr 24, 2013

Contributor

Does it need to allow_blessed? That implies that it's not pure JSON on the wire.

@schwern

schwern Apr 24, 2013

Owner

A handful of events have objects in them, like TB2::Event::SubtestEnd has a TB2::History and TB2::Result object. Not sure how to deal with that more gracefully. That may be the only exception. Everything in TB2::Event::SubtestEnd->history and ->result can be derived from the event stream.

@schwern

schwern Apr 24, 2013

Owner

I'm going to make as_hash go recursive and add an as_hash method to TB2::History.

+
+ return $json;
+ }
+}
+
+sub handle_test_start {
+ my $self = shift;
+ my($event, $ec) = @_;
+
+ $self->write(out => "[\n");
+ $self->_event2json($event);
+
+ return;
+}
+
+sub handle_test_end {
+ my $self = shift;
+ my($event, $ec) = @_;
+
+ $self->write(out => ",\n");
+ $self->_event2json($event);
+ $self->write(out => "]\n");
+
+ return;
+}
+
+sub handle_event {
+ my $self = shift;
+ my($event, $ec) = @_;
+
+ $self->write(out => ",\n");
+ $self->_event2json($event);
+
+ return;
+}
+
+sub _event2json {
+ my $self = shift;
+ my($event) = @_;
+
+ $self->write(out => $self->json->encode($event->as_hash) );
+
+ return;
+}
+
+1;
View
@@ -0,0 +1,41 @@
+#!/usr/bin/perl -w
+
+use strict;
+use lib 't/lib';
+
+BEGIN {
+ require "t/test.pl";
+ plan(skip_all => "JSON::PP required") unless eval { require JSON::PP };
+}
+use MyEventCoordinator;
+use TB2::Events;
+
+use JSON::PP;
+
+use_ok 'TB2::Formatter::JSON';
+
+my $formatter = TB2::Formatter::JSON->new(
+ streamer_class => 'TB2::Streamer::Debug'
+);
+
+my $ec = MyEventCoordinator->new(
+ formatters => [$formatter]
+);
+
+{
+ my @events = (
+ TB2::Event::TestStart->new,
+ TB2::Event::SetPlan->new( asserts_expected => 2 ),
+ TB2::Result->new_result( pass => 1 ),
+ TB2::Result->new_result( pass => 0 ),
+ TB2::Event::TestEnd->new,
+ );
+
+ $ec->post_event($_) for @events;
+
+ my $json = $formatter->streamer->read;
+
+ is_deeply decode_json($json), [map { $_->as_hash } @events];
+}
+
+done_testing;

0 comments on commit cf8b087

Please sign in to comment.