diff options
author | Kohei Yoshida <kohei.yoshida@gmail.com> | 2013-07-26 20:07:31 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@gmail.com> | 2013-07-30 23:49:57 -0400 |
commit | 1ce304cca32116de2ff3ea8dcac62a3be61aac77 (patch) | |
tree | 2883d975d27fa19c849e6ee516abd3cc6520d93d /sc | |
parent | 0982c0d95eb4bb986f79038f54ec1697a7255883 (diff) |
Add test for exporting and importing of formula refs from xls format.
Change-Id: I98f17889c8ad19114c055e87f9ab5dc4e864c817
Diffstat (limited to 'sc')
-rw-r--r-- | sc/qa/unit/helper/qahelper.cxx | 43 | ||||
-rw-r--r-- | sc/qa/unit/helper/qahelper.hxx | 11 | ||||
-rw-r--r-- | sc/qa/unit/subsequent_export-test.cxx | 42 | ||||
-rw-r--r-- | sc/qa/unit/ucalc_formula.cxx | 42 |
4 files changed, 96 insertions, 42 deletions
diff --git a/sc/qa/unit/helper/qahelper.cxx b/sc/qa/unit/helper/qahelper.cxx index e145f586afec..6ba96cec0b89 100644 --- a/sc/qa/unit/helper/qahelper.cxx +++ b/sc/qa/unit/helper/qahelper.cxx @@ -10,6 +10,7 @@ #include "qahelper.hxx" #include "csv_handler.hxx" #include "drwlayer.hxx" +#include "compiler.hxx" #include "svx/svdpage.hxx" #include "svx/svdoole2.hxx" @@ -235,6 +236,48 @@ ScRangeList getChartRanges(ScDocument& rDoc, const SdrOle2Obj& rChartObj) return aRanges; } +namespace { + +ScTokenArray* getTokens(ScDocument& rDoc, const ScAddress& rPos) +{ + ScFormulaCell* pCell = rDoc.GetFormulaCell(rPos); + if (!pCell) + return NULL; + + return pCell->GetCode(); +} + +} + +bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected) +{ + ScTokenArray* pCode = getTokens(rDoc, rPos); + if (!pCode) + { + cerr << "Empty token array." << endl; + return false; + } + + OUString aFormula = toString(rDoc, rPos, *pCode); + if (aFormula != OUString::createFromAscii(pExpected)) + { + cerr << "Formula '" << pExpected << "' expected, but '" << aFormula << "' found" << endl; + return false; + } + + return true; +} + +OUString toString( + ScDocument& rDoc, const ScAddress& rPos, ScTokenArray& rArray, formula::FormulaGrammar::Grammar eGram) +{ + ScCompiler aComp(&rDoc, rPos, rArray); + aComp.SetGrammar(eGram); + OUStringBuffer aBuf; + aComp.CreateStringFromTokenArray(aBuf); + return aBuf.makeStringAndClear(); +} + ScDocShellRef ScBootstrapFixture::load( bool bReadWrite, const OUString& rURL, const OUString& rFilter, const OUString &rUserData, const OUString& rTypeName, unsigned int nFilterFlags, unsigned int nClipboardID, diff --git a/sc/qa/unit/helper/qahelper.hxx b/sc/qa/unit/helper/qahelper.hxx index 6e332fe1e357..a4966a15ebfb 100644 --- a/sc/qa/unit/helper/qahelper.hxx +++ b/sc/qa/unit/helper/qahelper.hxx @@ -24,6 +24,7 @@ #include <sfx2/docfilt.hxx> #include "sfx2/docfile.hxx" #include "svl/stritem.hxx" +#include "formula/grammar.hxx" #include <string> #include <sstream> @@ -52,6 +53,7 @@ SC_DLLPUBLIC bool testEqualsWithTolerance( long nVal1, long nVal2, long nTol ); class SdrOle2Obj; class ScRangeList; +class ScTokenArray; // data format for row height tests struct TestParam @@ -95,6 +97,15 @@ SC_DLLPUBLIC std::vector<OUString> getChartRangeRepresentations(const SdrOle2Obj SC_DLLPUBLIC ScRangeList getChartRanges(ScDocument& rDoc, const SdrOle2Obj& rChartObj); +SC_DLLPUBLIC bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected); + +/** + * Convert formula token array to a formula string. + */ +SC_DLLPUBLIC OUString toString( + ScDocument& rDoc, const ScAddress& rPos, ScTokenArray& rArray, + formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE); + inline std::string print(const ScAddress& rAddr) { std::ostringstream str; diff --git a/sc/qa/unit/subsequent_export-test.cxx b/sc/qa/unit/subsequent_export-test.cxx index 1a8eca8aa1f6..19ecd3df88a7 100644 --- a/sc/qa/unit/subsequent_export-test.cxx +++ b/sc/qa/unit/subsequent_export-test.cxx @@ -27,6 +27,7 @@ #include "document.hxx" #include "cellform.hxx" #include "formulacell.hxx" +#include "tokenarray.hxx" #include "svx/svdoole2.hxx" @@ -56,6 +57,7 @@ public: void testInlineArrayXLS(); void testEmbeddedChartXLS(); + void testFormulaReferenceXLS(); CPPUNIT_TEST_SUITE(ScExportTest); CPPUNIT_TEST(test); @@ -70,6 +72,7 @@ public: CPPUNIT_TEST(testNamedRangeBugfdo62729); CPPUNIT_TEST(testInlineArrayXLS); CPPUNIT_TEST(testEmbeddedChartXLS); + CPPUNIT_TEST(testFormulaReferenceXLS); CPPUNIT_TEST_SUITE_END(); private: @@ -405,6 +408,45 @@ void ScExportTest::testEmbeddedChartXLS() xDocSh->DoClose(); } +void ScExportTest::testFormulaReferenceXLS() +{ + ScDocShellRef xShell = loadDoc("formula-reference.", XLS); + CPPUNIT_ASSERT(xShell.Is()); + + ScDocShellRef xDocSh = saveAndReload(xShell, XLS); + xShell->DoClose(); + CPPUNIT_ASSERT(xDocSh.Is()); + + ScDocument* pDoc = xDocSh->GetDocument(); + CPPUNIT_ASSERT(pDoc); + + if (!checkFormula(*pDoc, ScAddress(3,1,0), "$A$2+$B$2+$C$2")) + CPPUNIT_FAIL("Wrong formula in D2"); + + if (!checkFormula(*pDoc, ScAddress(3,2,0), "A3+B3+C3")) + CPPUNIT_FAIL("Wrong formula in D3"); + + if (!checkFormula(*pDoc, ScAddress(3,5,0), "SUM($A$6:$C$6)")) + CPPUNIT_FAIL("Wrong formula in D6"); + + if (!checkFormula(*pDoc, ScAddress(3,6,0), "SUM(A7:C7)")) + CPPUNIT_FAIL("Wrong formula in D7"); + + if (!checkFormula(*pDoc, ScAddress(3,9,0), "$Two.$A$2+$Two.$B$2+$Two.$C$2")) + CPPUNIT_FAIL("Wrong formula in D10"); + + if (!checkFormula(*pDoc, ScAddress(3,10,0), "$Two.A3+$Two.B3+$Two.C3")) + CPPUNIT_FAIL("Wrong formula in D11"); + + if (!checkFormula(*pDoc, ScAddress(3,13,0), "MIN($Two.$A$2:$C$2)")) + CPPUNIT_FAIL("Wrong formula in D14"); + + if (!checkFormula(*pDoc, ScAddress(3,14,0), "MAX($Two.A3:C3)")) + CPPUNIT_FAIL("Wrong formula in D15"); + + xDocSh->DoClose(); +} + ScExportTest::ScExportTest() : ScBootstrapFixture("/sc/qa/unit/data") { diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx index a8a54d351d0a..def390273859 100644 --- a/sc/qa/unit/ucalc_formula.cxx +++ b/sc/qa/unit/ucalc_formula.cxx @@ -155,48 +155,6 @@ void Test::testFormulaRefData() CPPUNIT_ASSERT_MESSAGE("Wrong end position of extended range.", aTest.aEnd == ScAddress(8,6,0)); } -namespace { - -OUString toString( - ScDocument& rDoc, const ScAddress& rPos, ScTokenArray& rArray, FormulaGrammar::Grammar eGram = FormulaGrammar::GRAM_NATIVE) -{ - ScCompiler aComp(&rDoc, rPos, rArray); - aComp.SetGrammar(eGram); - OUStringBuffer aBuf; - aComp.CreateStringFromTokenArray(aBuf); - return aBuf.makeStringAndClear(); -} - -ScTokenArray* getTokens(ScDocument& rDoc, const ScAddress& rPos) -{ - ScFormulaCell* pCell = rDoc.GetFormulaCell(rPos); - if (!pCell) - return NULL; - - return pCell->GetCode(); -} - -bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected) -{ - ScTokenArray* pCode = getTokens(rDoc, rPos); - if (!pCode) - { - cerr << "Empty token array." << endl; - return false; - } - - OUString aFormula = toString(rDoc, rPos, *pCode); - if (aFormula != OUString::createFromAscii(pExpected)) - { - cerr << "Formula '" << pExpected << "' expected, but '" << aFormula << "' found" << endl; - return false; - } - - return true; -} - -} - void Test::testFormulaCompiler() { struct { |