#!/usr/bin/perl -w use strict; use Test::More tests => 39; use Test::Exception; use lib 't'; use RestrictedTest; BEGIN { use_ok('Moose'); }; package Foo; { use Moose; 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); sub Foo_bar { shift->bar } no Moose; }; package Bar; { use Moose; extends 'Foo'; sub Bar_bar { shift->bar } sub Bar_baz { shift->baz } sub Bar_quux { shift->quux } no Moose; }; package main; { my $foo = new Foo; isa_ok($foo, 'Foo', '$foo'); ok($foo->meta->has_attribute('bar'), '$foo has bar attribute'); isa_ok($foo->meta->find_attribute_by_name('bar'), 'Moose::Meta::Attribute::Custom::Restricted', '$foo->bar'); ok($foo->meta->has_attribute('baz'), '$foo has baz attribute'); isa_ok($foo->meta->find_attribute_by_name('baz'), 'Moose::Meta::Attribute::Custom::Restricted', '$foo->baz'); ok($foo->meta->has_attribute('quux'), '$foo has quux attribute'); isa_ok($foo->meta->find_attribute_by_name('quux'), 'Moose::Meta::Attribute::Custom::Restricted', '$foo->quux'); throws_ok { $foo->bar } qr/is a private attribute/, '$foo->bar dies'; throws_ok { $foo->baz } qr/is a protected attribute/, '$foo->baz dies'; lives_and { is $foo->quux, 'quux' } '$foo->quux lives and eq "quux"'; lives_and { is $foo->Foo_bar, 'bar' } '$foo->Foo_bar lives and eq "bar"'; lives_and { is $foo->ro1, 'ro1' } '$foo->ro1 lives and eq "ro1"'; lives_and { is $foo->ro2, 'ro2' } '$foo->ro2 lives and eq "ro2"'; lives_and { is $foo->ro3, 'ro3' } '$foo->ro3 lives and eq "ro3"'; lives_and { is $foo->rw1, 'rw1' } '$foo->ro1 lives and eq "rw1"'; lives_and { is $foo->rw2, 'rw2' } '$foo->ro2 lives and eq "rw2"'; lives_and { is $foo->rw3, 'rw3' } '$foo->ro3 lives and eq "rw3"'; } { my $bar = new Bar; 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"'; throws_ok { $bar->Bar_bar } qr/is a private attribute/, '$bar->Bar_bar dies'; lives_and { is $bar->Bar_baz, 'baz' } '$bar->Bar_baz lives and is eq "baz"'; lives_and { is $bar->Bar_quux, 'quux' } '$bar->Bar_quux lives and is eq "quux"'; lives_and { is $bar->Foo_bar, 'bar' } '$bar->Foo_bar lives and is eq "bar"'; 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"'; } throws_ok { package Baz; use Moose; has 'foo' => (is => 'rw', metaclass => 'Restricted', default => 'foo', private => 1, protected => 1); } qr/Attributes cannot be both private and protected/, 'can\'t create attribute which is both protected and private'; 1;