#!/usr/bin/perl -w use strict; use Test::More tests => 45; use Test::Exception; use lib 't'; use RestrictedTest; BEGIN { use_ok('Moose'); } package Foo; { use Moose::Role; sub Foo_bar { shift->bar } sub Foo_baz { shift->baz } sub Foo_quux { shift->quux } has 'bar' => (is => 'rw', metaclass => 'Restricted', default => 'bar', private => 1); has 'baz' => (is => 'rw', metaclass => 'Restricted', default => 'baz', protected => 1); has 'quux' => (is => 'rw', metaclass => 'Restricted', default => 'quux'); has 'ro1' => (is => 'ro', metaclass => 'Restricted', default => 'ro1', private => 0); has 'ro2' => (is => 'ro', metaclass => 'Restricted', default => 'ro2', protected => 0); has 'ro3' => (is => 'ro', metaclass => 'Restricted', default => 'ro3', private => 0, protected => 0); has 'rw1' => (is => 'rw', metaclass => 'Restricted', default => 'rw1', private => 0); has 'rw2' => (is => 'rw', metaclass => 'Restricted', default => 'rw2', protected => 0); has 'rw3' => (is => 'rw', metaclass => 'Restricted', default => 'rw3', private => 0, protected => 0); } package Bar; { use Moose; with 'Foo'; sub Bar_bar { shift->bar } sub Bar_baz { shift->baz } sub Bar_quux { shift->quux } } package Baz; { # can you tell I've run out of names? use Moose; extends 'Bar'; sub Baz_bar { shift->bar } sub Baz_baz { shift->baz } sub Baz_quux { shift->quux } } package main; { my $bar = Bar->new; isa_ok($bar, 'Bar', '$bar'); ok(defined $bar->meta->find_attribute_by_name('bar'), '$bar has bar attribute'); isa_ok($bar->meta->find_attribute_by_name('bar'), 'Moose::Meta::Attribute::Custom::Restricted', '$bar->bar'); ok(defined $bar->meta->find_attribute_by_name('baz'), '$bar has baz attribute'); isa_ok($bar->meta->find_attribute_by_name('baz'), 'Moose::Meta::Attribute::Custom::Restricted', '$bar->baz'); ok(defined $bar->meta->find_attribute_by_name('quux'), '$bar has quux attribute'); isa_ok($bar->meta->find_attribute_by_name('quux'), 'Moose::Meta::Attribute::Custom::Restricted', '$bar->quux'); throws_ok { $bar->bar } qr/is a private attribute/, '$bar->bar dies'; throws_ok { $bar->baz } qr/is a protected attribute/, '$bar->baz dies'; lives_and { is $bar->quux, 'quux' } '$bar->quux lives and eq "quux"'; TODO: { local $TODO = 'roles support unimplemented'; lives_and { is $bar->Foo_bar, 'bar' } '$bar->Foo_bar lives and eq "Foo_bar"'; lives_and { is $bar->Foo_baz, 'baz' } '$bar->Foo_baz lives and eq "Foo_baz"'; } lives_and { is $bar->Foo_quux, 'quux' } '$bar->Foo_quux lives and eq "Foo_quux"'; lives_and { is $bar->ro1, 'ro1' } '$bar->ro1 lives and eq "ro1"'; lives_and { is $bar->ro2, 'ro2' } '$bar->ro2 lives and eq "ro2"'; lives_and { is $bar->ro3, 'ro3' } '$bar->ro3 lives and eq "ro3"'; lives_and { is $bar->rw1, 'rw1' } '$bar->ro1 lives and eq "rw1"'; lives_and { is $bar->rw2, 'rw2' } '$bar->ro2 lives and eq "rw2"'; lives_and { is $bar->rw3, 'rw3' } '$bar->ro3 lives and eq "rw3"'; } package main; { my $baz = Baz->new; isa_ok($baz, 'Baz', '$baz'); ok(defined $baz->meta->find_attribute_by_name('bar'), '$baz has bar attribute'); isa_ok($baz->meta->find_attribute_by_name('bar'), 'Moose::Meta::Attribute::Custom::Restricted', '$baz->bar'); ok(defined $baz->meta->find_attribute_by_name('baz'), '$baz has baz attribute'); isa_ok($baz->meta->find_attribute_by_name('baz'), 'Moose::Meta::Attribute::Custom::Restricted', '$baz->baz'); ok(defined $baz->meta->find_attribute_by_name('quux'), '$bar has quux attribute'); isa_ok($baz->meta->find_attribute_by_name('quux'), 'Moose::Meta::Attribute::Custom::Restricted', '$baz->quux'); throws_ok { $baz->bar } qr/is a private attribute/, '$baz->bar dies'; throws_ok { $baz->baz } qr/is a protected attribute/, '$baz->baz dies'; lives_and { is $baz->quux, 'quux' } '$baz->quux lives and eq "quux"'; TODO: { local $TODO = 'roles support unimplemented'; lives_and { is $baz->Foo_bar, 'bar' } '$baz->Foo_bar lives and eq "Foo_bar"'; lives_and { is $baz->Foo_baz, 'baz' } '$baz->Foo_baz lives and eq "Foo_baz"'; } lives_and { is $baz->Foo_quux, 'quux' } '$baz->Foo_quux lives and eq "Foo_quux"'; lives_and { is $baz->Bar_bar, 'bar' } '$baz->Bar_bar lives and eq "bar"'; lives_and { is $baz->Bar_baz, 'baz' } '$baz->Bar_baz lives and eq "baz"'; lives_and { is $baz->Bar_quux, 'quux' } '$baz->Bar_quux lives and eq "quux"'; throws_ok { $baz->Baz_bar } qr/is a private attribute/, '$baz->Baz_bar dies'; lives_and { is $baz->Baz_baz, 'baz' } '$baz->Baz_baz lives and eq "baz"'; lives_and { is $baz->Baz_quux, 'quux' } '$baz->Baz_quux lives and eq "quux"'; lives_and { is $baz->ro1, 'ro1' } '$baz->ro1 lives and eq "ro1"'; lives_and { is $baz->ro2, 'ro2' } '$baz->ro2 lives and eq "ro2"'; lives_and { is $baz->ro3, 'ro3' } '$baz->ro3 lives and eq "ro3"'; lives_and { is $baz->rw1, 'rw1' } '$baz->ro1 lives and eq "rw1"'; lives_and { is $baz->rw2, 'rw2' } '$baz->ro2 lives and eq "rw2"'; lives_and { is $baz->rw3, 'rw3' } '$baz->ro3 lives and eq "rw3"'; }