]> git.donarmstrong.com Git - class_modular.git/.git/blob - t/01_module.t
start 0.06SVN version
[class_modular.git/.git] / t / 01_module.t
1 # -*- mode: cperl;-*-
2 # This file is part of Class::Modular and is released under the terms
3 # of the GPL version 2, or any later version at your option. See the
4 # file README and COPYING for more information.
5 # Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: $
7
8
9 use Test::More tests => 11;
10
11 use UNIVERSAL;
12
13 use_ok('Class::Modular');
14
15 my $destroy_hit = 0;
16
17 {
18      # Foo require.
19      $INC{'Foo.pm'} = '1';
20      package Foo;
21
22      use vars qw(@METHODS);
23      BEGIN {
24           @METHODS = qw(blah);
25      }
26
27      use base qw(Class::Modular);
28
29      sub blah {
30           return 1;
31      }
32
33      sub _destroy{
34           $destroy_hit = 1;
35      }
36 }
37
38 {
39      # Bar require.
40      $INC{'Bar.pm'} = '1';
41      package Bar;
42
43      use base qw(Class::Modular);
44
45      sub bleh {
46           return 1;
47      }
48
49      sub _methods {
50           return qw(bleh);
51      }
52 }
53
54
55
56
57 my $foo = new Foo(qw(bar baz));
58
59 # 1: test new
60 ok(defined $foo and UNIVERSAL::isa($foo,'Class::Modular'), 'new() works');
61
62 # 2: test load()
63 ok($foo->is_loaded('Foo'), 'load and is_loaded work');
64 # 3: test AUTOLOAD
65 ok($foo->blah, 'AUTOLOAD works');
66
67 # Check override
68 $foo->override('blah',sub{return 2});
69 ok($foo->blah == 2, 'override() works');
70
71 # Check can
72 # 5: Check can
73 ok($foo->can('blah'),'can() works');
74
75 # Check clone
76 ok(defined $foo->clone, 'clone() works');
77
78 # Check handledby
79 ok($foo->handledby('blah') eq 'Foo', 'handledby() works');
80
81 # Check DESTROY
82 undef $foo;
83 ok($destroy_hit,'DESTROY called _destroy');
84
85 # Check non-existant _destroy doesn't cause a failure
86
87 eval {my $bar = new Bar();
88       undef $bar;
89  };
90 ok($@ eq '','Non existant _destroy not a problem');
91
92 # Check _methods way of defining methods
93 my $bar = new Bar;
94 ok($bar->bleh, '_methods function works to define methods');