Make copy use safe references.
Add tests for remove, and copy.
This commit is contained in:
parent
438058dc7d
commit
d370772762
2 changed files with 292 additions and 11 deletions
|
|
@ -431,14 +431,15 @@ sub insertObject {
|
|||
=head2 copy ( $address )
|
||||
|
||||
Duplicate the structure pointed to by $address, and add it to the end of the list of
|
||||
similar structures.
|
||||
similar structures. copy returns $address with the last element changed to the highest
|
||||
index in that array.
|
||||
|
||||
=head3 $address
|
||||
|
||||
An array ref. The number of elements array set what is added, and
|
||||
where.
|
||||
|
||||
This method modifies $address if it has 1 or more elements.
|
||||
This method modifies $address.
|
||||
|
||||
=over 4
|
||||
|
||||
|
|
@ -449,7 +450,7 @@ at the end of the array of sections.
|
|||
|
||||
=item 2 elements
|
||||
|
||||
If there are 2 elements, question in the section that is indexed
|
||||
If there are 2 elements, the question in the section that is indexed
|
||||
will be duplicated and added to the end of the array of questions
|
||||
in that section.
|
||||
|
||||
|
|
@ -464,14 +465,15 @@ Nothing happens. It is not allowed to duplicate answers.
|
|||
sub copy {
|
||||
my ( $self, $address ) = @_;
|
||||
if ( @$address == 1 ) {
|
||||
my %newSection = %{ $self->section($address) };
|
||||
push( @{ $self->sections }, \%newSection );
|
||||
return [ $#{ $self->sections } ];
|
||||
my $newSection = clone $self->section($address);
|
||||
push( @{ $self->sections }, $newSection );
|
||||
$address->[0] = $#{ $self->sections };
|
||||
return $address;
|
||||
}
|
||||
elsif ( @$address == 2 ) {
|
||||
my %newQuestion = %{ $self->question($address) };
|
||||
push( @{ $self->questions($address) }, \%newQuestion );
|
||||
$$address[1] = $#{ $self->questions($address) };
|
||||
my $newQuestion = clone $self->question($address);
|
||||
push( @{ $self->questions($address) }, $newQuestion );
|
||||
$address->[1] = $#{ $self->questions($address) };
|
||||
return $address;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ my $session = WebGUI::Test->session;
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
my $tests = 34;
|
||||
my $tests = 43;
|
||||
plan tests => $tests + 1 + 3;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -565,7 +565,7 @@ cmp_deeply(
|
|||
answers => [
|
||||
],
|
||||
}),
|
||||
'update: updating a question adds a new, default answer?'
|
||||
'update: updating a question properly'
|
||||
);
|
||||
|
||||
$surveyJSON->remove([1, 0, 0]),
|
||||
|
|
@ -580,6 +580,285 @@ cmp_deeply(
|
|||
'remove: No problems with removing nonexistant data'
|
||||
);
|
||||
|
||||
$surveyJSON->remove([0, 1, 2]),
|
||||
cmp_deeply(
|
||||
$surveyJSON->getObject([0, 1]),
|
||||
superhashof({
|
||||
type => 'question',
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
superhashof({
|
||||
type => 'answer',
|
||||
text => 'Answer 0-1-0',
|
||||
}),
|
||||
superhashof({
|
||||
type => 'answer',
|
||||
text => 'Answer 0-1-1',
|
||||
}),
|
||||
],
|
||||
}),
|
||||
'remove: Remove an answer'
|
||||
);
|
||||
|
||||
####################################################
|
||||
#
|
||||
# copy
|
||||
#
|
||||
####################################################
|
||||
|
||||
##Test return value, and stomped address
|
||||
$stompedAddress = [0, 1];
|
||||
my $returnedAddress = $surveyJSON->copy($stompedAddress);
|
||||
is_deeply($returnedAddress, [0, 3], 'Added a question');
|
||||
is_deeply($stompedAddress, [0, 3], 'copy writes to the reference when copying a question');
|
||||
cmp_deeply(
|
||||
summarizeSectionSkeleton($surveyJSON),
|
||||
[
|
||||
{
|
||||
title => 'Section 0',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 0-0',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-1-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-2',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-1-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 1-0',
|
||||
answers => [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [],
|
||||
},
|
||||
],
|
||||
'copy: copied a question with answers'
|
||||
);
|
||||
|
||||
##Test that copy handles references correctly
|
||||
$surveyJSON->answer([0,3,0])->{text} = 'Answer 0-3-0';
|
||||
cmp_deeply(
|
||||
summarizeSectionSkeleton($surveyJSON),
|
||||
[
|
||||
{
|
||||
title => 'Section 0',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 0-0',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-1-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-2',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-3-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 1-0',
|
||||
answers => [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [],
|
||||
},
|
||||
],
|
||||
'copy: copies safe references for a question'
|
||||
);
|
||||
|
||||
##Now, try a section copy.
|
||||
##Update the title to make the copying clear.
|
||||
$surveyJSON->section([2])->{title} = 'Section 2';
|
||||
$stompedAddress = [1];
|
||||
$returnedAddress = $surveyJSON->copy($stompedAddress);
|
||||
is_deeply($returnedAddress, [3], 'Added a section');
|
||||
is_deeply($stompedAddress, [3], 'copy writes to the reference when copying a section');
|
||||
cmp_deeply(
|
||||
summarizeSectionSkeleton($surveyJSON),
|
||||
[
|
||||
{
|
||||
title => 'Section 0',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 0-0',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-1-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-2',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-3-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 1-0',
|
||||
answers => [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 2',
|
||||
questions => [],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 1-0',
|
||||
answers => [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
'copy: copied a section'
|
||||
);
|
||||
|
||||
$surveyJSON->question([3,0])->{text} = 'Question 3-0';
|
||||
$surveyJSON->section([3])->{title} = 'Section 3';
|
||||
cmp_deeply(
|
||||
summarizeSectionSkeleton($surveyJSON),
|
||||
[
|
||||
{
|
||||
title => 'Section 0',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 0-0',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-1-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-2',
|
||||
answers => [],
|
||||
},
|
||||
{
|
||||
text => 'Question 0-1',
|
||||
answers => [
|
||||
{
|
||||
text => 'Answer 0-3-0',
|
||||
},
|
||||
{
|
||||
text => 'Answer 0-1-1',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 1',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 1-0',
|
||||
answers => [],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title => 'Section 2',
|
||||
questions => [],
|
||||
},
|
||||
{
|
||||
title => 'Section 3',
|
||||
questions => [
|
||||
{
|
||||
text => 'Question 3-0',
|
||||
answers => [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
'copy: safe copy of a section'
|
||||
);
|
||||
|
||||
####################################################
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue