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