Skip to content
Snippets Groups Projects
Commit b97bfaa7 authored by Leandro Melo's avatar Leandro Melo
Browse files

Generic highlighter: New unit tests for the highlighter engine.

parent 823064d9
No related branches found
No related tags found
No related merge requests found
TEMPLATE = subdirs TEMPLATE = subdirs
SUBDIRS += specificrules SUBDIRS += specificrules highlighterengine
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef BASETEXTDOCUMENTLAYOUT_H
#define BASETEXTDOCUMENTLAYOUT_H
/*
Since the text editor plugin directory is not included in the search list of the pro file, this
file replaces the "real" basetextdocumentlayout.h file. The objective is to simply use
QTextBlockUserData instead of TextEditor::TextBlockUserData to avoid "external"
dependencies or intrusive defines.
*/
#include <QtGui/QTextBlockUserData>
typedef QTextBlockUserData TextBlockUserData;
#endif // BASETEXTDOCUMENTLAYOUT_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "formats.h"
#include <QtCore/Qt>
Formats::Formats()
{
m_keywordFormat.setForeground(Qt::darkGray);
m_dataTypeFormat.setForeground(Qt::gray);
m_decimalFormat.setForeground(Qt::lightGray);
m_baseNFormat.setForeground(Qt::red);
m_floatFormat.setForeground(Qt::green);
m_charFormat.setForeground(Qt::blue);
m_stringFormat.setForeground(Qt::cyan);
m_commentFormat.setForeground(Qt::magenta);
m_alertFormat.setForeground(Qt::yellow);
m_errorFormat.setForeground(Qt::darkRed);
m_functionFormat.setForeground(Qt::darkGreen);
m_regionMarkerFormat.setForeground(Qt::darkBlue);
m_othersFormat.setForeground(Qt::darkCyan);
}
Formats &Formats::instance()
{
static Formats formats;
return formats;
}
QString Formats::name(const QTextCharFormat &format) const
{
if (format == QTextCharFormat())
return "Default format";
else if (format == m_keywordFormat)
return "Keyword";
else if (format == m_dataTypeFormat)
return "Data type format";
else if (format == m_decimalFormat)
return "Decimal format";
else if (format == m_baseNFormat)
return "Base N format";
else if (format == m_floatFormat)
return "Float format";
else if (format == m_charFormat)
return "Char format";
else if (format == m_stringFormat)
return "String format";
else if (format == m_commentFormat)
return "Comment format";
else if (format == m_alertFormat)
return "Alert format";
else if (format == m_errorFormat)
return "Error format";
else if (format == m_functionFormat)
return "Function format";
else if (format == m_regionMarkerFormat)
return "Region Marker format";
else if (format == m_othersFormat)
return "Others format";
else
return "Unidentified format";
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef FORMATS_H
#define FORMATS_H
#include <QtGui/QTextCharFormat>
class Formats
{
public:
static Formats &instance();
const QTextCharFormat &keywordFormat() const { return m_keywordFormat; }
const QTextCharFormat &dataTypeFormat() const { return m_dataTypeFormat; }
const QTextCharFormat &decimalFormat() const { return m_decimalFormat; }
const QTextCharFormat &baseNFormat() const { return m_baseNFormat; }
const QTextCharFormat &floatFormat() const { return m_floatFormat; }
const QTextCharFormat &charFormat() const { return m_charFormat; }
const QTextCharFormat &stringFormat() const { return m_stringFormat; }
const QTextCharFormat &commentFormat() const { return m_commentFormat; }
const QTextCharFormat &alertFormat() const { return m_alertFormat; }
const QTextCharFormat &errorFormat() const { return m_errorFormat; }
const QTextCharFormat &functionFormat() const { return m_functionFormat; }
const QTextCharFormat &regionMarketFormat() const { return m_regionMarkerFormat; }
const QTextCharFormat &othersFormat() const { return m_othersFormat; }
QString name(const QTextCharFormat &format) const;
private:
Formats();
Q_DISABLE_COPY(Formats);
QTextCharFormat m_keywordFormat;
QTextCharFormat m_dataTypeFormat;
QTextCharFormat m_decimalFormat;
QTextCharFormat m_baseNFormat;
QTextCharFormat m_floatFormat;
QTextCharFormat m_charFormat;
QTextCharFormat m_stringFormat;
QTextCharFormat m_commentFormat;
QTextCharFormat m_alertFormat;
QTextCharFormat m_errorFormat;
QTextCharFormat m_functionFormat;
QTextCharFormat m_regionMarkerFormat;
QTextCharFormat m_othersFormat;
};
#endif // FORMATS_H
QT += gui testlib
GENERICHIGHLIGHTERDIR = ../../../../src/plugins/texteditor/generichighlighter
SOURCES += tst_highlighterengine.cpp \
highlightermock.cpp \
$$GENERICHIGHLIGHTERDIR/highlighter.cpp \
$$GENERICHIGHLIGHTERDIR/context.cpp \
$$GENERICHIGHLIGHTERDIR/dynamicrule.cpp \
$$GENERICHIGHLIGHTERDIR/rule.cpp \
$$GENERICHIGHLIGHTERDIR/specificrules.cpp \
$$GENERICHIGHLIGHTERDIR/progressdata.cpp \
$$GENERICHIGHLIGHTERDIR/highlightdefinition.cpp \
$$GENERICHIGHLIGHTERDIR/keywordlist.cpp \
$$GENERICHIGHLIGHTERDIR/itemdata.cpp \
formats.cpp
HEADERS += \
highlightermock.h \
basetextdocumentlayout.h \
formats.h
INCLUDEPATH += $$GENERICHIGHLIGHTERDIR
TARGET=tst_$$TARGET
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "highlightermock.h"
#include "context.h"
#include "highlightdefinition.h"
#include "formats.h"
#include <QtCore/QDebug>
#include <QtTest/QtTest>
namespace QTest {
template<>
char *toString(const QTextCharFormat &format)
{
QByteArray ba = Formats::instance().name(format).toLatin1();
return qstrdup(ba.data());
}
}
using namespace TextEditor;
using namespace Internal;
HighlighterMock::HighlighterMock(const QSharedPointer<Context> &defaultContext,
QTextDocument *parent) :
Highlighter(defaultContext, parent),
m_debugDetails(false),
m_statesCounter(0),
m_formatsCounter(0),
m_noTestCall(false),
m_considerEmptyLines(false)
{}
void HighlighterMock::reset()
{
m_states.clear();
m_statesCounter = 0;
m_formatSequence.clear();
m_formatsCounter = 0;
m_debugDetails = false;
m_noTestCall = false;
m_considerEmptyLines = false;
}
void HighlighterMock::showDebugDetails()
{ m_debugDetails = true; }
void HighlighterMock::considerEmptyLines()
{ m_considerEmptyLines = true; }
void HighlighterMock::startNoTestCalls()
{ m_noTestCall = true; }
void HighlighterMock::endNoTestCalls()
{ m_noTestCall = false; }
void HighlighterMock::setExpectedBlockState(const int state)
{ m_states << state; }
void HighlighterMock::setExpectedBlockStates(const QList<int> &states)
{ m_states = states; }
void HighlighterMock::setExpectedHighlightSequence(const HighlightSequence &format)
{ m_formatSequence << format; }
void HighlighterMock::setExpectedHighlightSequences(const QList<HighlightSequence> &formats)
{ m_formatSequence = formats; }
void HighlighterMock::highlightBlock(const QString &text)
{
Highlighter::highlightBlock(text);
if (text.isEmpty() && !m_considerEmptyLines)
return;
if (m_noTestCall)
return;
if (m_states.isEmpty() || m_formatSequence.isEmpty())
QFAIL("No expected data setup.");
if (m_debugDetails)
qDebug() << "Highlighting" << text;
if (m_states.size() <= m_statesCounter)
QFAIL("Expected state for current block not set.");
QCOMPARE(currentBlockState(), m_states.at(m_statesCounter++));
if (m_formatSequence.size() <= m_formatsCounter)
QFAIL("Expected highlight sequence for current block not set.");
for (int i = 0; i < text.length(); ++i) {
if (text.at(i).isSpace())
continue;
if (m_debugDetails)
qDebug() << "at offset" << i;
QCOMPARE(format(i), m_formatSequence.at(m_formatsCounter).m_formats.at(i));
}
++m_formatsCounter;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef HIGHLIGHTERMOCK_H
#define HIGHLIGHTERMOCK_H
#include "highlighter.h"
#include <QtCore/QList>
namespace TextEditor {
namespace Internal {
class Context;
class HighlightDefinition;
}
}
struct HighlightSequence
{
HighlightSequence() {}
HighlightSequence(int from, int to, const QTextCharFormat &format = QTextCharFormat())
{ add(from, to, format); }
HighlightSequence(const HighlightSequence &sequence)
{ m_formats = sequence.m_formats; }
void add(int from, int to, const QTextCharFormat &format = QTextCharFormat())
{
for (int i = from; i < to; ++i)
m_formats.append(format);
}
QList<QTextCharFormat> m_formats;
};
class HighlighterMock : public TextEditor::Internal::Highlighter
{
public:
HighlighterMock(const QSharedPointer<TextEditor::Internal::Context> &defaultContext,
QTextDocument *parent = 0);
void reset();
void showDebugDetails();
void considerEmptyLines();
void startNoTestCalls();
void endNoTestCalls();
void setExpectedBlockState(const int state);
void setExpectedBlockStates(const QList<int> &states);
void setExpectedHighlightSequence(const HighlightSequence &format);
void setExpectedHighlightSequences(const QList<HighlightSequence> &formats);
protected:
virtual void highlightBlock(const QString &text);
private:
QList<int> m_states;
int m_statesCounter;
QList<HighlightSequence> m_formatSequence;
int m_formatsCounter;
bool m_debugDetails;
bool m_noTestCall;
bool m_considerEmptyLines;
};
#endif // HIGHLIGHTERMOCK_H
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment