spec/formula_peg_spec.rb in rubyfromexcel-0.0.10 vs spec/formula_peg_spec.rb in rubyfromexcel-0.0.13

- old
+ new

@@ -1,14 +1,18 @@ require_relative 'spec_helper' require 'textpeg2rubypeg' -text_peg = IO.readlines(File.join(File.dirname(__FILE__),'..','lib','formulae','parse','formula_peg.txt')).join -ast = TextPeg.parse(text_peg) +text_peg = File.join(File.dirname(__FILE__),'..','lib','formulae','parse','formula_peg.txt') +ruby_peg = File.join(File.dirname(__FILE__),'..','lib','formulae','parse','formula_peg.rb') +ast = TextPeg.parse(IO.readlines(text_peg).join) # puts ast.to_ast # exit builder = TextPeg2RubyPeg.new ruby = ast.visit(builder) +File.open(ruby_peg,'w') do |f| + f.puts ruby +end Kernel.eval(ruby) describe Formula do def check(text) @@ -76,18 +80,23 @@ end it "returns strings" do Formula.parse('"A handy string"').to_ast.should == [:formula,[:string,"A handy string"]] Formula.parse('"$A$1"').to_ast.should == [:formula,[:string,"$A$1"]] + Formula.parse('"Double quotes "" do not end strings."').to_ast.should == [:formula,[:string,"Double quotes \"\" do not end strings."]] + Formula.parse('"A ""quote"""').to_ast.should == [:formula,[:string,"A \"\"quote\"\""]] end it "returns string joins" do Formula.parse('"A handy string"&$A$1').to_ast.should == [:formula,[:string_join,[:string,"A handy string"],[:cell,'$A$1']]] Formula.parse('$A$1&"A handy string"').to_ast.should == [:formula,[:string_join,[:cell,'$A$1'],[:string,"A handy string"]]] Formula.parse('$A$1&"A handy string"&$A$1').to_ast.should == [:formula,[:string_join,[:cell,'$A$1'],[:string,"A handy string"],[:cell,'$A$1'],]] Formula.parse('$A$1&$A$1&$A$1').to_ast.should == [:formula,[:string_join,[:cell,'$A$1'],[:cell,'$A$1'],[:cell,'$A$1'],]] Formula.parse('"GW"&ISERR($AA$1)').to_ast.should == [:formula,[:string_join,[:string,'GW'],[:function,'ISERR',[:cell,'$AA$1']]]] + Formula.parse("\"Approximate total \"&$F$87&\" GW locations\"").to_ast.should == [:formula, [:string_join, [:string, "Approximate total "], [:cell, "$F$87"], [:string, " GW locations"]]] + Formula.parse("\"Approximate total \"&$F$87/$F$87&\" GW locations\"").to_ast.should == [:formula, [:string_join, [:string, "Approximate total "], [:arithmetic,[:cell,'$F$87'],[:operator,"/"],[:cell,'$F$87']], [:string, " GW locations"]]] + Formula.parse("\"Approximate total \"&$F$87/Unit.GW&\" GW locations\"").to_ast.should == [:formula, [:string_join, [:string, "Approximate total "], [:arithmetic,[:cell,'$F$87'],[:operator,"/"],[:named_reference,'Unit.GW']], [:string, " GW locations"]]] end it "returns numeric operations" do Formula.parse('$A$1+$A$2+1').to_ast.should == [:formula,[:arithmetic,[:cell,'$A$1'],[:operator,"+"],[:cell,'$A$2'],[:operator,"+"],[:number,'1']]] Formula.parse('$A$1-$A$2-1').to_ast.should == [:formula,[:arithmetic,[:cell,'$A$1'],[:operator,"-"],[:cell,'$A$2'],[:operator,"-"],[:number,'1']]] @@ -153,10 +162,13 @@ Formula.parse(complex).to_ast.should == [:formula, [:brackets, [:prefix, "-", [:quoted_sheet_reference, "DUKES 09 (2.5)", [:cell, "$B$25"]]]]] complex2 = %q{-(INDEX(INDIRECT(BI$19&"!Year.Matrix"),MATCH("Subtotal.Supply",INDIRECT(BI$19&"!Year.Modules"),0),MATCH("V.04",INDIRECT(BI$19&"!Year.Vectors"),0))+ INDEX(INDIRECT(BI$19&"!Year.Matrix"),MATCH("Subtotal.Consumption",INDIRECT(BI$19&"!Year.Modules"),0),MATCH("V.04",INDIRECT(BI$19&"!Year.Vectors"),0)))} Formula.parse(complex2).to_ast.should == [:formula, [:prefix, "-", [:brackets, [:arithmetic, [:function, "INDEX", [:function, "INDIRECT", [:string_join, [:cell, "BI$19"], [:string, "!Year.Matrix"]]], [:function, "MATCH", [:string, "Subtotal.Supply"], [:function, "INDIRECT", [:string_join, [:cell, "BI$19"], [:string, "!Year.Modules"]]], [:number, "0"]], [:function, "MATCH", [:string, "V.04"], [:function, "INDIRECT", [:string_join, [:cell, "BI$19"], [:string, "!Year.Vectors"]]], [:number, "0"]]], [:operator, "+"], [:function, "INDEX", [:function, "INDIRECT", [:string_join, [:cell, "BI$19"], [:string, "!Year.Matrix"]]], [:function, "MATCH", [:string, "Subtotal.Consumption"], [:function, "INDIRECT", [:string_join, [:cell, "BI$19"], [:string, "!Year.Modules"]]], [:number, "0"]], [:function, "MATCH", [:string, "V.04"], [:function, "INDIRECT", [:string_join, [:cell, "BI$19"], [:string, "!Year.Vectors"]]], [:number, "0"]]]]]]] Formula.parse("MAX(MIN(F121, -F22),0)").to_ast.should == [:formula, [:function, "MAX", [:function, "MIN", [:cell, "F121"], [:prefix, "-", [:cell, "F22"]]], [:number, "0"]]] + complex3 = 'IFERROR(INDEX(INDIRECT($C102&".Outputs["&this.Year&"]"), MATCH(Z$5, INDIRECT($C102&".Outputs[Vector]"), 0)), 0)' + Formula.parse(complex3).to_ast.should == [:formula, [:function, "IFERROR", [:function, "INDEX", [:function, "INDIRECT", [:string_join, [:cell, "$C102"], [:string, ".Outputs["], [:named_reference, "this.Year"], [:string, "]"]]], [:function, "MATCH", [:cell, "Z$5"], [:function, "INDIRECT", [:string_join, [:cell, "$C102"], [:string, ".Outputs[Vector]"]]], [:number, "0"]]], [:number, "0"]]] + Formula.parse('INDIRECT($C102&".Outputs["&this.Year&"]")').to_ast.should == [:formula, [:function, "INDIRECT", [:string_join, [:cell, "$C102"], [:string, ".Outputs["], [:named_reference, "this.Year"], [:string, "]"]]]] end it "returns formulas with spaces" do spaced = "(13.56 / 96935) * EF.IndustrialCoal.CO2 * GWP.CH4" Formula.parse(spaced).to_ast.should == [:formula, [:arithmetic, [:brackets, [:arithmetic, [:number, "13.56"], [:operator, "/"], [:number, "96935"]]], [:operator, "*"], [:named_reference, "EF.IndustrialCoal.CO2"], [:operator, "*"], [:named_reference, "GWP.CH4"]]] @@ -181,6 +193,9 @@ it "parses buggy references" do Formula.parse("'Calcs'!a17").to_ast.should == [:formula,[:quoted_sheet_reference,'Calcs',[:cell,'a17']]] Formula.parse("'Calcs'!aone2").to_ast.should == [:formula,[:quoted_sheet_reference,'Calcs',[:named_reference,'aone2']]] end + it "parses tricky indirect match table combos" do + Formula.parse(%q|INDEX(INDIRECT("'"&XVI.a.Inputs[#Headers]&"'!Year.Matrix"), MATCH("Subtotal."&$A$2, INDIRECT("'"&XVI.a.Inputs[#Headers]&"'!Year.Modules"), 0), MATCH([Vector], INDIRECT("'"&XVI.a.Inputs[#Headers]&"'!Year.Vectors"), 0))|).to_ast.should == [:formula, [:function, "INDEX", [:function, "INDIRECT", [:string_join, [:string, "'"], [:table_reference, "XVI.a.Inputs", "#Headers"], [:string, "'!Year.Matrix"]]], [:function, "MATCH", [:string_join, [:string, "Subtotal."], [:cell, "$A$2"]], [:function, "INDIRECT", [:string_join, [:string, "'"], [:table_reference, "XVI.a.Inputs", "#Headers"], [:string, "'!Year.Modules"]]], [:number, "0"]], [:function, "MATCH", [:local_table_reference, "Vector"], [:function, "INDIRECT", [:string_join, [:string, "'"], [:table_reference, "XVI.a.Inputs", "#Headers"], [:string, "'!Year.Vectors"]]], [:number, "0"]]]] + end end \ No newline at end of file