"Fossies" - the Fresh Open Source Software Archive

Member "Module-Build-0.4224/t/add_property.t" (30 May 2017, 3171 Bytes) of package /linux/privat/Module-Build-0.4224.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl -w
    2 
    3 use strict;
    4 use lib 't/lib';
    5 use MBTest tests => 27;
    6 #use MBTest 'no_plan';
    7 use DistGen;
    8 
    9 blib_load 'Module::Build';
   10 
   11 my $tmp = MBTest->tmpdir;
   12 my $dist = DistGen->new( dir => $tmp );
   13 $dist->regen;
   14 $dist->chdir_in;
   15 
   16 ADDPROP: {
   17   package My::Build::Prop;
   18   use base 'Module::Build';
   19   __PACKAGE__->add_property( 'foo' );
   20   __PACKAGE__->add_property( 'bar', 'howdy' );
   21   __PACKAGE__->add_property( 'baz', default => 'howdy' );
   22   __PACKAGE__->add_property( 'code', default => sub { 'yay' } );
   23   __PACKAGE__->add_property(
   24     'check',
   25     default => sub { 'howdy' },
   26     check   => sub {
   27       return 1 if $_ eq 'howdy';
   28       shift->property_error(qq{"$_" is invalid});
   29       return 0;
   30     },
   31   );
   32   __PACKAGE__->add_property(
   33     'hash',
   34     default => { foo => 1 },
   35     check   => sub {
   36       return 1 if !defined $_ or exists $_->{foo};
   37       shift->property_error(qq{hash is invalid});
   38       return 0;
   39     },
   40   );
   41 }
   42 
   43 ok my $build = My::Build::Prop->new(
   44   'module_name' => 'Simple',
   45   quiet => 1,
   46 ), 'Create new build object';
   47 
   48 is $build->foo, undef, 'Property "foo" should be undef';
   49 ok $build->foo(42), 'Set "foo"';
   50 is $build->foo, 42, 'Now "foo" should have new value';
   51 
   52 is $build->bar, 'howdy', 'Property "bar" should be its default';
   53 ok $build->bar('yo'), 'Set "bar"';
   54 is $build->bar, 'yo', 'Now "bar" should have new value';
   55 
   56 is $build->check, 'howdy', 'Property "check" should be its default';
   57 
   58 eval { $build->check('yo') };
   59 ok my $err = $@, 'Should get an error for an invalid value';
   60 like $err, qr/^ERROR: "yo" is invalid/, 'It should be the correct error';
   61 
   62 is $build->code, 'yay', 'Property "code" should have its code value';
   63 
   64 is_deeply $build->hash, { foo => 1 }, 'Property "hash" should be default';
   65 is $build->hash('foo'), 1, 'Should be able to get key in hash';
   66 ok $build->hash( bar => 3 ), 'Add a key to the hash prop';
   67 is_deeply $build->hash, { foo => 1, bar => 3 }, 'New key should be in hash';
   68 
   69 eval { $build->hash({ bar => 3 }) };
   70 ok $err = $@, 'Should get exception for assigning invalid hash';
   71 like $err, qr/^ERROR: hash is invalid/, 'It should be the correct error';
   72 
   73 eval { $build->hash( []) };
   74 ok $err = $@, 'Should get exception for assigning an array for a hash';
   75 like $err, qr/^Unexpected arguments for property 'hash'/,
   76   'It should be the proper error';
   77 is $build->hash(undef), undef, 'Should be able to set hash to undef';
   78 
   79 # Check core properties.
   80 is $build->installdirs, 'site', 'Property "installdirs" should be default';
   81 ok $build->installdirs('core'), 'Set "installdirst" to "core"';
   82 is $build->installdirs, 'core', 'Now "installdirs" should be "core"';
   83 
   84 eval { $build->installdirs('perl') };
   85 ok $err = $@, 'Should have caught exception setting "installdirs" to "perl"';
   86 like $err, qr/^ERROR: Perhaps you meant installdirs to be "core" rather than "perl"\?/,
   87   'And it should suggest "core" in the error message';
   88 
   89 eval { $build->installdirs('foo') };
   90 ok $err = $@, 'Should catch exception for invalid "installdirs" value';
   91 like $err, qr/ERROR: installdirs must be one of "core", "site", or "vendor"/,
   92   'And it should suggest the proper values in the error message';
   93 
   94 $dist->chdir_original if $dist->did_chdir;