require File.join(File.expand_path(File.dirname(__FILE__)),"helper") require "set" describe "An empty dots grid" do before :each do @grid = Dots::Grid.new(10,10) end it "should have one box for each edge on left side" do 10.times do |y| @grid[[0,y],[0,y+1]].length.should == 1 end end it "should have one box for each edge on right side" do 10.times do |y| @grid[[10,y],[10,y+1]].length.should == 1 end end it "should have one box for each edge on top side" do 10.times do |x| @grid[[x,10],[x+1,10]].length.should == 1 end end it "should have one box for each edge on bottom side" do 10.times do |x| @grid[[x,0],[x+1,0]].length.should == 1 end end it "should have two boxes for all inner edges" do (1..8).each do |x| (1..8).each do |y| @grid[[x,y],[x+1,y]].length.should == 2 @grid[[x,y],[x,y+1]].length.should == 2 end end end it "should allow connecting adjacent dots" do @grid.connect([0,0],[0,1]) @grid.box_at(0,0).edges[:east].should == :drawn end it "should throw an error when connecting non-adjacent dots" do lambda { @grid.connect([0,0],[0,5]) }.should raise_error(Dots::InvalidEdgeError) end end # in this iteration, I've replaced the notification approach for box completion # detection with a more simple one: connect() just returns a set of which boxes # it completes (if any) describe "A drawn on dots grid" do before :each do @grid = Dots::Grid.new(10,10) @grid.connect [0,0], [0,1] @grid.connect [0,1], [1,1] @grid.connect [1,0], [0,0] end it "should return an empty set when connect() does not complete a box" do (@grid.connect [5,5], [5,4]).should == Set[] end it "should return a set with a box when connect() completes one box" do result = @grid.connect [1,1], [1,0] result.size.should == 1 result.each do |b| b.should be_an_instance_of(Dots::Box) end end it "should return a set of two boxes when connect() completes two boxes" do @grid.connect [1,0], [2,0] @grid.connect [2,0], [2,1] @grid.connect [2,1], [1,1] result = @grid.connect [1,1], [1,0] result.size.should == 2 result.each do |b| b.should be_an_instance_of(Dots::Box) end end end