File: RootI.t

package info (click to toggle)
bioperl 1.7.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,888 kB
  • sloc: perl: 94,151; xml: 14,982; makefile: 20
file content (207 lines) | stat: -rw-r--r-- 5,591 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*-Perl-*- Test Harness script for Bioperl
# $Id$

use strict;

BEGIN {
    use Bio::Root::Test;

    test_begin(-tests => 43);

    use_ok 'Bio::Root::Root';
}

ok my $obj = Bio::Root::Root->new();
isa_ok $obj, 'Bio::Root::RootI';

throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;# 'throw failed';

# test throw_not_implemented()
throws_ok { $obj->throw_not_implemented() } qr/EXCEPTION: Bio::Root::NotImplemented/;

{
    package Bio::FooI;
    use base qw(Bio::Root::RootI);
    sub new {
            my $class = shift;
            my $self = {};
            bless $self, ref($class) || $class;
            return $self;
    };
}
$obj = Bio::FooI->new();
throws_ok { $obj->throw_not_implemented() } qr/EXCEPTION /;
$obj = Bio::Root::Root->new();

# doesn't work in perl 5.00405
#my $val;
#eval {
#    my ($tfh,$tfile) = $obj->tempfile();
#    local * STDERR = $tfh;
#    $obj->warn('Testing warn');
#    close $tfh;
#    open(IN, $tfile) or die("cannot open $tfile");
#    $val = join("", <IN>) ;
#    close IN;
#    unlink $tfile;
#};
#ok $val =~ /Testing warn/;
#'verbose(0) warn did not work properly' . $val;

$obj->verbose(-1);
throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;# 'verbose(-1) throw did not work properly' . $@;

lives_ok { $obj->warn('Testing warn') };

$obj->verbose(1);
throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;# 'verbose(1) throw did not work properly' . $@;

# doesn't work in perl 5.00405
#undef $val;
#eval {
#    my ($tfh,$tfile) = $obj->tempfile();
#    local * STDERR = $tfh;
#    $obj->warn('Testing warn');
#    close $tfh;
#    open(IN, $tfile) or die("cannot open $tfile");
#    $val = join("", <IN>);
#    close IN;
#    unlink $tfile;
#};
#ok $val =~ /Testing warn/;# 'verbose(1) warn did not work properly' . $val;

my @stack = $obj->stack_trace();
is scalar @stack, 2;

my $verbobj = Bio::Root::Root->new(-verbose=>1,-strict=>1);
is $verbobj->verbose(), 1;

$Bio::Root::Root::DEBUG = 1;
my $seq = Bio::Root::Root->new();
is $seq->verbose, 1;

# test for bug #1343
my @vals = Bio::Root::RootI->_rearrange([qw(apples pears)],
                                        -apples => 'up the',
                                        -pears  => 'stairs');
is shift @vals, 'up the';
is shift @vals, 'stairs';

# tests for _set_from_args()
# Let's not pollute Bio::Root::Root namespace if possible
# Create temp classes instead which inherit Bio::Root::Root, then test

{

    package Bio::Foo1;
    use base qw(Bio::Root::Root);
    our $VERSION = '2.00';
    sub new {
        my $class = shift;
        my $self = {};
        bless $self, ref($class) || $class;

        $self->_set_from_args(\@_);

        return $self;
    };
}

$obj = Bio::Foo1->new(-verbose => 1, t1 => 1, '--Test-2' => 2);
#ok ! $obj->can('t1'), 'arg not callable';

{

    package Bio::Foo2;
    use base qw(Bio::Root::Root);
    sub new {
        my $class = shift;
        my $self = {};
        bless $self, ref($class) || $class;

        $self->_set_from_args(\@_, -create => 1);

        return $self;
    };

}

$obj = Bio::Foo2->new(-verbose => 1, t3 => 1, '--Test-4' => 2);
ok $obj->can('t3'), 'arg callable since method was created';
ok $obj->can('test_4'), 'mal-formed arg callable since method was created with good name';
for my $m (qw(t3 test_4)) {
    can_ok('Bio::Foo2',$m);
    ok ! Bio::Root::Root->can($m), "Methods don't pollute original Bio::Root::Root namespace";
}

{
    package Bio::Foo3;
    use base qw(Bio::Root::Root);
    sub new {
        my $class = shift;
        my $self = {};
        bless $self, ref($class) || $class;

        $self->_set_from_args(\@_, -methods => ['verbose', 't5'], -create => 1);

        return $self;
    };
}

$obj = Bio::Foo3->new(-verbose => 1, t5 => 1, '--Test-6' => 2);
can_ok $obj, 't5';
ok ! $obj->can('test_6'), 'arg not in method list not created';

can_ok ('Bio::Foo3','t5');
ok ! UNIVERSAL::can('Bio::Root::Root','t5'), "Methods don't pollute original Bio::Root::Root namespace";

{
    package Bio::Foo4;
    use base qw(Bio::Root::Root);
    sub new {
            my $class = shift;
            my $self = {};
            bless $self, ref($class) || $class;

            my %args = @_;

            $self->_set_from_args(\%args, -methods => {(verbose => 'v',
                                                        test7 => 't7',
                                                        test_8 => 't8')},
                                          -create => 1);

            return $self;
    };
}

# with synonyms

$obj = Bio::Foo4->new(-verbose => 1, t7 => 1, '--Test-8' => 2);
is $obj->verbose, 1, 'verbose was set correctly';
is $obj->t7, 1, 'synonym was set correctly';
is $obj->test7, 1, 'real method of synonym was set correctly';
is $obj->test_8, 2, 'mal-formed arg correctly resolved to created method';
is $obj->t8, 2, 'synonym of set method was set correctly';

for my $m (qw(t7 test7 test_8 t8)) {
    can_ok 'Bio::Foo4', $m;
    ok ! UNIVERSAL::can('Bio::Root::Root','t7'), "Methods don't pollute original Bio::Root::Root namespace";
}

# test basic Root::clone()

my $clone = $obj->clone;

is $clone->t7, $obj->t7, 'clone';
is $clone->test7, $obj->test7, 'clone';
is $clone->test_8, $obj->test_8, 'clone';
$clone->test_8('xyz');
isnt $clone->test_8, $obj->test_8, 'clone changed, original didn\'t';

# test Root::clone() with parameter passing, only works for methods
# (introspection via can())

my $clone2 = $obj->clone(-t7 => 'foo');

is $clone2->t7, 'foo', 'parameters passed to clone() modify object';
is $obj->t7, 1, 'original is not modified';