Commit 87a6f003 authored by Christian Stenger's avatar Christian Stenger Committed by Christian Stenger

AutoTest: Split off TestResult...

...to avoid dragging around members that are not used in sub-classes.

Change-Id: I3e30b403491b1ae9219a0b461c3034cb854701a6
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent b58a10df
......@@ -270,29 +270,29 @@ void QtTestOutputReader::processOutput()
const QStringRef currentTag = m_xmlReader.name();
if (currentTag == QStringLiteral("TestFunction")) {
if (!m_duration.isEmpty()) {
TestResultPtr testResult = TestResultPtr(new QTestResult(m_className));
testResult->setTestCase(m_testCase);
QTestResult *testResult = new QTestResult(m_className);
testResult->setFunctionName(m_testCase);
testResult->setResult(Result::MessageInternal);
testResult->setDescription(tr("Execution took %1 ms.").arg(m_duration));
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
}
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} else if (currentTag == QStringLiteral("TestCase")) {
TestResultPtr testResult = TestResultPtr(new QTestResult(m_className));
QTestResult *testResult = new QTestResult(m_className);
testResult->setResult(Result::MessageTestCaseEnd);
testResult->setDescription(
m_duration.isEmpty() ? tr("Test finished.")
: tr("Test execution took %1 ms.").arg(m_duration));
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
} else if (validEndTags.contains(currentTag.toString())) {
TestResultPtr testResult = TestResultPtr(new QTestResult(m_className));
testResult->setTestCase(m_testCase);
QTestResult *testResult = new QTestResult(m_className);
testResult->setFunctionName(m_testCase);
testResult->setDataTag(m_dataTag);
testResult->setResult(m_result);
testResult->setFileName(m_file);
testResult->setLine(m_lineNumber);
testResult->setDescription(m_description);
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
}
break;
}
......@@ -367,11 +367,11 @@ void GTestOutputReader::processOutput()
}
if (testEnds.exactMatch(line)) {
TestResultPtr testResult = TestResultPtr(new GTestResult(m_currentTestName));
testResult->setTestCase(m_currentTestSet);
GTestResult *testResult = new GTestResult(m_currentTestName);
testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::MessageTestCaseEnd);
testResult->setDescription(tr("Test execution took %1").arg(testEnds.cap(2)));
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
m_currentTestName.clear();
m_currentTestSet.clear();
} else if (newTestStarts.exactMatch(line)) {
......@@ -394,21 +394,21 @@ void GTestOutputReader::processOutput()
m_futureInterface.reportResult(testResult);
m_description.clear();
} else if (testSetSuccess.exactMatch(line)) {
TestResultPtr testResult = TestResultPtr(new GTestResult(m_currentTestName));
testResult->setTestCase(m_currentTestSet);
GTestResult *testResult = new GTestResult(m_currentTestName);
testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::Pass);
testResult->setDescription(m_description);
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
m_description.clear();
testResult = TestResultPtr(new GTestResult(m_currentTestName));
testResult->setTestCase(m_currentTestSet);
testResult = new GTestResult(m_currentTestName);
testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::MessageInternal);
testResult->setDescription(tr("Execution took %1.").arg(testSetSuccess.cap(2)));
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} else if (testSetFail.exactMatch(line)) {
TestResultPtr testResult = TestResultPtr(new GTestResult(m_currentTestName));
testResult->setTestCase(m_currentTestSet);
GTestResult *testResult = new GTestResult(m_currentTestName);
testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::Fail);
m_description.chop(1);
testResult->setDescription(m_description);
......@@ -423,13 +423,13 @@ void GTestOutputReader::processOutput()
break;
}
}
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
m_description.clear();
testResult = TestResultPtr(new GTestResult(m_currentTestName));
testResult->setTestCase(m_currentTestSet);
testResult = new GTestResult(m_currentTestName);
testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::MessageInternal);
testResult->setDescription(tr("Execution took %1.").arg(testSetFail.cap(2)));
m_futureInterface.reportResult(testResult);
m_futureInterface.reportResult(TestResultPtr(testResult));
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
}
}
......
......@@ -41,14 +41,16 @@ TestResult::TestResult()
{
}
TestResult::TestResult(const QString &className)
: m_class(className)
, m_result(Result::Invalid)
, m_line(0)
, m_type(TestTypeQt)
TestResult::TestResult(const QString &name)
: m_name(name)
{
}
const QString TestResult::outputString(bool selected) const
{
return selected ? m_description : m_description.split(QLatin1Char('\n')).first();
}
Result::Type TestResult::resultFromString(const QString &resultString)
{
if (resultString == QLatin1String("pass"))
......@@ -146,23 +148,71 @@ QColor TestResult::colorForType(const Result::Type type)
}
}
bool operator==(const TestResult &t1, const TestResult &t2)
QTestResult::QTestResult(const QString &className)
: TestResult(className)
{
return t1.className() == t2.className()
&& t1.testCase() == t2.testCase()
&& t1.dataTag() == t2.dataTag()
&& t1.result() == t2.result();
}
QTestResult::QTestResult(const QString &className)
: TestResult(className)
const QString QTestResult::outputString(bool selected) const
{
const QString &desc = description();
const QString &className = name();
QString output;
switch (result()) {
case Result::Pass:
case Result::Fail:
case Result::ExpectedFail:
case Result::UnexpectedPass:
case Result::BlacklistedFail:
case Result::BlacklistedPass:
output = className + QLatin1String("::") + m_function;
if (!m_dataTag.isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(m_dataTag));
if (selected && !desc.isEmpty()) {
output.append(QLatin1Char('\n')).append(desc);
}
break;
case Result::Benchmark:
output = className + QLatin1String("::") + m_function;
if (!m_dataTag.isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(m_dataTag));
if (!desc.isEmpty()) {
int breakPos = desc.indexOf(QLatin1Char('('));
output.append(QLatin1String(": ")).append(desc.left(breakPos));
if (selected)
output.append(QLatin1Char('\n')).append(desc.mid(breakPos));
}
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
}
return output;
}
GTestResult::GTestResult(const QString &name)
: TestResult(name)
{
}
GTestResult::GTestResult(const QString &className)
: TestResult(className)
const QString GTestResult::outputString(bool selected) const
{
setTestType(TestTypeGTest);
const QString &desc = description();
QString output;
switch (result()) {
case Result::Pass:
case Result::Fail:
output = m_testSetName;
if (selected && !desc.isEmpty())
output.append(QLatin1Char('\n')).append(desc);
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
}
return output;
}
} // namespace Internal
......
......@@ -67,25 +67,22 @@ enum Type {
class TestResult
{
public:
TestResult();
TestResult(const QString &className);
explicit TestResult();
explicit TestResult(const QString &name);
virtual ~TestResult() {}
QString className() const { return m_class; }
QString testCase() const { return m_case; }
QString dataTag() const { return m_dataTag; }
virtual const QString outputString(bool selected) const;
QString name() const { return m_name; }
Result::Type result() const { return m_result; }
QString description() const { return m_description; }
QString fileName() const { return m_file; }
int line() const { return m_line; }
TestType type() const { return m_type; }
void setDescription(const QString &description) { m_description = description; }
void setFileName(const QString &fileName) { m_file = fileName; }
void setLine(int line) { m_line = line; }
void setResult(Result::Type type) { m_result = type; }
void setTestCase(const QString &testCase) { m_case = testCase; }
void setDataTag(const QString &dataTag) { m_dataTag = dataTag; }
void setTestType(TestType type) { m_type = type; }
static Result::Type resultFromString(const QString &resultString);
static Result::Type toResultType(int rt);
......@@ -93,15 +90,11 @@ public:
static QColor colorForType(const Result::Type type);
private:
QString m_class;
QString m_case;
QString m_dataTag;
Result::Type m_result;
QString m_name;
Result::Type m_result = Result::Invalid;
QString m_description;
QString m_file;
int m_line;
TestType m_type;
// environment?
int m_line = 0;
};
using TestResultPtr = QSharedPointer<TestResult>;
......@@ -115,16 +108,28 @@ public:
class QTestResult : public TestResult
{
public:
QTestResult(const QString &className = QString());
explicit QTestResult(const QString &className = QString());
const QString outputString(bool selected) const override;
void setFunctionName(const QString &functionName) { m_function = functionName; }
void setDataTag(const QString &dataTag) { m_dataTag = dataTag; }
private:
QString m_function;
QString m_dataTag;
};
class GTestResult : public TestResult
{
public:
GTestResult(const QString &className = QString());
};
explicit GTestResult(const QString &name = QString());
const QString outputString(bool selected) const override;
bool operator==(const TestResult &t1, const TestResult &t2);
void setTestSetName(const QString &testSetName) { m_testSetName = testSetName; }
private:
QString m_testSetName;
};
} // namespace Internal
} // namespace Autotest
......
......@@ -43,46 +43,6 @@ TestResultDelegate::TestResultDelegate(QObject *parent)
{
}
QString TestResultDelegate::outputString(const TestResult &testResult, bool selected)
{
const QString desc = testResult.description();
QString output;
switch (testResult.result()) {
case Result::Pass:
case Result::Fail:
case Result::ExpectedFail:
case Result::UnexpectedPass:
case Result::BlacklistedFail:
case Result::BlacklistedPass:
if (testResult.type() == TestTypeQt)
output = testResult.className() + QLatin1String("::") + testResult.testCase();
else // TestTypeGTest
output = testResult.testCase();
if (!testResult.dataTag().isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(testResult.dataTag()));
if (selected && !desc.isEmpty()) {
output.append(QLatin1Char('\n')).append(desc);
}
break;
case Result::Benchmark:
output = testResult.className() + QLatin1String("::") + testResult.testCase();
if (!testResult.dataTag().isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(testResult.dataTag()));
if (!desc.isEmpty()) {
int breakPos = desc.indexOf(QLatin1Char('('));
output.append(QLatin1String(": ")).append(desc.left(breakPos));
if (selected)
output.append(QLatin1Char('\n')).append(desc.mid(breakPos));
}
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
}
return output;
}
void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
......@@ -110,7 +70,8 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
LayoutPositions positions(opt, resultFilterModel);
const TestResult &testResult = resultFilterModel->testResult(index);
const TestResult *testResult = resultFilterModel->testResult(index);
QTC_ASSERT(testResult, painter->restore();return);
// draw the indicator by ourself as we paint across it with the delegate
QStyleOptionViewItem indicatorOpt = option;
......@@ -122,17 +83,17 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
painter->drawPixmap(positions.left(), positions.top(),
icon.pixmap(positions.iconSize(), positions.iconSize()));
QString typeStr = TestResult::resultToString(testResult.result());
QString typeStr = TestResult::resultToString(testResult->result());
if (selected) {
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
} else {
QPen tmp = painter->pen();
painter->setPen(TestResult::colorForType(testResult.result()));
painter->setPen(TestResult::colorForType(testResult->result()));
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
painter->setPen(tmp);
}
QString output = outputString(testResult, selected);
QString output = testResult->outputString(selected);
if (selected) {
output.replace(QLatin1Char('\n'), QChar::LineSeparator);
......@@ -151,7 +112,7 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
fm.elidedText(output.left(2000), Qt::ElideRight, positions.textAreaWidth()));
}
QString file = testResult.fileName();
QString file = testResult->fileName();
const int pos = file.lastIndexOf(QLatin1Char('/'));
if (pos != -1)
file = file.mid(pos + 1);
......@@ -160,8 +121,8 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
painter->drawText(positions.fileAreaLeft(), positions.top() + fm.ascent(), file);
if (testResult.line()) {
QString line = QString::number(testResult.line());
if (testResult->line()) {
QString line = QString::number(testResult->line());
painter->setClipRect(positions.lineArea());
painter->drawText(positions.lineAreaLeft(), positions.top() + fm.ascent(), line);
}
......@@ -190,9 +151,9 @@ QSize TestResultDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
s.setWidth(opt.rect.width());
if (selected) {
const TestResult &testResult = resultFilterModel->testResult(index);
QString output = outputString(testResult, selected);
const TestResult *testResult = resultFilterModel->testResult(index);
QTC_ASSERT(testResult, return QSize());
QString output = testResult->outputString(selected);
output.replace(QLatin1Char('\n'), QChar::LineSeparator);
if (AutotestPlugin::instance()->settings()->limitResultOutput
......
......@@ -42,8 +42,6 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
static QString outputString(const TestResult &testResult, bool selected);
public slots:
void currentChanged(const QModelIndex &current, const QModelIndex &previous);
......
......@@ -81,8 +81,7 @@ QVariant TestResultItem::data(int column, int role) const
case Qt::DecorationRole:
return m_testResult ? testResultIcon(m_testResult->result()) : QVariant();
case Qt::DisplayRole:
return m_testResult ? TestResultDelegate::outputString(*m_testResult.data(), true)
: QVariant();
return m_testResult ? m_testResult->outputString(true) : QVariant();
default:
return Utils::TreeItem::data(column, role);
}
......@@ -175,11 +174,11 @@ void TestResultModel::addTestResult(const TestResultPtr &testResult, bool autoEx
TestResultItem *newItem = new TestResultItem(testResult);
// FIXME this might be totally wrong... we need some more unique information!
if (!testResult->className().isEmpty()) {
if (!testResult->name().isEmpty()) {
for (int row = lastRow; row >= 0; --row) {
TestResultItem *current = static_cast<TestResultItem *>(topLevelItems.at(row));
const TestResult *result = current->testResult();
if (result && result->className() == testResult->className()) {
if (result && result->name() == testResult->name()) {
current->appendChild(newItem);
if (autoExpand)
current->expand();
......@@ -226,12 +225,12 @@ void TestResultModel::clearTestResults()
m_widthOfLineNumber = 0;
}
TestResult TestResultModel::testResult(const QModelIndex &idx)
const TestResult *TestResultModel::testResult(const QModelIndex &idx)
{
if (idx.isValid())
return *(static_cast<TestResultItem *>(itemForIndex(idx))->testResult());
return static_cast<TestResultItem *>(itemForIndex(idx))->testResult();
return TestResult();
return 0;
}
int TestResultModel::maxWidthOfFileName(const QFont &font)
......@@ -327,7 +326,7 @@ bool TestResultFilterModel::hasResults()
return rowCount(QModelIndex());
}
TestResult TestResultFilterModel::testResult(const QModelIndex &index) const
const TestResult *TestResultFilterModel::testResult(const QModelIndex &index) const
{
return m_sourceModel->testResult(mapToSource(index));
}
......@@ -337,7 +336,7 @@ bool TestResultFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &s
QModelIndex index = m_sourceModel->index(sourceRow, 0, sourceParent);
if (!index.isValid())
return false;
return m_enabled.contains(m_sourceModel->testResult(index).result());
return m_enabled.contains(m_sourceModel->testResult(index)->result());
}
} // namespace Internal
......
......@@ -61,7 +61,7 @@ public:
void removeCurrentTestMessage();
void clearTestResults();
TestResult testResult(const QModelIndex &idx);
const TestResult *testResult(const QModelIndex &idx);
int maxWidthOfFileName(const QFont &font);
int maxWidthOfLineNumber(const QFont &font);
......@@ -88,7 +88,7 @@ public:
void toggleTestResultType(Result::Type type);
void clearTestResults();
bool hasResults();
TestResult testResult(const QModelIndex &index) const;
const TestResult *testResult(const QModelIndex &index) const;
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
......
......@@ -388,9 +388,9 @@ void TestResultsPane::onItemActivated(const QModelIndex &index)
if (!index.isValid())
return;
const TestResult tr = m_filterModel->testResult(index);
if (!tr.fileName().isEmpty())
Core::EditorManager::openEditorAt(tr.fileName(), tr.line(), 0);
const TestResult *testResult = m_filterModel->testResult(index);
if (testResult && !testResult->fileName().isEmpty())
Core::EditorManager::openEditorAt(testResult->fileName(), testResult->line(), 0);
}
void TestResultsPane::onRunAllTriggered()
......@@ -554,8 +554,9 @@ void TestResultsPane::onCustomContextMenuRequested(const QPoint &pos)
void TestResultsPane::onCopyItemTriggered(const QModelIndex &idx)
{
const TestResult result = m_filterModel->testResult(idx);
QApplication::clipboard()->setText(TestResultDelegate::outputString(result, true));
const TestResult *result = m_filterModel->testResult(idx);
QTC_ASSERT(result, return);
QApplication::clipboard()->setText(result->outputString(true));
}
void TestResultsPane::onCopyWholeTriggered()
......@@ -584,9 +585,10 @@ QString TestResultsPane::getWholeOutput(const QModelIndex &parent)
QString output;
for (int row = 0, count = m_model->rowCount(parent); row < count; ++row) {
QModelIndex current = m_model->index(row, 0, parent);
const TestResult result = m_model->testResult(current);
output.append(TestResult::resultToString(result.result())).append(QLatin1Char('\t'));
output.append(TestResultDelegate::outputString(result, true)).append(QLatin1Char('\n'));
const TestResult *result = m_model->testResult(current);
QTC_ASSERT(result, continue);
output.append(TestResult::resultToString(result->result())).append(QLatin1Char('\t'));
output.append(result->outputString(true)).append(QLatin1Char('\n'));
output.append(getWholeOutput(current));
}
return output;
......
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