Add tests for insertObject, and a helper method for summarizing the SurveyJSON data structure.

Add POD for insertObject.  Note that splicing beyond the end of the array will generate
a warning.
This commit is contained in:
Colin Kuskie 2008-11-29 05:32:14 +00:00
parent 63646aa497
commit fe33c87f13
2 changed files with 174 additions and 4 deletions

View file

@ -293,16 +293,62 @@ sub update {
#determine what to add and add it.
# ref should contain all the information for the new
=head2 insertObject ( $object, $address )
Add new "objects" into the current data structure. It does not
return anything significant.
=head3 $object
A perl data structure. Note, that it is not checked for homegeneity,
so it is possible to add a "question" object into the list of section
objects.
=head3 $address
An array ref. The number of elements array set what is added, and
where.
=over 4
=item empty
If the array ref is empty, nothing is done.
=item 1 element
If there's just 1 element, then that element is used as an index into
the array of sections, and $object is spliced into place right after
that index.
=item 2 elements
If there are 2 elements, then the first element is an index into
section array, and the second element is an index into the questions
in that section. $object is added right after that question.
=back
=item 3 elements
Three elements are enough to reference an answer, inside of a particular
question in a section. $object is spliced in right after that answer.
=back
=cut
sub insertObject {
my ( $self, $object, $address ) = @_;
if ( @$address == 1 ) {
splice( @{ $self->sections($address) }, $$address[0] + 1, 0, $object );
splice( @{ $self->sections($address) }, $$address[0] + 1, 0, $object ); ##always a default section
}
elsif ( @$address == 2 ) {
splice( @{ $self->questions($address) }, $$address[1] + 1, 0, $object );
splice( @{ $self->questions($address) }, $$address[1] + 1, 0, $object ); ##warning, beyond end of array
}
elsif ( @$address == 3 ) {
splice( @{ $self->answers($address) }, $$address[2] + 1, 0, $object );
splice( @{ $self->answers($address) }, $$address[2] + 1, 0, $object ); ##warning, beyond end of array
}
}

View file

@ -20,7 +20,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 22;
my $tests = 26;
plan tests => $tests + 1 + 3;
#----------------------------------------------------------------------------
@ -252,6 +252,101 @@ cmp_deeply(
'newObject: Added an answer to the 2nd question in the 2nd section'
);
####################################################
#
# insertObject, section
#
####################################################
$surveyJSON = WebGUI::Asset::Wobject::Survey::SurveyJSON->new('{}', $session->log);
{
my $section = $surveyJSON->section([0]);
$section->{title} = 'Section 0';
}
cmp_deeply(
summarizeSectionSkeleton($surveyJSON),
[
{
title => 'Section 0',
questions => [],
},
],
'section: Set the title for the default section'
);
{
my $section = $surveyJSON->newSection;
$section->{title} = 'Section 1';
$surveyJSON->insertObject($section, [0]);
}
cmp_deeply(
summarizeSectionSkeleton($surveyJSON),
[
{
title => 'Section 0',
questions => [],
},
{
title => 'Section 1',
questions => [],
},
],
'section: Insert a new section after the default section'
);
{
my $section = $surveyJSON->newSection;
$section->{title} = 'Section 0+';
$surveyJSON->insertObject($section, [0]);
}
cmp_deeply(
summarizeSectionSkeleton($surveyJSON),
[
{
title => 'Section 0',
questions => [],
},
{
title => 'Section 0+',
questions => [],
},
{
title => 'Section 1',
questions => [],
},
],
'section: Insert another new section after the default section'
);
{
my $question = $surveyJSON->newQuestion;
$question->{text} = 'Question 0-0';
$surveyJSON->insertObject($question, [0,0]);
}
cmp_deeply(
summarizeSectionSkeleton($surveyJSON),
[
{
title => 'Section 0',
questions => [
{
text => 'Question 0-0',
answers => [],
}
],
},
{
title => 'Section 0+',
questions => [],
},
{
title => 'Section 1',
questions => [],
},
],
'section: Insert a question into the first section'
);
####################################################
#
# TODO
@ -265,6 +360,35 @@ cmp_deeply(
}
# Go through a JSON survey type data structure and just grab some unique
# elements
sub summarizeSectionSkeleton {
my ($skeleton) = @_;
my $summary = [];
foreach my $section (@{ $skeleton->{sections} }) {
my $summarySection = {
title => $section->{title},
questions => [],
};
foreach my $question ( @{ $section->{questions} } ) {
my $summaryQuestion = {
text => $question->{text},
answers => [],
};
foreach my $answer ( @{ $question->{answers} } ) {
my $summaryAnswer = {
text => $answer->{text},
};
push @{ $summaryQuestion->{answers} }, $summaryAnswer;
}
push @{ $summarySection->{questions} }, $summaryQuestion;
}
push @{ $summary }, $summarySection;
}
return $summary;
}
# [
# [0,1,1], # A section with three questions, no answer, 1 answer, 1 answer
# [4], # A section with 1 question with 4 answers