From: yasushiito Date: Sun, 9 Jun 2013 01:22:59 +0000 (+0900) Subject: t#31484:validate z X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=e32a749f995f0520b4c395c45a0c285565b5cd19 t#31484:validate z --- diff --git a/app/models/panel.rb b/app/models/panel.rb index 89589780..e343bfa5 100644 --- a/app/models/panel.rb +++ b/app/models/panel.rb @@ -258,33 +258,34 @@ class Panel < ActiveRecord::Base }.flatten end - def self.validate_t ary - i = 0 - ary.compact.sort.each do |t| - break false unless t == i + def self.validate_serial ary, offset = 0 + i = offset + ary.compact.sort.each do |n| + break false unless n == i i += 1 end - ary.compact.size == i + ary.compact.size == i - offset end - def self.validate_element_t elements, name - Panel.validate_t(Panel.collect_element_value(elements, name)) + def self.validate_element_serial elements, name, offset = 0 + Panel.validate_serial(Panel.collect_element_value(elements, name), offset) end - def self.validate_elements_t c + def self.validate_elements_serial c c.map {|conf| - Panel.validate_element_t(conf[:elements], conf[:name]) ? nil : false + Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false }.compact.empty? end - def validate_t_list + def validate_serial_list [ - {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t} + {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0}, + {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1} ] end def validate_child # r1 = Panel.validate_elements_id validate_id_list - Panel.validate_elements_t validate_t_list + Panel.validate_elements_serial validate_serial_list end def store attr, au diff --git a/spec/models/panel_spec.rb b/spec/models/panel_spec.rb index 585b355d..4aa269c7 100644 --- a/spec/models/panel_spec.rb +++ b/spec/models/panel_spec.rb @@ -1031,48 +1031,58 @@ describe Panel do describe 'シリアライズチェックに於いて' do context 'つつがなく終わるとき' do it '0からシリアライズされているならTrueを返す' do - r = Panel.validate_t [0, 1, 2] + r = Panel.validate_serial [0, 1, 2] r.should be_true end it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do - r = Panel.validate_t [0, 2, 1] + r = Panel.validate_serial [0, 2, 1] r.should be_true end it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do - r = Panel.validate_t [ 2, 1, 4, 3, 0] + r = Panel.validate_serial [ 2, 1, 4, 3, 0] + r.should be_true + end + end + context 'オフセットが1のとき' do + it '0からシリアライズされているならFalseを返す' do + r = Panel.validate_serial [0, 1, 2], 1 + r.should be_false + end + it '1からシリアライズされているならTrueを返す' do + r = Panel.validate_serial [1, 2, 3], 1 r.should be_true end end context '異常なとき' do it '0から始まらないならFalseを返す' do - r = Panel.validate_t [1, 2, 3] + r = Panel.validate_serial [1, 2, 3] r.should be_false end it '連続していないならFalseを返す' do - r = Panel.validate_t [0, 1, 2, 4] + r = Panel.validate_serial [0, 1, 2, 4] r.should be_false end it '連続していないならFalseを返す' do - r = Panel.validate_t [0, 1, 2, 4, 5] + r = Panel.validate_serial [0, 1, 2, 4, 5] r.should be_false end end end - describe 'tチェック単体に於いて' do + describe 'シリアライズチェック単体に於いて' do before do end context 'つつがなく終わるとき' do it '検証値収集を依頼している' do Panel.should_receive(:collect_element_value).with(any_args).exactly(1) Panel.stub(:collect_element_value).with(any_args).and_return([]) - Panel.stub(:validate_t).with(any_args).and_return(true) - r = Panel.validate_element_t [], :t + Panel.stub(:validate_serial).with(any_args).and_return(true) + r = Panel.validate_element_serial [], :t end it 'シリアライズチェック依頼している' do Panel.stub(:collect_element_value).with(any_args).and_return([]) - Panel.should_receive(:validate_t).with(any_args).exactly(1) - Panel.stub(:validate_t).with(any_args).and_return(true) - r = Panel.validate_element_t [], :t + Panel.should_receive(:validate_serial).with(any_args).exactly(1) + Panel.stub(:validate_serial).with(any_args).and_return(true) + r = Panel.validate_element_serial [], :t end end end @@ -1081,13 +1091,13 @@ describe Panel do it 'trueを返している' do @panel = FactoryGirl.build :panel, :author_id => @author.id @panel.panel_pictures.build( - FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0) + FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0, :z => 0+1) ) @panel.panel_pictures.build( - FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1) + FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1, :z => 1+1) ) sb1 = @panel.speech_balloons.build( - FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2) + FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2, :z => 2+1) ) sb1.balloons.build( FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id) @@ -1096,7 +1106,7 @@ describe Panel do FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id) ) sb2 = @panel.speech_balloons.build( - FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 3) + FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 3, :z => 3+1) ) sb2.balloons.build( FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb2.id) @@ -1105,26 +1115,26 @@ describe Panel do FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb2.id) ) @gc = @panel.ground_colors.build( - FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 4) + FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 4, :z => 4+1) ) @gp = @panel.ground_pictures.build( - FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 5) + FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 5, :z => 5+1) ) r = @panel.validate_child r.should be_true end end - context 'シリアライズされていないとき' do + context 'tシリアライズされていないとき' do it 'falseを返している' do @panel = FactoryGirl.build :panel, :author_id => @author.id @panel.panel_pictures.build( - FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0) + FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0, :z => 0+1) ) @panel.panel_pictures.build( - FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1) + FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1, :z => 1+1) ) sb1 = @panel.speech_balloons.build( - FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2) + FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2, :z => 2+1) ) sb1.balloons.build( FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id) @@ -1133,7 +1143,44 @@ describe Panel do FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id) ) sb2 = @panel.speech_balloons.build( - FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4) + FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4, :z => 3+1) + ) + sb2.balloons.build( + FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb2.id) + ) + sb2.speeches.build( + FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb2.id) + ) + @gc = @panel.ground_colors.build( + FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 4, :z => 4+1) + ) + @gp = @panel.ground_pictures.build( + FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 5, :z => 5+1) + ) + r = @panel.validate_child + r.should be_false + end + end + context 'zシリアライズされていないとき' do + it 'falseを返している' do + @panel = FactoryGirl.build :panel, :author_id => @author.id + @panel.panel_pictures.build( + FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0, :z => 0+1) + ) + @panel.panel_pictures.build( + FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1, :z => 0+1) + ) + sb1 = @panel.speech_balloons.build( + FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2, :z => 2+1) + ) + sb1.balloons.build( + FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id) + ) + sb1.speeches.build( + FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id) + ) + sb2 = @panel.speech_balloons.build( + FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 3, :z => 3+1) ) sb2.balloons.build( FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb2.id) @@ -1142,10 +1189,10 @@ describe Panel do FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb2.id) ) @gc = @panel.ground_colors.build( - FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 4) + FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 4, :z => 4+1) ) @gp = @panel.ground_pictures.build( - FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 5) + FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 5, :z => 5+1) ) r = @panel.validate_child r.should be_false