~ubuntu-branches/ubuntu/intrepid/libcontextual-return-perl/intrepid

« back to all changes in this revision

Viewing changes to t/fixed.t

  • Committer: Bazaar Package Importer
  • Author(s): David Moreno Garza
  • Date: 2006-05-10 14:52:27 UTC
  • Revision ID: james.westby@ubuntu.com-20060510145227-we2w6vjubaz828db
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use Contextual::Return;
 
2
 
 
3
sub foo {
 
4
    return FIXED (
 
5
        BOOL      { 0 }
 
6
        LIST      { 1,2,3 }
 
7
        NUM       { 42 }
 
8
        STR       { 'forty-two' }
 
9
        SCALAR    { 86 }
 
10
        SCALARREF { \7 }
 
11
        HASHREF   { { name => 'foo', value => 99} }
 
12
        ARRAYREF  { [3,2,1] }
 
13
        GLOBREF   { \*STDERR }
 
14
        CODEREF   { \&baz }
 
15
        OBJREF    { bless {}, 'Bar' }
 
16
    );
 
17
}
 
18
 
 
19
sub bar {
 
20
    return FIXED
 
21
        STR       { 'forty-two' }
 
22
        LIST      { 1,2,3 }
 
23
    ;
 
24
}
 
25
 
 
26
sub baz {
 
27
    return 'in baz';
 
28
}
 
29
 
 
30
package Other;
 
31
use Test::More 'no_plan';
 
32
 
 
33
# We only need to test the scalar contexts, because LIST and VOID are
 
34
# optimized out by checks against wantarray().
 
35
 
 
36
my $CLASS = 'Contextual::Return::Value';
 
37
 
 
38
my $bool = ::foo();
 
39
is ref($bool), $CLASS                         => 'Before usage, it is a C::R::V';
 
40
is do{ $bool ? 'true' : 'false' }, 'false'    => 'BOOLEAN context';
 
41
isnt ref($bool), $CLASS                       => 'After usage, it is not a C::R::V';
 
42
 
 
43
my $num = ::foo();
 
44
is ref($num), $CLASS                         => 'Before usage, it is a C::R::V';
 
45
is $num+0, 42                                => 'NUMERIC context';
 
46
isnt ref($num), $CLASS                       => 'After usage, it is not a C::R::V';
 
47
 
 
48
my $str = ::foo();
 
49
is ref($str), $CLASS                         => 'Before usage, it is a C::R::V';
 
50
is "".$str, 'forty-two'                      => 'STRING context';
 
51
isnt ref($str), $CLASS                       => 'After usage, it is not a C::R::V';
 
52
 
 
53
my $sref = ::foo();
 
54
is ref($sref), $CLASS                         => 'Before usage, it is a C::R::V';
 
55
is ${$sref}, 7                                => 'SCALARREF context';
 
56
isnt ref($sref), $CLASS                       => 'After usage, it is not a C::R::V';
 
57
 
 
58
my $sref2 = ::bar();
 
59
is ref($sref2), $CLASS                     => 'Before usage, it is a C::R::V';
 
60
is ${$sref2}, 'forty-two'                  => 'SCALARREF context (no SCALARREF provided)';
 
61
isnt ref($sref2), $CLASS                   => 'After usage, it is not a C::R::V';
 
62
 
 
63
my $href = ::foo();
 
64
is ref($href), $CLASS                     => 'Before usage, it is a C::R::V';
 
65
is_deeply \%{$href},
 
66
          { name => 'foo', value => 99}         => 'HASHREF context';
 
67
isnt ref($href), $CLASS                   => 'After usage, it is not a C::R::V';
 
68
 
 
69
my $aref = ::foo();
 
70
is ref($aref), $CLASS                     => 'Before usage, it is a C::R::V';
 
71
is_deeply \@{$aref}, [3,2,1]                  => 'ARRAYREF context';
 
72
isnt ref($aref), $CLASS                   => 'After usage, it is not a C::R::V';
 
73
 
 
74
my $aref2 = ::bar();
 
75
is ref($aref2), $CLASS                     => 'Before usage, it is a C::R::V';
 
76
is_deeply \@{$aref2}, [1,2,3]              => 'ARRAYREF context (no ARRAYREF provided)';
 
77
isnt ref($aref2), $CLASS                   => 'After usage, it is not a C::R::V';
 
78
 
 
79
my $gref = ::foo();
 
80
is ref($gref), $CLASS                     => 'Before usage, it is a C::R::V';
 
81
is \*{$gref}, \*STDERR                        => 'GLOBREF context';
 
82
isnt ref($gref), $CLASS                   => 'After usage, it is not a C::R::V';
 
83
 
 
84
my $cref = ::foo();
 
85
is ref($cref), $CLASS                     => 'Before usage, it is a C::R::V';
 
86
is $cref->(), 'in baz'                          => 'CODEREF context';
 
87
isnt ref($cref), $CLASS                   => 'After usage, it is not a C::R::V';
 
88
 
 
89
my $oref = ::foo();
 
90
is ref($oref), $CLASS                     => 'Before usage, it is a C::R::V';
 
91
is $oref->bar, "baaaaa!\n"                => 'OBJREF context';
 
92
isnt ref($oref), $CLASS                   => 'After usage, it is not a C::R::V';
 
93
 
 
94
package Bar;
 
95
 
 
96
sub bar { return "baaaaa!\n"; }