Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 315_magic.t
CommitLineData
e59e6222 1# Testing magical scalars (using tied scalar)
2# Note that XSUBs do not handle magical scalars automatically.
3
4use strict;
5use warnings;
6
86a4d873 7use Test::More;
871e9eb5 8use Test::Fatal;
e59e6222 9
10use Class::MOP;
11
12use Tie::Scalar;
13
14{
15 package Foo;
16 use metaclass;
17
86a4d873 18 Foo->meta->add_attribute('bar' =>
e59e6222 19 reader => 'get_bar',
20 writer => 'set_bar',
86a4d873 21 );
e59e6222 22
86a4d873 23 Foo->meta->add_attribute('baz' =>
e59e6222 24 accessor => 'baz',
86a4d873 25 );
e59e6222 26
27 Foo->meta->make_immutable();
28}
29
30{
31 tie my $foo, 'Tie::StdScalar', Foo->new(bar => 100, baz => 200);
32
33 is $foo->get_bar, 100, 'reader with tied self';
34 is $foo->baz, 200, 'accessor/r with tied self';
35
36 $foo->set_bar(300);
37 $foo->baz(400);
38
39 is $foo->get_bar, 300, 'writer with tied self';
40 is $foo->baz, 400, 'accessor/w with tied self';
41}
42
43{
44 my $foo = Foo->new();
45
46 tie my $value, 'Tie::StdScalar', 42;
47
48 $foo->set_bar($value);
49 $foo->baz($value);
50
51 is $foo->get_bar, 42, 'reader/writer with tied value';
52 is $foo->baz, 42, 'accessor with tied value';
53}
54
55{
56 my $x = tie my $value, 'Tie::StdScalar', 'Class::MOP';
57
871e9eb5 58 is( exception { Class::MOP::load_class($value) }, undef, 'load_class(tied scalar)' );
e59e6222 59
60 $value = undef;
61 $x->STORE('Class::MOP'); # reset
62
871e9eb5 63 is( exception {
e59e6222 64 ok Class::MOP::is_class_loaded($value);
871e9eb5 65 }, undef, 'is_class_loaded(tied scalar)' );
e59e6222 66
67 $value = undef;
68 $x->STORE(\&Class::MOP::get_code_info); # reset
69
871e9eb5 70 is( exception {
e59e6222 71 is_deeply [Class::MOP::get_code_info($value)], [qw(Class::MOP get_code_info)], 'get_code_info(tied scalar)';
871e9eb5 72 }, undef );
e59e6222 73}
86a4d873 74
75done_testing;