From 377126da8c3525de06758af562d8a494adab8f42 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sun, 30 Nov 2008 04:30:40 +0000 Subject: [PATCH] Add more POD to SurveyJSON. Add some basic tests for getObject, update and remove. --- lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm | 47 +++++++- t/Asset/Wobject/Survey/SurveyJSON.t | 100 +++++++++++++++++- 2 files changed, 140 insertions(+), 7 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm index 68552be9e..fff767bd3 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/SurveyJSON.pm @@ -323,14 +323,15 @@ in that section. =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. +Three elements are enough to reference an answer, for a particular +question in a section. =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. +A perl data structure. Note, that it is not checked for type, so it is +possible to add a "question" object into the list of section objects. +Only the properties defined in $object will be defined in the data +structure, so it is not a replacement. =back @@ -482,6 +483,42 @@ sub copy { } } +=head2 remove ( $address, $movingOverride ) + +Delete the structure pointed to by $address. + +=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. + +=over 4 + +=item 1 element + +If there's just 1 element, then the section with that index is removed. Normally, +the first section, index 0, cannot be removed. See $movingOverride below. + +=item 2 elements + +If there are 2 elements, the question in the section is removed. +in that section. + +=item 3 elements + +Removes the answer in the specified question and section. + +=back + +=head3 $movingOverride + +If $movingOverride is defined (meaning including 0 and ''), then the first section +is allowed to be removed. + +=cut + sub remove { my ( $self, $address, $movingOverride ) = @_; if ( @$address == 1 ) { diff --git a/t/Asset/Wobject/Survey/SurveyJSON.t b/t/Asset/Wobject/Survey/SurveyJSON.t index 2540a0400..eb24d484d 100644 --- a/t/Asset/Wobject/Survey/SurveyJSON.t +++ b/t/Asset/Wobject/Survey/SurveyJSON.t @@ -20,7 +20,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 28; +my $tests = 33; plan tests => $tests + 1 + 3; #---------------------------------------------------------------------------- @@ -416,7 +416,7 @@ cmp_deeply( #################################################### # -# getObject, update +# getObject, update, remove # #################################################### @@ -425,6 +425,15 @@ my $section1 = $surveyJSON->getObject([2]); ##sections out of order. Let's fix it and show the danger of ##using references. +cmp_deeply( + $section1, + superhashof({ + type => 'section', + title => 'Section 1', + }), + 'getObject: Retrieved correct section' +); + $section1->{title} = 'Section 2'; cmp_deeply( summarizeSectionSkeleton($surveyJSON), @@ -473,6 +482,93 @@ cmp_deeply( 'getObject: Returns live, dangerous references' ); +my $question1 = $surveyJSON->getObject([1, 0]); + +cmp_deeply( + $question1, + superhashof({ + type => 'question', + text => 'Question 0+-0', + }), + 'getObject: Retrieved correct question' +); + +$surveyJSON->update([1], { title => 'Section 1'} ); +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 => 'Answer 0-1-2', + }, + ], + }, + { + text => 'Question 0-2', + answers => [], + }, + ], + }, + { + title => 'Section 1', + questions => [ + { + text => 'Question 0+-0', + answers => [], + }, + ], + }, + { + title => 'Section 2', + questions => [], + }, + ], + 'Update: updated a section' +); + +$surveyJSON->update([1, 0], { text => 'Question 1-0'} ); + +cmp_deeply( + $surveyJSON->getObject([1, 0]), + superhashof({ + type => 'question', + text => 'Question 1-0', + answers => [ + superhashof ({ + text => '', + }), + ], + }), + 'update: updating a question adds a new, default answer?' +); + +$surveyJSON->remove([1, 0, 0]), +cmp_deeply( + $surveyJSON->getObject([1, 0]), + superhashof({ + type => 'question', + text => 'Question 1-0', + answers => [ + ], + }), + 'remove: removed that extra, default answer' +); + #################################################### #