1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  
     | 
    
      #!/usr/bin/perl
package X;
use Test;
BEGIN { plan tests => 19 }
use Class::MakeMethods::Template::Hash (
  new => 'new',
  'new --with_init' => 'new_with_init',
  'new --instance_with_methods' => 'new_hash_init'
);
my $init_called;
my @args_in_init;
my $foo_called;
my $bar_called;
sub init {
  my ($self, @args) = @_;
  $init_called++;
  @args_in_init = @args;
}
sub foo {
  my ($self, $new) = @_;
  defined $new and $self->{'foo'} = $new;
  $foo_called = 1;
  $self->{'foo'};
}
sub bar {
  my ($self, $new) = @_;
  defined $new and $self->{'bar'} = $new;
  $bar_called = 1;
  $self->{'bar'};
}
ok( 1 ); #1
# Regular new
ok( $o = new X ); #2
ok( ref $o eq 'X' ); #3
# new_with_init
my @args = (1, 2, 3);
ok( $o = X->new_with_init(@args) ); #4
ok( ref $o eq 'X' ); #5
ok(  $#args_in_init == $#args ); #6
ok do {
  my $v = 1;
  for (0..$#args) { $args_in_init[$_] == $args[$_] or $v = 0; }
   $v;
};
# new_hash_init
ok( $o = X->new_hash_init( 'foo' => 123, 'bar' => 456 ) ); #7
ok( ref $o eq 'X' ); #8
ok( $foo_called ); #9
ok( $bar_called ); #10
ok( $o->foo == 123 ); #11
ok( $o->bar == 456 ); #12
# new_hash_init (taking hashref)
ok( $o = X->new_hash_init({ 'foo' => 123, 'bar' => 456 }) ); #13
ok( ref $o eq 'X' ); #14
ok( $foo_called ); #15
ok( $bar_called ); #16
ok( $o->foo == 123 ); #17
ok( $o->bar == 456 ); #18
exit 0;
 
     |