diff options
author | Kohei Yoshida <kohei.yoshida@collabora.com> | 2014-02-25 12:16:45 -0500 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@collabora.com> | 2014-02-25 22:53:28 -0500 |
commit | 3cb186f57d188ad17503d60cf2f8e6a4bbd8523c (patch) | |
tree | d6a7e798352eb0923e18196b829cd278e38e0f66 /sc/qa | |
parent | 5706ff70dbb18d03e396fd484bd1392dbcefb6c7 (diff) |
fdo#75386: Add unit test for this.
Change-Id: Iff2b5fd05eb098e83281c5ae9cedf309edde7250
Diffstat (limited to 'sc/qa')
-rw-r--r-- | sc/qa/unit/helper/qahelper.cxx | 58 | ||||
-rw-r--r-- | sc/qa/unit/helper/qahelper.hxx | 6 | ||||
-rw-r--r-- | sc/qa/unit/ucalc.cxx | 48 | ||||
-rw-r--r-- | sc/qa/unit/ucalc.hxx | 2 | ||||
-rw-r--r-- | sc/qa/unit/ucalc_sharedformula.cxx | 107 |
5 files changed, 177 insertions, 44 deletions
diff --git a/sc/qa/unit/helper/qahelper.cxx b/sc/qa/unit/helper/qahelper.cxx index 773b7791f36b..b875265e16c8 100644 --- a/sc/qa/unit/helper/qahelper.cxx +++ b/sc/qa/unit/helper/qahelper.cxx @@ -430,6 +430,64 @@ bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected return true; } +bool checkFormulaPosition(ScDocument& rDoc, const ScAddress& rPos) +{ + OUString aStr(rPos.Format(SCA_VALID)); + const ScFormulaCell* pFC = rDoc.GetFormulaCell(rPos); + if (!pFC) + { + cerr << "Formula cell expected at " << aStr << " but not found." << endl; + return false; + } + + if (pFC->aPos != rPos) + { + OUString aStr2(pFC->aPos.Format(SCA_VALID)); + cerr << "Formula cell at " << aStr << " has incorrect position of " << aStr2 << endl; + return false; + } + + return true; +} + +bool checkFormulaPositions( + ScDocument& rDoc, SCTAB nTab, SCCOL nCol, const SCROW* pRows, size_t nRowCount) +{ + ScAddress aPos(nCol, 0, nTab); + for (size_t i = 0; i < nRowCount; ++i) + { + SCROW nRow = pRows[i]; + aPos.SetRow(nRow); + + if (!checkFormulaPosition(rDoc, aPos)) + { + OUString aStr(aPos.Format(SCA_VALID)); + cerr << "Formula cell position failed at " << aStr << "." << endl; + return false; + } + } + return true; +} + +void clearFormulaCellChangedFlag( ScDocument& rDoc, const ScRange& rRange ) +{ + const ScAddress& s = rRange.aStart; + const ScAddress& e = rRange.aEnd; + for (SCTAB nTab = s.Tab(); nTab <= e.Tab(); ++nTab) + { + for (SCCOL nCol = s.Col(); nCol <= e.Col(); ++nCol) + { + for (SCROW nRow = s.Row(); nRow <= e.Row(); ++nRow) + { + ScAddress aPos(nCol, nRow, nTab); + ScFormulaCell* pFC = rDoc.GetFormulaCell(aPos); + if (pFC) + pFC->SetChanged(false); + } + } + } +} + bool isFormulaWithoutError(ScDocument& rDoc, const ScAddress& rPos) { ScFormulaCell* pFC = rDoc.GetFormulaCell(rPos); diff --git a/sc/qa/unit/helper/qahelper.hxx b/sc/qa/unit/helper/qahelper.hxx index 657f66b033e9..33a93a6ab7ab 100644 --- a/sc/qa/unit/helper/qahelper.hxx +++ b/sc/qa/unit/helper/qahelper.hxx @@ -120,6 +120,12 @@ SCQAHELPER_DLLPUBLIC ScRangeList getChartRanges(ScDocument& rDoc, const SdrOle2O SCQAHELPER_DLLPUBLIC bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected); +SCQAHELPER_DLLPUBLIC bool checkFormulaPosition(ScDocument& rDoc, const ScAddress& rPos); +SCQAHELPER_DLLPUBLIC bool checkFormulaPositions( + ScDocument& rDoc, SCTAB nTab, SCCOL nCol, const SCROW* pRows, size_t nRowCount); + +SCQAHELPER_DLLPUBLIC void clearFormulaCellChangedFlag( ScDocument& rDoc, const ScRange& rRange ); + /** * Check if the cell at specified position is a formula cell that doesn't * have an error value. diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx index f4c1896740eb..d5d705500cda 100644 --- a/sc/qa/unit/ucalc.cxx +++ b/sc/qa/unit/ucalc.cxx @@ -4036,48 +4036,6 @@ void Test::testSearchCells() m_pDoc->DeleteTab(0); } -namespace { - -bool checkFormulaPosition(ScDocument& rDoc, const ScAddress& rPos) -{ - OUString aStr(rPos.Format(SCA_VALID)); - const ScFormulaCell* pFC = rDoc.GetFormulaCell(rPos); - if (!pFC) - { - cerr << "Formula cell expected at " << aStr << " but not found." << endl; - return false; - } - - if (pFC->aPos != rPos) - { - OUString aStr2(pFC->aPos.Format(SCA_VALID)); - cerr << "Formula cell at " << aStr << " has incorrect position of " << aStr2 << endl; - return false; - } - - return true; -} - -void checkFormulaPositions(ScDocument& rDoc, const ScAddress& rPos, const SCROW* pRows, size_t nRowCount) -{ - ScAddress aPos = rPos; - for (size_t i = 0; i < nRowCount; ++i) - { - SCROW nRow = pRows[i]; - aPos.SetRow(nRow); - - if (!checkFormulaPosition(rDoc, aPos)) - { - OUString aStr(aPos.Format(SCA_VALID)); - std::ostringstream os; - os << "Formula cell position failed at " << aStr; - CPPUNIT_FAIL(os.str().c_str()); - } - } -} - -} - void Test::testFormulaPosition() { m_pDoc->InsertTab(0, "Test"); @@ -4091,13 +4049,15 @@ void Test::testFormulaPosition() { SCROW aRows[] = { 0, 1, 3 }; - checkFormulaPositions(*m_pDoc, aPos, aRows, SAL_N_ELEMENTS(aRows)); + bool bRes = checkFormulaPositions(*m_pDoc, aPos.Tab(), aPos.Col(), aRows, SAL_N_ELEMENTS(aRows)); + CPPUNIT_ASSERT(bRes); } m_pDoc->InsertRow(0,0,0,0,1,5); // Insert 5 rows at A2. { SCROW aRows[] = { 0, 6, 8 }; - checkFormulaPositions(*m_pDoc, aPos, aRows, SAL_N_ELEMENTS(aRows)); + bool bRes = checkFormulaPositions(*m_pDoc, aPos.Tab(), aPos.Col(), aRows, SAL_N_ELEMENTS(aRows)); + CPPUNIT_ASSERT(bRes); } m_pDoc->DeleteTab(0); diff --git a/sc/qa/unit/ucalc.hxx b/sc/qa/unit/ucalc.hxx index 28e015604a97..4f8810aaac50 100644 --- a/sc/qa/unit/ucalc.hxx +++ b/sc/qa/unit/ucalc.hxx @@ -271,6 +271,7 @@ public: void testSharedFormulasRefUpdateCopySheets(); void testSharedFormulasCopyPaste(); void testSharedFormulaInsertColumn(); + void testSharedFormulaMoveBlock(); void testFormulaPosition(); void testMixData(); @@ -442,6 +443,7 @@ public: CPPUNIT_TEST(testSharedFormulasRefUpdateCopySheets); CPPUNIT_TEST(testSharedFormulasCopyPaste); CPPUNIT_TEST(testSharedFormulaInsertColumn); + CPPUNIT_TEST(testSharedFormulaMoveBlock); CPPUNIT_TEST(testFormulaPosition); CPPUNIT_TEST(testMixData); CPPUNIT_TEST(testJumpToPrecedentsDependents); diff --git a/sc/qa/unit/ucalc_sharedformula.cxx b/sc/qa/unit/ucalc_sharedformula.cxx index a64398b0e799..91885ea85f4e 100644 --- a/sc/qa/unit/ucalc_sharedformula.cxx +++ b/sc/qa/unit/ucalc_sharedformula.cxx @@ -15,6 +15,7 @@ #include "clipparam.hxx" #include "undoblk.hxx" #include "scopetools.hxx" +#include <docfunc.hxx> #include "svl/sharedstring.hxx" #include "formula/grammar.hxx" @@ -738,4 +739,110 @@ void Test::testSharedFormulaInsertColumn() m_pDoc->DeleteTab(0); } +void Test::testSharedFormulaMoveBlock() +{ + sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn on auto calc. + FormulaGrammarSwitch aFGSwitch(m_pDoc, formula::FormulaGrammar::GRAM_ENGLISH_XL_R1C1); + + m_pDoc->InsertTab(0, "Test"); + + // Set values to A1:A3. + m_pDoc->SetValue(ScAddress(0,0,0), 1.0); + m_pDoc->SetValue(ScAddress(0,1,0), 2.0); + m_pDoc->SetValue(ScAddress(0,2,0), 3.0); + + // Set formulas in B1:B3 to reference A1:A3. + m_pDoc->SetString(ScAddress(1,0,0), "=RC[-1]"); + m_pDoc->SetString(ScAddress(1,1,0), "=RC[-1]"); + m_pDoc->SetString(ScAddress(1,2,0), "=RC[-1]"); + + ScRange aFormulaRange(1,0,0,1,2,0); + + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + + clearFormulaCellChangedFlag(*m_pDoc, aFormulaRange); + + // Move A1:A3 to D1:D3. + ScDocFunc& rFunc = getDocShell().GetDocFunc(); + rFunc.MoveBlock(ScRange(0,0,0,0,2,0), ScAddress(3,0,0), true, true, false, true); + + // The result should stay the same. + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + + clearFormulaCellChangedFlag(*m_pDoc, aFormulaRange); + + // Make sure these formula cells in B1:B3 have correct positions even after the move. + std::vector<SCROW> aRows; + aRows.push_back(0); + aRows.push_back(1); + aRows.push_back(2); + bool bRes = checkFormulaPositions(*m_pDoc, 0, 1, &aRows[0], aRows.size()); + CPPUNIT_ASSERT(bRes); + + SfxUndoManager* pUndoMgr = m_pDoc->GetUndoManager(); + CPPUNIT_ASSERT(pUndoMgr); + + // Undo and check the result. + pUndoMgr->Undo(); + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + + clearFormulaCellChangedFlag(*m_pDoc, aFormulaRange); + + // Redo and check the result. + pUndoMgr->Redo(); + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + + // Clear the range and start over. + clearRange(m_pDoc, ScRange(0,0,0,MAXCOL,MAXROW,0)); + + // Set values 1,2,3,4,5 to A1:A5. + for (SCROW i = 0; i <= 4; ++i) + m_pDoc->SetValue(ScAddress(0,i,0), (i+1)); + + // Set formulas to B1:B5. + for (SCROW i = 0; i <= 4; ++i) + m_pDoc->SetString(ScAddress(1,i,0), "=RC[-1]"); + + // Check the initial formula results. + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(ScAddress(1,3,0))); + CPPUNIT_ASSERT_EQUAL(5.0, m_pDoc->GetValue(ScAddress(1,4,0))); + + // Move A1:A2 to D2:D3. + rFunc.MoveBlock(ScRange(0,0,0,0,1,0), ScAddress(3,1,0), true, true, false, true); + + // Check the formula values again. They should not change. + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(ScAddress(1,3,0))); + CPPUNIT_ASSERT_EQUAL(5.0, m_pDoc->GetValue(ScAddress(1,4,0))); + + pUndoMgr->Undo(); + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(ScAddress(1,3,0))); + CPPUNIT_ASSERT_EQUAL(5.0, m_pDoc->GetValue(ScAddress(1,4,0))); + + pUndoMgr->Redo(); + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,0,0))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(1,1,0))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,0))); + CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(ScAddress(1,3,0))); + CPPUNIT_ASSERT_EQUAL(5.0, m_pDoc->GetValue(ScAddress(1,4,0))); + + m_pDoc->DeleteTab(0); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |