| 1 | package JSTAPd::TAP; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | sub new { | ||
| 6 | my $class = shift; | ||
| 7 | bless { | ||
| 8 | tests => undef, | ||
| 9 | tap => [], | ||
| 10 | global_stash => {}, | ||
| 11 | }, $class; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub global_stash { $_[0]->{global_stash} } | ||
| 15 | sub tests { $_[0]->{tests} = defined $_[1] ? $_[1] : $_[0]->{tests} } | ||
| 16 | sub error { $_[0]->{error} = defined $_[1] ? $_[1] : $_[0]->{error} } | ||
| 17 | |||
| 18 | sub push_tap { | ||
| 19 | my($self, $tap) = @_; | ||
| 20 | push @{ $self->{tap} }, $tap; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub each_tap { | ||
| 24 | my($self, $cb) = @_; | ||
| 25 | |||
| 26 | for my $tap (@{ $self->{tap} }) { | ||
| 27 | $cb->($tap); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | sub as_string { | ||
| 32 | my $self = shift; | ||
| 33 | my $str; | ||
| 34 | |||
| 35 | $str = sprintf "1..%d\n", $self->tests if $self->tests; | ||
| 36 | $self->each_tap(sub { | ||
| 37 | my $tap = shift; | ||
| 38 | $str .= sprintf "%s %d%s\n", $tap->{ret}, $tap->{num}, ($tap->{msg} ? ' - ' . $tap->{msg} : ''); | ||
| 39 | $str .= "# $tap->{comment}\n" if $tap->{comment}; | ||
| 40 | unless ($tap->{ret} eq 'ok') { | ||
| 41 | $str .= sprintf <<"MSG", ($tap->{msg} || $tap->{num}), $tap->{got}, $tap->{expected}, | ||
| 42 | # Failed test '%s' | ||
| 43 | # got: '%s' | ||
| 44 | # expected: '%s' | ||
| 45 | MSG | ||
| 46 | } | ||
| 47 | }); | ||
| 48 | $str .= $self->error . "\n" if $self->error; | ||
| 49 | |||
| 50 | return $str; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub as_hash { | ||
| 54 | my $self = shift; | ||
| 55 | my $hash = +{ | ||
| 56 | tests => $self->tests || 0, | ||
| 57 | run => 0, | ||
| 58 | ok => 0, | ||
| 59 | fail => 0, | ||
| 60 | error => $self->error, | ||
| 61 | }; | ||
| 62 | |||
| 63 | $self->each_tap(sub { | ||
| 64 | my $tap = shift; | ||
| 65 | $hash->{run}++; | ||
| 66 | if ($tap->{ret} eq 'ok') { | ||
| 67 | $hash->{ok}++; | ||
| 68 | } else { | ||
| 69 | $hash->{fail}++; | ||
| 70 | } | ||
| 71 | }); | ||
| 72 | $hash; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 |