Cpp{Tools,Editor}: Tests: Use QString instead of QByteArray

This is necessary in order to add tests with multi-byte UTF-8 code
points. Otherwise the initial and target source code marker positions
will be calculated on the QByteArray (test code) but used with a QString
(editor document).

Change-Id: I108961b13d32912a4d3193cf26eb59f65d296f57
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
Nikolai Kosjar
2014-05-08 13:21:42 -04:00
parent 41aa2cb3bd
commit cadc4b42ba
7 changed files with 25 additions and 24 deletions

View File

@@ -99,8 +99,7 @@ public:
QCOMPARE(QLatin1String(result), QLatin1String(expected)); QCOMPARE(QLatin1String(result), QLatin1String(expected));
testDocument.m_editorWidget->undo(); testDocument.m_editorWidget->undo();
const QByteArray contentsAfterUndo const QString contentsAfterUndo = testDocument.m_editorWidget->document()->toPlainText();
= testDocument.m_editorWidget->document()->toPlainText().toUtf8();
QCOMPARE(contentsAfterUndo, testDocument.m_source); QCOMPARE(contentsAfterUndo, testDocument.m_source);
} }
}; };

View File

@@ -43,7 +43,7 @@ namespace Tests {
TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker) TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker)
: CppTools::Tests::TestDocument(fileName, source, cursorMarker) : CppTools::Tests::TestDocument(fileName, source, cursorMarker)
, m_cursorPosition(source.indexOf(m_cursorMarker)) , m_cursorPosition(m_source.indexOf(QLatin1Char(m_cursorMarker)))
, m_editor(0) , m_editor(0)
, m_editorWidget(0) , m_editorWidget(0)
{ {

View File

@@ -77,12 +77,13 @@ QuickFixTestDocument::QuickFixTestDocument(const QByteArray &fileName,
const QByteArray &source, const QByteArray &source,
const QByteArray &expectedSource) const QByteArray &expectedSource)
: TestDocument(fileName, source) : TestDocument(fileName, source)
, m_expectedSource(expectedSource) , m_expectedSource(QString::fromUtf8(expectedSource))
{ {
if (m_cursorPosition > -1) if (m_cursorPosition > -1)
m_source.remove(m_cursorPosition, 1); m_source.remove(m_cursorPosition, 1);
const int cursorPositionInExpectedSource = m_expectedSource.indexOf(m_cursorMarker); const int cursorPositionInExpectedSource
= m_expectedSource.indexOf(QLatin1Char(m_cursorMarker));
if (cursorPositionInExpectedSource > -1) if (cursorPositionInExpectedSource > -1)
m_expectedSource.remove(cursorPositionInExpectedSource, 1); m_expectedSource.remove(cursorPositionInExpectedSource, 1);
} }
@@ -96,15 +97,15 @@ QList<QuickFixTestDocument::Ptr> singleDocument(const QByteArray &original,
/// Leading whitespace is not removed, so we can check if the indetation ranges /// Leading whitespace is not removed, so we can check if the indetation ranges
/// have been set correctly by the quick-fix. /// have been set correctly by the quick-fix.
static QByteArray &removeTrailingWhitespace(QByteArray &input) static QString &removeTrailingWhitespace(QString &input)
{ {
QList<QByteArray> lines = input.split('\n'); const QStringList lines = input.split(QLatin1Char('\n'));
input.resize(0); input.resize(0);
for (int i = 0, total = lines.size(); i < total; ++i) { for (int i = 0, total = lines.size(); i < total; ++i) {
QByteArray line = lines.at(i); QString line = lines.at(i);
while (line.length() > 0) { while (line.length() > 0) {
char lastChar = line[line.length() - 1]; QChar lastChar = line[line.length() - 1];
if (lastChar == ' ' || lastChar == '\t') if (lastChar == QLatin1Char(' ') || lastChar == QLatin1Char('\t'))
line.chop(1); line.chop(1);
else else
break; break;
@@ -113,7 +114,7 @@ static QByteArray &removeTrailingWhitespace(QByteArray &input)
const bool isLastLine = i == lines.size() - 1; const bool isLastLine = i == lines.size() - 1;
if (!isLastLine) if (!isLastLine)
input.append('\n'); input.append(QLatin1Char('\n'));
} }
return input; return input;
} }
@@ -197,14 +198,14 @@ QuickFixTestCase::QuickFixTestCase(const QList<QuickFixTestDocument::Ptr> &theTe
// Compare all files // Compare all files
foreach (const QuickFixTestDocument::Ptr testFile, m_testFiles) { foreach (const QuickFixTestDocument::Ptr testFile, m_testFiles) {
// Check // Check
QByteArray result = testFile->m_editorWidget->document()->toPlainText().toUtf8(); QString result = testFile->m_editorWidget->document()->toPlainText();
removeTrailingWhitespace(result); removeTrailingWhitespace(result);
QCOMPARE(QLatin1String(result), QLatin1String(testFile->m_expectedSource)); QCOMPARE(result, testFile->m_expectedSource);
// Undo the change // Undo the change
for (int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
testFile->m_editorWidget->undo(); testFile->m_editorWidget->undo();
result = testFile->m_editorWidget->document()->toPlainText().toUtf8(); result = testFile->m_editorWidget->document()->toPlainText();
QCOMPARE(result, testFile->m_source); QCOMPARE(result, testFile->m_source);
} }
} }

View File

@@ -62,7 +62,7 @@ public:
const QByteArray &expectedSource); const QByteArray &expectedSource);
public: public:
QByteArray m_expectedSource; QString m_expectedSource;
}; };
/** /**

View File

@@ -175,7 +175,7 @@ class TestDocument : public CppEditor::Internal::Tests::TestDocument
public: public:
TestDocument(const QByteArray &source, const QByteArray &fileName) TestDocument(const QByteArray &source, const QByteArray &fileName)
: CppEditor::Internal::Tests::TestDocument(fileName, source) : CppEditor::Internal::Tests::TestDocument(fileName, source)
, m_targetCursorPosition(source.indexOf('$')) , m_targetCursorPosition(m_source.indexOf(QLatin1Char('$')))
{ {
if (m_cursorPosition != -1 || m_targetCursorPosition != -1) if (m_cursorPosition != -1 || m_targetCursorPosition != -1)
QVERIFY(m_cursorPosition != m_targetCursorPosition); QVERIFY(m_cursorPosition != m_targetCursorPosition);

View File

@@ -61,20 +61,21 @@ namespace CppTools {
namespace Tests { namespace Tests {
TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker) TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker)
: m_fileName(fileName), m_source(source), m_cursorMarker(cursorMarker) : m_fileName(QString::fromUtf8(fileName))
, m_source(QString::fromUtf8(source))
, m_cursorMarker(cursorMarker)
{} {}
QString TestDocument::filePath() const QString TestDocument::filePath() const
{ {
const QString fileNameAsString = QString::fromUtf8(m_fileName); if (!QFileInfo(m_fileName).isAbsolute())
if (!QFileInfo(fileNameAsString).isAbsolute()) return QDir::tempPath() + QLatin1Char('/') + m_fileName;
return QDir::tempPath() + QLatin1Char('/') + fileNameAsString; return m_fileName;
return fileNameAsString;
} }
bool TestDocument::writeToDisk() const bool TestDocument::writeToDisk() const
{ {
return TestCase::writeFile(filePath(), m_source); return TestCase::writeFile(filePath(), m_source.toUtf8());
} }
TestCase::TestCase(bool runGarbageCollector) TestCase::TestCase(bool runGarbageCollector)

View File

@@ -56,8 +56,8 @@ public:
bool writeToDisk() const; bool writeToDisk() const;
public: public:
QByteArray m_fileName; QString m_fileName;
QByteArray m_source; QString m_source;
char m_cursorMarker; char m_cursorMarker;
}; };