Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Protect against autovivication.

  • Loading branch information...
commit add5792b45c600abbdd656e397ca679e589c9b29 1 parent 9abc1e8
David E. Wheeler theory authored

Showing 2 changed files with 42 additions and 3 deletions. Show diff stats Hide diff stats

  1. +1 0  Changes
  2. +41 3 t/base.t
1  Changes
... ... @@ -1,6 +1,7 @@
1 1 Revision history for Perl extension PGXN-Meta-Validator.
2 2
3 3 0.14.0
  4 + - Added tests to protect against autovivication of keys.
4 5
5 6 0.13.0 2011-05-26T03:52:07
6 7 - Fixed invalid Unicode character class.
44 t/base.t
... ... @@ -1,6 +1,6 @@
1 1 use strict;
2 2 use warnings;
3   -use Test::More tests => 7;
  3 +use Test::More tests => 28;
4 4 use File::Spec;
5 5
6 6 BEGIN {
@@ -9,11 +9,13 @@ BEGIN {
9 9
10 10 my $file = File::Spec->catfile(qw(t META.json));
11 11
12   -ok my $pmv = PGXN::Meta::Validator->new(JSON->new->decode(do {
  12 +my $data = JSON->new->decode(do {
13 13 local $/;
14 14 open my $fh, '<:raw', $file or die "Cannot open $file: $!\n";
15 15 <$fh>;
16   -})), 'Construct from data structure';
  16 +});
  17 +
  18 +ok my $pmv = PGXN::Meta::Validator->new($data), 'Construct from data structure';
17 19 ok $pmv->is_valid, 'Structure should be valid';
18 20
19 21 ok $pmv = PGXN::Meta::Validator->load_file($file), 'Load from file';
@@ -31,3 +33,39 @@ eval {
31 33 };
32 34 like $@, qr{^malformed JSON string},
33 35 'Should catch exception for invalid JSON';
  36 +
  37 +# Make sure there's no autovivication.
  38 +delete $data->{no_index};
  39 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no no_index';
  40 +ok $pmv->is_valid, 'Structure should be valid';
  41 +ok !exists $data->{no_index}, 'Should still have no no_index key';
  42 +
  43 +delete $data->{prereqs}{runtime}{requires};
  44 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no requires';
  45 +ok $pmv->is_valid, 'Structure should be valid';
  46 +ok !exists $data->{prereqs}{runtime}{requires}, 'Should still have no requires key';
  47 +
  48 +delete $data->{prereqs}{runtime};
  49 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no runtime';
  50 +ok $pmv->is_valid, 'Structure should be valid';
  51 +ok !exists $data->{prereqs}{runtime}, 'Should still have no runtime key';
  52 +
  53 +delete $data->{prereqs};
  54 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no prereqs';
  55 +ok $pmv->is_valid, 'Structure should be valid';
  56 +ok !exists $data->{prereqs}, 'Should still have no prereqs key';
  57 +
  58 +delete $data->{resources}{bugtracker};
  59 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no bugtracker';
  60 +ok $pmv->is_valid, 'Structure should be valid';
  61 +ok !exists $data->{resources}{bugtracker}, 'Should still have no bugtracker key';
  62 +
  63 +delete $data->{resources}{repository};
  64 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no repository';
  65 +ok $pmv->is_valid, 'Structure should be valid';
  66 +ok !exists $data->{resources}{repository}, 'Should still have no repository key';
  67 +
  68 +delete $data->{resources};
  69 +ok $pmv = PGXN::Meta::Validator->new($data), 'Construct with no resources';
  70 +ok $pmv->is_valid, 'Structure should be valid';
  71 +ok !exists $data->{resources}, 'Should still have no resources key';

0 comments on commit add5792

Please sign in to comment.
Something went wrong with that request. Please try again.