summaryrefslogtreecommitdiffstats
path: root/t/moosex-globref.t
blob: e33c2044ad1940d0ed2bf06b3da4b6e72fa9e9aa (plain) (blame)
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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Moose;
BEGIN {
    eval "use MooseX::GlobRef ()";
    plan skip_all => "MooseX::GlobRef is required for this test" if $@;
}
# XXX: the way the IO modules are loaded means we can't just rely on cmop to
# load these properly/:
use IO::Handle;
use IO::File;

BEGIN {
    require Moose;

    package Foo::Exporter;
    use Moose::Exporter;
    Moose::Exporter->setup_import_methods(also => ['Moose']);

    sub init_meta {
        shift;
        my %options = @_;
        Moose->init_meta(%options);
        Moose::Util::MetaRole::apply_metaroles(
            for             => $options{for_class},
            class_metaroles => {
                class => ['MooseX::NonMoose::Meta::Role::Class'],
                constructor =>
                    ['MooseX::NonMoose::Meta::Role::Constructor'],
                instance =>
                    ['MooseX::GlobRef::Role::Meta::Instance'],
            },
        );
        return Moose::Util::find_meta($options{for_class});
    }
}

package IO::Handle::Moose;
BEGIN { Foo::Exporter->import }
extends 'IO::Handle';

has bar => (
    is => 'rw',
    isa => 'Str',
);

sub FOREIGNBUILDARGS { return }

package IO::File::Moose;
BEGIN { Foo::Exporter->import }
extends 'IO::File';

has baz => (
    is => 'rw',
    isa => 'Str',
);

sub FOREIGNBUILDARGS { return }

package main;

with_immutable {
    my $handle = IO::Handle::Moose->new(bar => 'BAR');
    is($handle->bar, 'BAR', 'moose accessor works properly');
    $handle->bar('RAB');
    is($handle->bar, 'RAB', 'moose accessor works properly (setting)');
} 'IO::Handle::Moose';

with_immutable {
    SKIP: {
        my $fh = IO::File::Moose->new(baz => 'BAZ');
        open $fh, "+>", undef
            or skip "couldn't open a temporary file", 3;
        is($fh->baz, 'BAZ', "accessor works");
        $fh->baz('ZAB');
        is($fh->baz, 'ZAB', "accessor works (writing)");
        $fh->print("foo\n");
        print $fh "bar\n";
        $fh->seek(0, 0);
        my $buf;
        $fh->read($buf, 8);
        is($buf, "foo\nbar\n", "filehandle still works as normal");
    }
} 'IO::File::Moose';

done_testing;