spec/formula_builder_spec.rb in rubyfromexcel-0.0.4 vs spec/formula_builder_spec.rb in rubyfromexcel-0.0.5
- old
+ new
@@ -26,11 +26,11 @@
ruby_for("((1+3)*(12+13/2.0))").should == "((1.0+3.0)*(12.0+13.0/2.0))"
end
it "Should convert percentages appropriately to their float equivalents" do
ruby_for("1+3%").should == "1.0+0.03"
- ruby_for("1+103.12%").should == "1.0+1.0312"
+ ruby_for("1+103.12%").should == "1.0+1.0312000000000001" # Uh oh
end
it "Should convert powers to their ruby equivalent" do
ruby_for("1^12").should == "1.0**12.0"
end
@@ -63,12 +63,17 @@
end
it "should convert sheet names to ruby methods" do
ruby_for("asheetname!A3:B20").should == "sheet1.a('a3','b20')"
ruby_for("'a long sheet name with spaces'!A3:B20").should == "sheet2.a('a3','b20')"
+
end
+ it "should ignore external references in sheet names (because they are not implemented yet)" do
+ ruby_for("'[1]a long sheet name with spaces'!A3:B20").should == "sheet2.a('a3','b20')"
+ end
+
it "should convert named references to ruby methods and inline their values" do
worksheet = mock(:worksheet)
workbook = mock(:workbook)
@builder.formula_cell = mock(:cell,:worksheet => worksheet)
worksheet.should_receive(:named_references).and_return({"one_and2"=>'sheet1.a(\'a1\',\'f10\')'})
@@ -85,17 +90,29 @@
worksheet.should_receive(:workbook).and_return(workbook)
workbook.should_receive(:named_references).and_return({"reference_one" => "sheet10.a1","ef_natural_gas_n2o"=> "sheet10.a1"})
ruby_for("-($AG70+$X70)*EF.NaturalGas.N2O").should == "-(ag70+x70)*sheet10.a1"
end
+ it "Edge case: it should convert formulae that refer to an error on a particular sheet made in a named reference definition" do
+ @builder.formula_cell = nil
+ ruby_for("Control!#REF!").should == ":name"
+ end
+
it "should convert table names to references" do
Table.new(mock(:worksheet,:to_s => 'sheet1'),'Vectors','a1:c41',['ColA','Description','ColC'],1)
ruby_for("Vectors[Description]").should == "sheet1.a('b2','b40')"
ruby_for("Vectors[#all]").should == "sheet1.a('a1','c41')"
ruby_for("Vectors[#totals]").should == "sheet1.a('a41','c41')"
ruby_for("Vectors[[#totals],[Description]]").should == "sheet1.b41"
@builder.formula_cell = mock(:cell,:reference => Reference.new('f30'))
ruby_for("Vectors[[#This Row],[Description]]").should == "sheet1.b30"
+ ruby_for("Vectors[[#This Row],[Description]:[ColC]]").should == "sheet1.a('b30','c30')"
+ end
+
+ it "should ignore external references, assuming that they point to an internal equivalent" do
+ formula_with_external = "INDEX([1]!Modules[Module], MATCH($C5, [1]!Modules[Code], 0))"
+ Table.new(mock(:worksheet,:to_s => 'sheet1'),'Modules','a1:c41',['Module','Code','ColC'],1)
+ ruby_for(formula_with_external).should == "index(sheet1.a('a2','a40'),match(c5,sheet1.a('b2','b40'),0.0))"
end
it "should convert unkown table names to :ref" do
ruby_for("Unknown[Not likely]").should == ":ref"
end