Skip to content

Commit

Permalink
Add spec tests
Browse files Browse the repository at this point in the history
Make the TestResult and TestSet able to determine if they're passing.

Add a default version to the TestSet of 12.
  • Loading branch information
schwern authored and arodland committed Jul 1, 2010
1 parent 5f26048 commit ed026cc
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/TAP/Spec/TestResult.pm
Expand Up @@ -37,6 +37,14 @@ has 'reason' => (
predicate => 'has_reason',
);

sub passed {
my $self = shift;

return 1 if $self->status eq 'ok';
return 1 if $self->directive and $self->directive eq 'TODO';
return '';
}

sub as_tap {
my ($self) = @_;

Expand Down
37 changes: 35 additions & 2 deletions lib/TAP/Spec/TestSet.pm
Expand Up @@ -24,12 +24,27 @@ has 'plan' => (
isa => 'TAP::Spec::Plan',
);

has version => (
is => 'rw',
isa => 'Int',
lazy => 1,
default => sub {
my $self = shift;

if( my $v = $self->header->version ) {
return $v->version_number;
}
else {
return 12;
}
}
);

has 'header' => (
is => 'rw',
isa => 'TAP::Spec::Header',
handles => {
header_comments => 'comments',
version => 'version',
},
required => 1,
);
Expand All @@ -52,6 +67,24 @@ sub as_tap {
$self->footer->as_tap();
}

sub passed {
my $self = shift;

return '' unless $self->plan;
my $expected = $self->plan->number_of_tests;

my $lines = $self->body->lines;
return '' unless @$lines == $expected;

my $count = 1;
for my $line (@$lines) {
return '' unless $line->passed;
return '' unless $line->number == $count++;
}

return 1;
}

__PACKAGE__->meta->make_immutable;
1;
1;

75 changes: 75 additions & 0 deletions t/spec-test.t
@@ -0,0 +1,75 @@
#!/usr/bin/perl

use strict;
use warnings;

use TAP::Spec::Parser;
use YAML::Any qw(LoadFile);
use Path::Class;
use Carp;

use Test::More;

for my $file ( dir("t/spec-tests")->children ) {
next unless $file =~ m{\.ya?ml};

my @yaml = LoadFile($file);

for my $num (0..$#yaml) {
my $test = $yaml[$num];

my $name = $test->{name} || sprintf "Test #%d from %s", $num, $file;
note $name;

my $tap = $test->{tap} || croak "Test has no tap";
my $want = $test->{want} || croak "Test has no want";

my $result = TAP::Spec::Parser->parse_from_string($tap);
ok $result, "Got a result" or next;

my $have = result2have($result);
is_deeply $have, $want, $name or diag explain $have;
}
}

done_testing;


sub result2have {
my $result = shift;
my %have;

$have{passed} = $result->passed;
$have{version} = $result->version;

if( my $plan = $result->plan ) {
$have{has_plan} = 1;
$have{planned_tests} = $plan->number_of_tests;
}

for my $line ( @{ $result->body->lines } ) {
push @{$have{tests}}, line2have($line);
}

return \%have;
}


sub line2have {
my $line = shift;
my %have;

my %fields = (
description => "description",
number => "number",
status => "status",
passed => "passed",
);

for my $field (keys %fields) {
my $value = eval { $line->$field() };
$have{$field} = $value if defined $value;
}

return \%have;
}
15 changes: 15 additions & 0 deletions t/spec-tests/fail.yaml
@@ -0,0 +1,15 @@
---
name: Basic failing test
tap: |
1..1
not ok 1
want:
passed: false
has_plan: true
planned_tests: 1
version: 12
tests:
- number: 1
status: not ok
passed: false
...
15 changes: 15 additions & 0 deletions t/spec-tests/pass.yml
@@ -0,0 +1,15 @@
---
name: Basic passing test
tap: |
1..1
ok 1
want:
passed: true
has_plan: true
planned_tests: 1
version: 12
tests:
- number: 1
status: ok
passed: true
...
17 changes: 17 additions & 0 deletions t/spec-tests/version.yaml
@@ -0,0 +1,17 @@
---
name: TAP version
tap: |
TAP version 13
1..1
ok 1
want:
passed: true
has_plan: true
planned_tests: 1
version: 13
tests:
- number: 1
status: ok
passed: true
...

0 comments on commit ed026cc

Please sign in to comment.