# Copyright (c) 2006 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: PropertyModel.pm,v 1.8 2007/12/12 04:05:58 nagler Exp $ package Bivio::Test::PropertyModel; use strict; use Bivio::Base 'TestUnit.Unit'; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.8 $ =~ /\d+/g); sub new_unit { my($proto, $class, $attrs) = @_; $attrs = {} unless ref($attrs); my($m) = $class; $proto->use('TestUnit.Request')->get_instance; return $proto->SUPER::new({ class_name => $m->package_name, check_return => sub { my($case, $actual, $expect) = @_; return $expect unless $case->get('method') =~ /^(create|update|load)/; my($e) = $expect->[0]; return $expect unless ref($e) eq 'HASH' && @$expect == 1; my($o) = $case->get('object'); $e = _walk_tree_expect($case, $e); $case->actual_return([_walk_tree_actual($case, $e, [])]); return [$e]; }, %$attrs, }); } sub run_unit { # Example RealmMail.bunit. return shift->new(shift)->unit(shift) if @_ == 3; my($self, $method_groups) = @_; return $self->SUPER::run_unit([ [$self->builtin_req->initialize_fully] => $method_groups, ]); } sub _walk_tree_actual { my($case, $e, $names) = @_; my($o) = $case->get('object'); return ref($e) eq 'HASH' ? {map(($_ => _walk_tree_actual($case, $e->{$_}, [@$names, $_])), keys(%$e))} : @$names == 1 && $o->has_fields($names->[0]) ? $o->get($names->[0]) : Bivio::Test::Request->get_instance->unsafe_get_nested(@$names); } sub _walk_tree_expect { my($case, $e) = @_; return ref($e) eq 'HASH' ? {map(($_ => _walk_tree_expect($case, $e->{$_})), keys(%$e))} : ref($e) eq 'CODE' ? $e->($case) : $e; } 1;