Break Definition.t into Defintion.t and Definition/Asset.t
This commit is contained in:
parent
ec5f610701
commit
cb9510d9b8
2 changed files with 199 additions and 302 deletions
206
t/Definition.t
206
t/Definition.t
|
|
@ -46,15 +46,13 @@ my $called_getProperties;
|
|||
::can_ok +__PACKAGE__, 'get';
|
||||
::can_ok +__PACKAGE__, 'set';
|
||||
|
||||
# can retreive property metadata
|
||||
::is +__PACKAGE__->getProperty('property1')->form->{'arbitrary_key'}, 'arbitrary_value', 'arbitrary keys mapped into the form attribute';
|
||||
|
||||
# can retreive property metadata
|
||||
::is +__PACKAGE__->getProperty('property1')->form->{'arbitrary_key'}, 'arbitrary_value', 'arbitrary keys mapped into the form attribute';
|
||||
|
||||
# can retreive property metadata
|
||||
::isa_ok +__PACKAGE__->getProperty('property1'), 'WebGUI::Definition::Meta::Property';
|
||||
|
||||
::is +__PACKAGE__->getProperty('property1')->form->{'arbitrary_key'}, 'arbitrary_value', 'arbitrary keys mapped into the form attribute';
|
||||
|
||||
::is +__PACKAGE__->getProperty('property2')->form->{'nother_key'}, 'nother_value', '... and again';
|
||||
|
||||
::cmp_deeply(
|
||||
[ +__PACKAGE__->getProperties ],
|
||||
[qw/property1 property2/],
|
||||
|
|
@ -63,199 +61,3 @@ my $called_getProperties;
|
|||
|
||||
}
|
||||
|
||||
{
|
||||
package WGT::Class::Atset;
|
||||
use WebGUI::Definition::Asset;
|
||||
|
||||
attribute tableName => 'asset';
|
||||
::dies_ok { property 'property1' => (); } 'must have a fieldType';
|
||||
::dies_ok { property 'property1' => (fieldType => 'text'); } 'must pass either a label or noFormPost flag';
|
||||
::lives_ok { property 'property1' => (
|
||||
fieldType => 'YUI Super Form',
|
||||
noFormPost => '1',
|
||||
);
|
||||
} '... pass noFormPost flag';
|
||||
::lives_ok { property 'property1' => (
|
||||
fieldType => 'YUI Super Form',
|
||||
label => 'JSON Powered Uber Widget',
|
||||
);
|
||||
} '... pass label';
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
package WGT::Class::Asset;
|
||||
use WebGUI::Definition::Asset;
|
||||
|
||||
attribute tableName => 'asset';
|
||||
property 'property2' => (
|
||||
fieldType => 'text',
|
||||
label => 'property2',
|
||||
);
|
||||
property 'property1' => (
|
||||
fieldType => 'text',
|
||||
label => 'property1',
|
||||
);
|
||||
|
||||
my $filter2 = 0;
|
||||
around 'property2' => sub {
|
||||
my $orig = shift;
|
||||
my $self = shift;
|
||||
$filter2 = 1;
|
||||
$self->$orig(@_);
|
||||
};
|
||||
|
||||
my $written;
|
||||
sub write {
|
||||
$written++;
|
||||
}
|
||||
|
||||
::is +__PACKAGE__->meta->get_attribute('property1')->tableName, 'asset', 'tableName copied from attribute into property';
|
||||
::isa_ok +__PACKAGE__->getProperty('property1'), 'WebGUI::Definition::Meta::Property::Asset';
|
||||
|
||||
::can_ok +__PACKAGE__, 'update';
|
||||
::can_ok +__PACKAGE__, 'tableName';
|
||||
|
||||
::can_ok +__PACKAGE__->getProperty('property1'), 'tableName';
|
||||
::is +__PACKAGE__->getProperty('property1')->tableName, 'asset', 'tableName set on property to asset';
|
||||
|
||||
my $object = __PACKAGE__->new;
|
||||
$object->set({property1 => 'property value'});
|
||||
::is $object->property1, 'property value', 'checking set, hashref form';
|
||||
|
||||
$object->set('property1', 'newer property value');
|
||||
::is $object->property1, 'newer property value', '... hash form';
|
||||
|
||||
# write called
|
||||
$object->update;
|
||||
::is $written, 1, 'update calls write';
|
||||
|
||||
$object->property2('foo');
|
||||
::is $filter2, 1, 'around modifier works';
|
||||
|
||||
::is $object->tableName, 'asset', 'tableName set for object';
|
||||
$object->tableName('not asset');
|
||||
::is $object->tableName, 'asset', 'tableName may not be set from the object';
|
||||
$object->meta->tableName('not asset');
|
||||
::is $object->tableName, 'not asset', 'object can access meta and change the table';
|
||||
$object->meta->tableName('asset');
|
||||
|
||||
::cmp_deeply(
|
||||
[ $object->meta->get_property_list ],
|
||||
[qw/property2 property1/],
|
||||
'->meta->get_property_list returns properties as a list in insertion order'
|
||||
);
|
||||
|
||||
::cmp_deeply(
|
||||
[ $object->meta->get_all_properties ],
|
||||
::array_each(::isa('WebGUI::Definition::Meta::Property::Asset')),
|
||||
'->meta->get_all_properties returns a list of Properties'
|
||||
);
|
||||
|
||||
::cmp_deeply(
|
||||
[$object->getProperties ],
|
||||
[qw/property2 property1/],
|
||||
'getProperties is an alias for ->meta->get_property_list'
|
||||
);
|
||||
|
||||
::cmp_deeply(
|
||||
[$object->meta->get_tables ],
|
||||
[qw/asset/],
|
||||
'get_tables returns a list of all tables used by this class'
|
||||
);
|
||||
|
||||
my $object2 = __PACKAGE__->new(tableName => 'notAsset');
|
||||
::is $object2->tableName, 'asset', 'tableName ignored in constructor';
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
package WGT::Class::AlsoAsset;
|
||||
use WebGUI::Definition::Asset;
|
||||
|
||||
attribute tableName => 'asset';
|
||||
property 'property1' => (
|
||||
fieldType => 'text',
|
||||
label => 'property1',
|
||||
);
|
||||
property 'property2' => (
|
||||
fieldType => 'text',
|
||||
label => 'property2',
|
||||
);
|
||||
property 'property3' => (
|
||||
fieldType => 'text',
|
||||
label => 'property3',
|
||||
);
|
||||
|
||||
package WGT::Class::Asset::Snippet;
|
||||
use WebGUI::Definition::Asset;
|
||||
extends 'WGT::Class::AlsoAsset';
|
||||
|
||||
attribute tableName => 'snippet';
|
||||
property 'property10' => (
|
||||
fieldType => 'text',
|
||||
label => 'property10',
|
||||
);
|
||||
property 'property11' => (
|
||||
fieldType => 'text',
|
||||
label => 'property11',
|
||||
);
|
||||
|
||||
package main;
|
||||
|
||||
is +WGT::Class::AlsoAsset->getProperty('property1')->tableName, 'asset', 'tableName set in base class';
|
||||
|
||||
is +WGT::Class::Asset::Snippet->getProperty('property10')->tableName, 'snippet', 'tableName set in subclass';
|
||||
is +WGT::Class::Asset::Snippet->getProperty('property1')->tableName, 'asset', '... but inherited properties keep their tableName';
|
||||
|
||||
cmp_bag(
|
||||
[ map {$_->name} WGT::Class::AlsoAsset->meta->get_attributes ],
|
||||
[qw/property1 property2 property3/],
|
||||
'get_attributes returns attributes for my class'
|
||||
);
|
||||
|
||||
cmp_bag(
|
||||
[ map {$_->name} WGT::Class::Asset::Snippet->meta->get_attributes ],
|
||||
[qw/property10 property11/],
|
||||
'...even in a subclass'
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
[ WGT::Class::Asset::Snippet->getProperties ],
|
||||
[qw/property1 property2 property3 property10 property11/],
|
||||
'checking inheritance of properties by name, insertion order'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
package WGT::Class::Asset::NotherOne;
|
||||
use WebGUI::Definition::Asset;
|
||||
extends 'WGT::Class::AlsoAsset';
|
||||
|
||||
attribute tableName => 'snippet';
|
||||
property 'property10' => (
|
||||
fieldType => 'text',
|
||||
label => 'property10',
|
||||
);
|
||||
property 'property1' => (
|
||||
fieldType => 'text',
|
||||
label => 'property1',
|
||||
);
|
||||
|
||||
package main;
|
||||
|
||||
cmp_deeply(
|
||||
[WGT::Class::Asset::NotherOne->getProperties],
|
||||
[qw/property1 property2 property3 property10/],
|
||||
'checking inheritance of properties by name, insertion order with an overridden property'
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
[WGT::Class::Asset::NotherOne->meta->get_tables],
|
||||
[qw/asset snippet/],
|
||||
'get_tables returns both tables'
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,113 +16,208 @@ use FindBin;
|
|||
use lib "$FindBin::Bin/../lib";
|
||||
|
||||
use Test::More 'no_plan'; #tests => 1;
|
||||
use Test::Deep;
|
||||
use Test::Exception;
|
||||
use WebGUI::Test;
|
||||
|
||||
{
|
||||
package WGT::Class;
|
||||
use WebGUI::Definition::Asset (
|
||||
attribute1 => 'attribute 1 value',
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
showInForms => {
|
||||
fieldType => 'Text',
|
||||
label => ['show in forms'],
|
||||
},
|
||||
confirmChange => {
|
||||
fieldType => 'Text',
|
||||
label => ['confirm change', 'Asset'],
|
||||
tableName => 'othertable',
|
||||
},
|
||||
noTrans => {
|
||||
fieldType => 'Text',
|
||||
label => 'this label will not be translated',
|
||||
},
|
||||
],
|
||||
);
|
||||
package WGT::Class::Atset;
|
||||
use WebGUI::Definition::Asset;
|
||||
|
||||
attribute tableName => 'asset';
|
||||
::dies_ok { property 'property1' => (); } 'must have a fieldType';
|
||||
::dies_ok { property 'property1' => (fieldType => 'text'); } 'must pass either a label or noFormPost flag';
|
||||
::lives_ok { property 'property1' => (
|
||||
fieldType => 'YUI Super Form',
|
||||
noFormPost => '1',
|
||||
);
|
||||
} '... pass noFormPost flag';
|
||||
::lives_ok { property 'property1' => (
|
||||
fieldType => 'YUI Super Form',
|
||||
label => 'JSON Powered Uber Widget',
|
||||
);
|
||||
} '... pass label';
|
||||
|
||||
sub session {
|
||||
return WebGUI::Test->session;
|
||||
}
|
||||
}
|
||||
|
||||
my $object = WGT::Class->instantiate;
|
||||
|
||||
can_ok($object, 'getTables');
|
||||
my @tables = $object->getTables;
|
||||
is $tables[0], 'mytable', 'found first table';
|
||||
is $tables[1], 'othertable', 'found second table';
|
||||
|
||||
is $object->getProperty('showInForms')->{tableName}, 'mytable',
|
||||
'properties copy tableName attribute';
|
||||
is $object->getProperty('confirmChange')->{tableName}, 'othertable',
|
||||
'tableName property element not overwritten if manually specified';
|
||||
|
||||
is $object->getProperty('showInForms')->{label}, 'Show In Forms?',
|
||||
'getProperty internationalizes label';
|
||||
is $object->getProperty('confirmChange')->{label}, 'Are you sure?',
|
||||
'getProperty internationalizes label with namespace';
|
||||
is $object->getProperty('noTrans')->{label}, 'this label will not be translated',
|
||||
q{getProperty doesn't internationalize plain scalars};
|
||||
|
||||
{
|
||||
package WGT::Class2;
|
||||
use Test::Exception;
|
||||
throws_ok { WebGUI::Definition::Asset->import(
|
||||
properties => [],
|
||||
) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no tableName specified';
|
||||
throws_ok { WebGUI::Definition::Asset->import(
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
'property1' => {
|
||||
label => 'label',
|
||||
},
|
||||
],
|
||||
) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no fieldType specified';
|
||||
throws_ok { WebGUI::Definition::Asset->import(
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
'property1' => {
|
||||
fieldType => sub { return 'Text' },
|
||||
label => 'label',
|
||||
},
|
||||
],
|
||||
) } 'WebGUI::Error::InvalidParam', 'Exception thrown when dynamic fieldType specified';
|
||||
throws_ok { WebGUI::Definition::Asset->import(
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
'property1' => {
|
||||
tableName => sub { return 'othertable' },
|
||||
fieldType => 'Text',
|
||||
label => 'label',
|
||||
},
|
||||
],
|
||||
) } 'WebGUI::Error::InvalidParam', 'Exception thrown when dynamic tableName specified';
|
||||
throws_ok { WebGUI::Definition::Asset->import(
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
'property1' => {
|
||||
fieldType => 'Text',
|
||||
},
|
||||
],
|
||||
) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no label specified';
|
||||
throws_ok { WebGUI::Definition::Asset->import(
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
'property1' => {
|
||||
fieldType => 'Text',
|
||||
noFormPost => sub { 1 },
|
||||
},
|
||||
],
|
||||
) } 'WebGUI::Error::InvalidParam', 'Exception thrown when no label and noFormPost is dynamic';
|
||||
lives_ok { WebGUI::Definition::Asset->import(
|
||||
tableName => 'mytable',
|
||||
properties => [
|
||||
'property1' => {
|
||||
fieldType => 'Text',
|
||||
noFormPost => 1,
|
||||
},
|
||||
],
|
||||
) } 'Allows no label when noFormPost specified';
|
||||
package WGT::Class::Asset;
|
||||
use WebGUI::Definition::Asset;
|
||||
|
||||
attribute tableName => 'asset';
|
||||
property 'property2' => (
|
||||
fieldType => 'text',
|
||||
label => 'property2',
|
||||
);
|
||||
property 'property1' => (
|
||||
fieldType => 'text',
|
||||
label => 'property1',
|
||||
);
|
||||
|
||||
my $filter2 = 0;
|
||||
around 'property2' => sub {
|
||||
my $orig = shift;
|
||||
my $self = shift;
|
||||
$filter2 = 1;
|
||||
$self->$orig(@_);
|
||||
};
|
||||
|
||||
my $written;
|
||||
sub write {
|
||||
$written++;
|
||||
}
|
||||
|
||||
::is +__PACKAGE__->meta->get_attribute('property1')->tableName, 'asset', 'tableName copied from attribute into property';
|
||||
::isa_ok +__PACKAGE__->getProperty('property1'), 'WebGUI::Definition::Meta::Property::Asset';
|
||||
|
||||
::can_ok +__PACKAGE__, 'update';
|
||||
::can_ok +__PACKAGE__, 'tableName';
|
||||
|
||||
::can_ok +__PACKAGE__->getProperty('property1'), 'tableName';
|
||||
::is +__PACKAGE__->getProperty('property1')->tableName, 'asset', 'tableName set on property to asset';
|
||||
|
||||
my $object = __PACKAGE__->new;
|
||||
$object->set({property1 => 'property value'});
|
||||
::is $object->property1, 'property value', 'checking set, hashref form';
|
||||
|
||||
$object->set('property1', 'newer property value');
|
||||
::is $object->property1, 'newer property value', '... hash form';
|
||||
|
||||
# write called
|
||||
$object->update;
|
||||
::is $written, 1, 'update calls write';
|
||||
|
||||
$object->property2('foo');
|
||||
::is $filter2, 1, 'around modifier called';
|
||||
::is $object->property2(), 'foo', '...and it works for set/get';
|
||||
|
||||
$object->update(property2 => 'bar', property1 => 'baz');
|
||||
::is $object->property1(), 'baz', 'update set property1';
|
||||
::is $object->property2(), 'bar', 'and ... property1';
|
||||
|
||||
::is $object->tableName, 'asset', 'tableName set for object';
|
||||
$object->tableName('not asset');
|
||||
::is $object->tableName, 'asset', 'tableName may not be set from the object';
|
||||
$object->meta->tableName('not asset');
|
||||
::is $object->tableName, 'not asset', 'object can access meta and change the table';
|
||||
$object->meta->tableName('asset');
|
||||
|
||||
::cmp_deeply(
|
||||
[ $object->meta->get_property_list ],
|
||||
[qw/property2 property1/],
|
||||
'->meta->get_property_list returns properties as a list in insertion order'
|
||||
);
|
||||
|
||||
::cmp_deeply(
|
||||
[ $object->meta->get_all_properties ],
|
||||
::array_each(::isa('WebGUI::Definition::Meta::Property::Asset')),
|
||||
'->meta->get_all_properties returns a list of Properties'
|
||||
);
|
||||
|
||||
::cmp_deeply(
|
||||
[$object->getProperties ],
|
||||
[qw/property2 property1/],
|
||||
'getProperties is an alias for ->meta->get_property_list'
|
||||
);
|
||||
|
||||
::cmp_deeply(
|
||||
[$object->meta->get_tables ],
|
||||
[qw/asset/],
|
||||
'get_tables returns a list of all tables used by this class'
|
||||
);
|
||||
|
||||
my $object2 = __PACKAGE__->new(tableName => 'notAsset');
|
||||
::is $object2->tableName, 'asset', 'tableName ignored in constructor';
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
package WGT::Class::AlsoAsset;
|
||||
use WebGUI::Definition::Asset;
|
||||
|
||||
attribute tableName => 'asset';
|
||||
property 'property1' => (
|
||||
fieldType => 'text',
|
||||
label => 'property1',
|
||||
);
|
||||
property 'property2' => (
|
||||
fieldType => 'text',
|
||||
label => 'property2',
|
||||
);
|
||||
property 'property3' => (
|
||||
fieldType => 'text',
|
||||
label => 'property3',
|
||||
);
|
||||
|
||||
package WGT::Class::Asset::Snippet;
|
||||
use WebGUI::Definition::Asset;
|
||||
extends 'WGT::Class::AlsoAsset';
|
||||
|
||||
attribute tableName => 'snippet';
|
||||
property 'property10' => (
|
||||
fieldType => 'text',
|
||||
label => 'property10',
|
||||
);
|
||||
property 'property11' => (
|
||||
fieldType => 'text',
|
||||
label => 'property11',
|
||||
);
|
||||
|
||||
package main;
|
||||
|
||||
is +WGT::Class::AlsoAsset->getProperty('property1')->tableName, 'asset', 'tableName set in base class';
|
||||
|
||||
is +WGT::Class::Asset::Snippet->getProperty('property10')->tableName, 'snippet', 'tableName set in subclass';
|
||||
is +WGT::Class::Asset::Snippet->getProperty('property1')->tableName, 'asset', '... but inherited properties keep their tableName';
|
||||
|
||||
cmp_bag(
|
||||
[ map {$_->name} WGT::Class::AlsoAsset->meta->get_attributes ],
|
||||
[qw/property1 property2 property3/],
|
||||
'get_attributes returns attributes for my class'
|
||||
);
|
||||
|
||||
cmp_bag(
|
||||
[ map {$_->name} WGT::Class::Asset::Snippet->meta->get_attributes ],
|
||||
[qw/property10 property11/],
|
||||
'...even in a subclass'
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
[ WGT::Class::Asset::Snippet->getProperties ],
|
||||
[qw/property1 property2 property3 property10 property11/],
|
||||
'checking inheritance of properties by name, insertion order'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
package WGT::Class::Asset::NotherOne;
|
||||
use WebGUI::Definition::Asset;
|
||||
extends 'WGT::Class::AlsoAsset';
|
||||
|
||||
attribute tableName => 'snippet';
|
||||
property 'property10' => (
|
||||
fieldType => 'text',
|
||||
label => 'property10',
|
||||
);
|
||||
property 'property1' => (
|
||||
fieldType => 'text',
|
||||
label => 'property1',
|
||||
);
|
||||
|
||||
package main;
|
||||
|
||||
cmp_deeply(
|
||||
[WGT::Class::Asset::NotherOne->getProperties],
|
||||
[qw/property1 property2 property3 property10/],
|
||||
'checking inheritance of properties by name, insertion order with an overridden property'
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
[WGT::Class::Asset::NotherOne->meta->get_tables],
|
||||
[qw/asset snippet/],
|
||||
'get_tables returns both tables'
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue