Commit cadc4b42 authored by Nikolai Kosjar's avatar Nikolai Kosjar

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: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 41aa2cb3
......@@ -99,8 +99,7 @@ public:
QCOMPARE(QLatin1String(result), QLatin1String(expected));
testDocument.m_editorWidget->undo();
const QByteArray contentsAfterUndo
= testDocument.m_editorWidget->document()->toPlainText().toUtf8();
const QString contentsAfterUndo = testDocument.m_editorWidget->document()->toPlainText();
QCOMPARE(contentsAfterUndo, testDocument.m_source);
}
};
......
......@@ -43,7 +43,7 @@ namespace Tests {
TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source, char 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_editorWidget(0)
{
......
......@@ -77,12 +77,13 @@ QuickFixTestDocument::QuickFixTestDocument(const QByteArray &fileName,
const QByteArray &source,
const QByteArray &expectedSource)
: TestDocument(fileName, source)
, m_expectedSource(expectedSource)
, m_expectedSource(QString::fromUtf8(expectedSource))
{
if (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)
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
/// 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);
for (int i = 0, total = lines.size(); i < total; ++i) {
QByteArray line = lines.at(i);
QString line = lines.at(i);
while (line.length() > 0) {
char lastChar = line[line.length() - 1];
if (lastChar == ' ' || lastChar == '\t')
QChar lastChar = line[line.length() - 1];
if (lastChar == QLatin1Char(' ') || lastChar == QLatin1Char('\t'))
line.chop(1);
else
break;
......@@ -113,7 +114,7 @@ static QByteArray &removeTrailingWhitespace(QByteArray &input)
const bool isLastLine = i == lines.size() - 1;
if (!isLastLine)
input.append('\n');
input.append(QLatin1Char('\n'));
}
return input;
}
......@@ -197,14 +198,14 @@ QuickFixTestCase::QuickFixTestCase(const QList<QuickFixTestDocument::Ptr> &theTe
// Compare all files
foreach (const QuickFixTestDocument::Ptr testFile, m_testFiles) {
// Check
QByteArray result = testFile->m_editorWidget->document()->toPlainText().toUtf8();
QString result = testFile->m_editorWidget->document()->toPlainText();
removeTrailingWhitespace(result);
QCOMPARE(QLatin1String(result), QLatin1String(testFile->m_expectedSource));
QCOMPARE(result, testFile->m_expectedSource);
// Undo the change
for (int i = 0; i < 100; ++i)
testFile->m_editorWidget->undo();
result = testFile->m_editorWidget->document()->toPlainText().toUtf8();
result = testFile->m_editorWidget->document()->toPlainText();
QCOMPARE(result, testFile->m_source);
}
}
......
......@@ -62,7 +62,7 @@ public:
const QByteArray &expectedSource);
public:
QByteArray m_expectedSource;
QString m_expectedSource;
};
/**
......
......@@ -175,7 +175,7 @@ class TestDocument : public CppEditor::Internal::Tests::TestDocument
public:
TestDocument(const QByteArray &source, const QByteArray &fileName)
: CppEditor::Internal::Tests::TestDocument(fileName, source)
, m_targetCursorPosition(source.indexOf('$'))
, m_targetCursorPosition(m_source.indexOf(QLatin1Char('$')))
{
if (m_cursorPosition != -1 || m_targetCursorPosition != -1)
QVERIFY(m_cursorPosition != m_targetCursorPosition);
......
......@@ -61,20 +61,21 @@ namespace CppTools {
namespace Tests {
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
{
const QString fileNameAsString = QString::fromUtf8(m_fileName);
if (!QFileInfo(fileNameAsString).isAbsolute())
return QDir::tempPath() + QLatin1Char('/') + fileNameAsString;
return fileNameAsString;
if (!QFileInfo(m_fileName).isAbsolute())
return QDir::tempPath() + QLatin1Char('/') + m_fileName;
return m_fileName;
}
bool TestDocument::writeToDisk() const
{
return TestCase::writeFile(filePath(), m_source);
return TestCase::writeFile(filePath(), m_source.toUtf8());
}
TestCase::TestCase(bool runGarbageCollector)
......
......@@ -56,8 +56,8 @@ public:
bool writeToDisk() const;
public:
QByteArray m_fileName;
QByteArray m_source;
QString m_fileName;
QString m_source;
char m_cursorMarker;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment