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:
parent
63646aa497
commit
fe33c87f13
2 changed files with 174 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue