File: new.t

package info (click to toggle)
libclass-methodmaker-perl 2.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: perl: 1,849; objc: 492; makefile: 3
file content (171 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (9)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# (X)Emacs mode: -*- cperl -*-

use strict;

=head1 Unit Test Package for Class::MethodMaker

This package tests the scalar type of Class::MethodMaker

=cut

use FindBin                1.42 qw( $Bin $Script );
use Test                   1.13 qw( ok plan );

use lib $Bin;
use test qw( evcheck );

BEGIN {
  plan tests  => 32,
       todo   => [],
}

# ----------------------------------------------------------------------------

=head2 Test 1: compilation

This test confirms that the test script and the modules it calls compiled
successfully.

=cut

package X;

use Class::MethodMaker
  [ new => 'new',
    new => [qw/ -hash new_hash_init /],
    new => [qw/ -init new_with_init
                -hash new_both /],
  ];

my @args_in_init;
my $foo_called;
my $bar_called;

sub init {
  my ($self, @args) = @_;
  @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'};
}

package main;

ok 1, 1, 'compilation';

# -------------------------------------

=head Tests 2--3: new

=cut

{
  my $o;

  ok evcheck(sub { $o = new X; }, 'new ( 1)'), 1,                  'new ( 1)';
  ok ref $o, 'X',                                                  'new ( 2)';
}

# -------------------------------------

=head Tests 4--9: new_with_init

=cut

{
  my $o;
  my @args = (1, 2, 3);
  ok(evcheck(sub { $o = X->new_with_init(@args) }, 'new_with_init ( 1)'), 1,
                                                         'new_with_init ( 1)');
  ok ref $o, 'X',                                        'new_with_init ( 2)';
  ok $#args_in_init, $#args,                             'new_with_init ( 3)';
  ok $args_in_init[$_], $args[$_], sprintf('new_with_init (%2d)', $_+4)
    for 0..$#args;

  @args_in_init = ();
}

# -------------------------------------

=head Tests 10--15: new_hash_init

=cut

{
  my $o;
  ok(evcheck(sub { $o = X->new_hash_init( 'foo' => 123, 'bar' => 456 ) },
           'new_hash_init ( 1)'), 1,                     'new_hash_init ( 1)');
  ok ref $o, 'X',                                        'new_hash_init ( 2)';
  ok $foo_called, 1,                                     'new_hash_init ( 3)';
  ok $bar_called, 1,                                     'new_hash_init ( 4)';
  ok $o->foo, 123,                                       'new_hash_init ( 5)';
  ok $o->bar, 456,                                       'new_hash_init ( 6)';

  $foo_called = 0;
  $bar_called = 0;
}
# -------------------------------------

=head Tests 16--21: new_hash_init (taking hashref)

=cut

{
  my $o;
  $foo_called = 0;
  $bar_called = 0;

  ok(evcheck(sub { $o = X->new_hash_init({ 'foo' => 111, 'bar' => 444 }) },
           'new_hash_init (taking hashref) ( 1)'), 1,
                                        'new_hash_init (taking hashref) ( 1)');
  ok ref $o, 'X',                       'new_hash_init (taking hashref) ( 2)';
  ok $foo_called, 1,                    'new_hash_init (taking hashref) ( 3)';
  ok $bar_called, 1,                    'new_hash_init (taking hashref) ( 4)';
  ok $o->foo, 111,                      'new_hash_init (taking hashref) ( 5)';
  ok $o->bar, 444,                      'new_hash_init (taking hashref) ( 6)';

  $foo_called = 0;
  $bar_called = 0;
}

# -------------------------------------

=head Tests 22--32: new_hash_init (with init)

=cut

{
  my $o;

  my @args = ('foo' => 987, 'bar' => 654);
  ok(evcheck(sub { $o = X->new_both(@args) },
                'new_hash_init (with init) ( 1)'), 1,
                                             'new_hash_init (with init) ( 1)');
  ok ref $o, 'X',                            'new_hash_init (with init) ( 2)';
  ok $foo_called, 1,                         'new_hash_init (with init) ( 3)';
  ok $bar_called, 1,                         'new_hash_init (with init) ( 4)';
  ok $o->foo, 987,                           'new_hash_init (with init) ( 5)';
  ok $o->bar, 654,                           'new_hash_init (with init) ( 6)';
  ok $#args_in_init, $#args,                 'new_hash_init (with init) ( 7)';
  ok($args_in_init[$_], $args[$_],
     sprintf('new_hash_init (with init) (%2d)', $_+8))
    for 0..$#args;

  $foo_called = 0;
  $bar_called = 0;
  @args_in_init = ();
}

# ----------------------------------------------------------------------------