Skip to content

Commit

Permalink
Add load_file().
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed May 25, 2011
1 parent a05e8ad commit 9e36e7e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Module::Build->new(
}, },
requires => { requires => {
'Carp' => 0, 'Carp' => 0,
'JSON' => 0,
'perl' => 5.010, 'perl' => 5.010,
'SemVer' => '0.2.0', 'SemVer' => '0.2.0',
}, },
Expand Down
28 changes: 27 additions & 1 deletion lib/PGXN/Meta/Validator.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use 5.010;
use strict; use strict;
use warnings; use warnings;
use SemVer; use SemVer;
use JSON;
use Carp qw(croak);
our $VERSION = v0.10.0; our $VERSION = v0.10.0;


=head1 Name =head1 Name
Expand Down Expand Up @@ -160,7 +162,7 @@ The constructor must be passed a metadata structure.
=cut =cut


sub new { sub new {
my ($class,$data) = @_; my ($class, $data) = @_;


# create an attributes hash # create an attributes hash
my $self = { my $self = {
Expand All @@ -173,6 +175,30 @@ sub new {
return bless $self, $class; return bless $self, $class;
} }


=head3 C<load_file>
my $meta = PGXN::Meta::Validator->load_file('META.json');
Reads in the specified JSON file and passes the resulting data structure to
C<new()>, returning the resulting PGXN::Meta::Validator object. An exception
will be thrown if the file does not exist or if its contents are not valid
JSON.
=cut

sub load_file {
my ($class, $file) = @_;

croak "load_file() requires a valid, readable filename"
unless -r $file;

$class->new(JSON->new->decode(do {
local $/;
open my $fh, '<:raw', $file or croak "Cannot open $file: $!\n";
<$fh>;
}));
}

=head2 Instance Methods =head2 Instance Methods
=head3 C<is_valid> =head3 C<is_valid>
Expand Down
54 changes: 54 additions & 0 deletions t/META.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "pgTAP",
"abstract": "Unit testing for PostgreSQL",
"description": "pgTAP is a suite of database functions that make it easy to write TAP-emitting unit tests in psql scripts or xUnit-style test functions.",
"version": "0.26.0",
"maintainer": [
"David E. Wheeler <david@kineticode.com>",
"pgTAP List <pgtap-users@pgfoundry.org>"
],
"license": {
"PostgreSQL": "http://www.postgresql.org/about/licence"
},
"prereqs": {
"runtime": {
"requires": {
"plpgsql": 0,
"PostgreSQL": "8.0.0"
},
"recommends": {
"PostgreSQL": "8.4.0"
}
}
},
"provides": {
"pgtap": {
"abstract": "Unit testing for PostgreSQL",
"file": "pgtap.sql",
"version": "0.26.0"
}
},
"resources": {
"homepage": "http://pgtap.org/",
"bugtracker": {
"web": "https://github.com/theory/pgtap/issues"
},
"repository": {
"url": "https://github.com/theory/pgtap.git",
"web": "https://github.com/theory/pgtap",
"type": "git"
}
},
"generated_by": "David E. Wheeler",
"meta-spec": {
"version": "1.0.0",
"url": "http://pgxn.org/meta/spec.txt"
},
"tags": [
"testing",
"unit testing",
"tap",
"tddd",
"test driven database development"
]
}
33 changes: 33 additions & 0 deletions t/base.t
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
use strict;
use warnings;
use Test::More tests => 7;
use File::Spec;

BEGIN {
use_ok 'PGXN::Meta::Validator' or die;
}

my $file = File::Spec->catfile(qw(t META.json));

ok my $pmv = PGXN::Meta::Validator->new(JSON->new->decode(do {
local $/;
open my $fh, '<:raw', $file or die "Cannot open $file: $!\n";
<$fh>;
})), 'Construct from data structure';
ok $pmv->is_valid, 'Structure should be valid';

ok $pmv = PGXN::Meta::Validator->load_file($file), 'Load from file';
ok $pmv->is_valid, 'File should be valid';

local $@;
eval {
PGXN::Meta::Validator->load_file('nonexistent');
};
like $@, qr{^load_file\(\) requires a valid, readable filename},
'Should catch exception for nonexistent file';

eval {
PGXN::Meta::Validator->load_file('Changes');
};
like $@, qr{^malformed JSON string},
'Should catch exception for invalid JSON';

0 comments on commit 9e36e7e

Please sign in to comment.