Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add TB2::Event::Generic.

A generic event bucket useful for reconstituting events from serialization.

It will accept anything and turn it into an attribute.

This isn't 100% for all events.  For example, subtest_end has a history
object and a result.  Might have to add in some special cases later.
  • Loading branch information...
commit 0cdaa56609a98bd84f970eac4b69a2ecf69cfda8 1 parent 2a3dacc
@schwern schwern authored
Showing with 100 additions and 0 deletions.
  1. +62 −0 lib/TB2/Event/Generic.pm
  2. +38 −0 t/Event/Generic.t
View
62 lib/TB2/Event/Generic.pm
@@ -0,0 +1,62 @@
+package TB2::Event::Generic;
+
+use strict;
+use warnings;
+
+use Carp;
+use TB2::Mouse;
+with 'TB2::Event';
+
+
+=head1 NAME
+
+TB2::Event::Generic - A container for any type of event
+
+=head1 SYNOPSIS
+
+ use TB2::Event::Generic;
+
+ my $event = TB2::Event::Generic->new( $event->as_hash );
+
+=head1 DESCRIPTION
+
+This is a container for any type of event. Its primary purpose is to
+receive events serialized using C<< TB2::Event->as_hash >>.
+
+All attributes are read only.
+
+=head1 SEE ALSO
+
+See L<TB2::Formatter::JSON> for an example of use.
+
+=cut
+
+
+sub build_event_type {
+ croak("The event_type must be defined in the constructor");
+}
+
+# Ensure that all attributes are dumped via as_hash
+my @Attributes = grep !/^_/, map { $_->name } __PACKAGE__->meta->get_all_attributes;
+sub keys_for_as_hash {
+ my $self = shift;
+
+ return \@Attributes;
+}
+
+sub BUILDARGS {
+ my $class = shift;
+ my %args = @_;
+
+ # Generate attributes for whatever they pass in
+ for my $attribute (keys %args) {
+ next if $class->can($attribute);
+ has $attribute =>
+ is => 'ro';
+ push @Attributes, $attribute;
+ }
+
+ return \%args;
+}
+
+1;
View
38 t/Event/Generic.t
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+BEGIN { require "t/test.pl" }
+
+my $CLASS = 'TB2::Event::Generic';
+require_ok $CLASS;
+
+note "generic events"; {
+ my $line = __LINE__ + 1;
+ my $event = $CLASS->new(
+ foo => 23,
+ bar => 42,
+ event_type => "some_thing",
+ );
+
+ is $event->foo, 23;
+ is $event->bar, 42;
+ is $event->event_type, "some_thing";
+ ok $event->object_id;
+
+ is_deeply $event->as_hash, {
+ foo => 23,
+ bar => 42,
+ event_type => 'some_thing',
+ object_id => $event->object_id,
+ pid => $$
+ };
+}
+
+note "generic events must be given an event type"; {
+ ok !eval { $CLASS->new() };
+ like $@, qr{^The event_type must be defined in the constructor};
+}
+
+done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.