From 5cf5a8035d16706ce99f2befb802b4d863597324 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 10 Mar 2012 22:08:22 -0500 Subject: [PATCH] More work on verifying tournament round generation --- rails/map-mash/app/models/mash_tournament.rb | 18 ++++----- .../spec/models/mash_tournament_spec.rb | 37 +++++++++++++------ 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/rails/map-mash/app/models/mash_tournament.rb b/rails/map-mash/app/models/mash_tournament.rb index 0a6c4aa..c81d9fb 100644 --- a/rails/map-mash/app/models/mash_tournament.rb +++ b/rails/map-mash/app/models/mash_tournament.rb @@ -2,7 +2,6 @@ class MashTournament < ActiveRecord::Base belongs_to :requester has_many :mashes has_many :rounds, :class_name => 'MashTournamentRound' - #after_create :create_rounds #after_save :fill_in_next_round def next_unplayed_mash @@ -19,20 +18,21 @@ class MashTournament < ActiveRecord::Base ) end - def create_rounds - n_contenders = Map.count + def valid_number_of_contenders?(n_contenders) + [8, 16, 32, 64, 128].include?(n_contenders) + end - #ap "Original n_contenders = #{n_contenders}" - while n_contenders % 4 != 0 - n_contenders -= 1 + def create_rounds(n_contenders) + if not valid_number_of_contenders?(n_contenders) + raise StandardError.new( + "The number of contenders must be 8, 16, 32, 64, or 128! " + + "Got '#{n_contenders}'" + ) end - #ap "Resulting n_contenders divisible by 4 = #{n_contenders}" - round = 1 while n_contenders > 1 - #ap "Generating round #{round}, n_contenders = #{n_contenders}" create_round(round, n_contenders) n_contenders = n_contenders / 2 round += 1 diff --git a/rails/map-mash/spec/models/mash_tournament_spec.rb b/rails/map-mash/spec/models/mash_tournament_spec.rb index f8a9b14..953572e 100644 --- a/rails/map-mash/spec/models/mash_tournament_spec.rb +++ b/rails/map-mash/spec/models/mash_tournament_spec.rb @@ -5,29 +5,42 @@ describe MashTournament do @valid_attributes = {:requester_id => 1} end + before(:each) do + @requester = Requester.make + @requester.save! + subject.requester_id = @requester.id + subject.save! + subject.reload + end + it "should create a new instance given valid attributes" do MashTournament.create!(@valid_attributes) end context 'creating rounds' do + + it 'should reject invalid numbers of contenders' do + [11, 24, 40].each do |n| + expect do + subject.create_rounds(n) + end.to raise_error + end + end + before(:all) do Map.destroy_all - 8.times do + 40.times do Map.make.save end - @requester = Requester.make - @requester.save! end - before(:each) do - subject.requester_id = @requester.id - subject.save! - subject.reload - end - - it 'should create 3 rounds for 8 contenders' do - subject.create_rounds - subject.total_rounds.should == 3 + [[8, 3], [16, 4], [32, 5]].each do |n_contenders,n_rounds| + context "for #{n_contenders} total contenders" do + it "should create #{n_rounds} rounds" do + subject.create_rounds(n_contenders) + subject.total_rounds.should == n_rounds + end + end end end end