require_relative 'spec_helper' describe SharingFormulaCell do it "it is given a pre-parsed formula and offsets its references by the amount of its shared_formula_offset" do worksheet = mock(:worksheet,:class_name => 'Sheet1',:to_s => 'sheet1') sharing_cell = SharingFormulaCell.new( worksheet, Nokogiri::XML('A1+B21').root ) shared_cell = SharedFormulaCell.new( worksheet, Nokogiri::XML('2').root ) worksheet.should_receive(:cell).with('w8').and_return(shared_cell) sharing_cell.alter_other_cells_if_required sharing_cell.to_ruby.should == "def w7; @w7 ||= a1+b2; end\n" shared_cell.to_ruby.should == "def w8; @w8 ||= a2+b3; end\n" end it "knows what it depends upon" do worksheet = mock(:worksheet,:class_name => 'Sheet1',:to_s => 'sheet1') sharing_cell = SharingFormulaCell.new( worksheet, Nokogiri::XML('A1+B21').root ) shared_cell = SharedFormulaCell.new( worksheet, Nokogiri::XML('2').root ) worksheet.should_receive(:cell).with('w8').and_return(shared_cell) sharing_cell.alter_other_cells_if_required sharing_cell.work_out_dependencies shared_cell.work_out_dependencies sharing_cell.dependencies.should == ['sheet1.a1','sheet1.b2'] shared_cell.dependencies.should == ['sheet1.a2','sheet1.b3'] end it "it should cope if it is sharing only with itself" do worksheet = mock(:worksheet,:class_name => 'Sheet1') sharing_cell = SharingFormulaCell.new( worksheet, Nokogiri::XML('A1+B21').root ) sharing_cell.alter_other_cells_if_required sharing_cell.to_ruby.should == "def w7; @w7 ||= a1+b2; end\n" end it "it should cope if it is sharing to a formula that doesn't exist" do worksheet = mock(:worksheet,:class_name => 'Sheet1') sharing_cell = SharingFormulaCell.new( worksheet, Nokogiri::XML('A1+B21').root ) worksheet.should_receive(:cell).with('w8').and_return(nil) sharing_cell.alter_other_cells_if_required sharing_cell.to_ruby.should == "def w7; @w7 ||= a1+b2; end\n" end it "it should cope if it is sharing with a cell that has its own formula" do worksheet = mock(:worksheet,:class_name => 'Sheet1') sharing_cell = SharingFormulaCell.new( worksheet, Nokogiri::XML('A1+B21').root ) shared_cell = SimpleFormulaCell.new( worksheet, Nokogiri::XML('AND(1,2)1').root ) worksheet.should_receive(:cell).with('w8').and_return(shared_cell) sharing_cell.alter_other_cells_if_required sharing_cell.to_ruby.should == "def w7; @w7 ||= a1+b2; end\n" shared_cell.to_ruby.should == "def w8; @w8 ||= excel_and(1.0,2.0); end\n" end end