Improve the QTextBoundaryFinder autotests a lot

The existing tests has been retained, the new ones has been added.

Change-Id: I12ae1b4e63dde46f3b14a7c1423c13d5881d4507
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Konstantin Ritt 2012-05-31 12:46:18 +03:00 committed by Qt by Nokia
parent 879539fb99
commit 93ead35939

View File

@ -45,6 +45,7 @@
#include <qtextcodec.h>
#include <qfile.h>
#include <qdebug.h>
#include <qlist.h>
class tst_QTextBoundaryFinder : public QObject
{
@ -52,16 +53,23 @@ class tst_QTextBoundaryFinder : public QObject
public slots:
void init();
private slots:
void graphemeBoundaries();
void wordBoundaries();
void sentenceBoundaries();
void isAtWordStart();
#ifdef QT_BUILD_INTERNAL
void graphemeBoundariesDefault_data();
void graphemeBoundariesDefault();
void wordBoundariesDefault_data();
void wordBoundariesDefault();
void sentenceBoundariesDefault_data();
void sentenceBoundariesDefault();
#endif
void wordBoundaries_manual_data();
void wordBoundaries_manual();
void sentenceBoundaries_manual_data();
void sentenceBoundaries_manual();
void lineBoundaries_manual_data();
void lineBoundaries_manual();
void fastConstructor();
void isAtBoundaryLine();
void toNextBoundary_data();
void toNextBoundary();
void toPreviousBoundary_data();
void toPreviousBoundary();
void thaiLineBreak();
};
@ -74,314 +82,444 @@ void tst_QTextBoundaryFinder::init()
#endif
}
void tst_QTextBoundaryFinder::graphemeBoundaries()
Q_DECLARE_METATYPE(QList<int>)
QT_BEGIN_NAMESPACE
namespace QTest {
template<>
inline char *toString(const QTextBoundaryFinder::BoundaryReasons &flags)
{
QFile file("data/GraphemeBreakTest.txt");
file.open(QFile::ReadOnly);
int lines = 0;
while (!file.atEnd()) {
QByteArray line = file.readLine();
if (line.startsWith('#'))
continue;
lines++;
QString test = QString::fromUtf8(line);
int hash = test.indexOf('#');
if (hash > 0)
test = test.left(hash);
test = test.simplified();
test = test.replace(QLatin1String(" "), QString());
QList<int> breakPositions;
QString testString;
int pos = 0;
int strPos = 0;
while (pos < test.length()) {
if (test.at(pos).unicode() == 0xf7)
breakPositions.append(strPos);
else
QVERIFY(test.at(pos).unicode() == 0xd7);
++pos;
if (pos < test.length()) {
QVERIFY(pos < test.length() - 4);
QString hex = test.mid(pos, 4);
bool ok = true;
testString.append(QChar(hex.toInt(&ok, 16)));
QVERIFY(ok);
pos += 4;
}
++strPos;
}
QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, testString);
for (int i = 0; i < breakPositions.size(); ++i) {
QCOMPARE(finder.position(), breakPositions.at(i));
finder.toNextBoundary();
}
QCOMPARE(finder.toNextBoundary(), -1);
for (int i = 0; i < testString.length(); ++i) {
finder.setPosition(i);
QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true);
}
}
QCOMPARE(lines, 100); // to see it actually found the file and all.
return qstrdup(QByteArray::number(int(flags)).constData());
}
void tst_QTextBoundaryFinder::wordBoundaries()
template<>
inline char *toString(const QList<int> &list)
{
QFile file("data/WordBreakTest.txt");
file.open(QFile::ReadOnly);
QByteArray s;
for (QList<int>::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) {
if (!s.isEmpty())
s += ", ";
s += QByteArray::number(*it);
}
s = "{ " + s + " }";
return qstrdup(s.constData());
}
while (!file.atEnd()) {
QByteArray line = file.readLine();
} // namespace QTest
QT_END_NAMESPACE
static void generateDataFromFile(const QString &fname)
{
QTest::addColumn<QString>("testString");
QTest::addColumn<QList<int> >("expectedBreakPositions");
QString testFile = QFINDTESTDATA(fname);
QVERIFY2(!testFile.isEmpty(), (fname.toLatin1() + QByteArray(" not found!")));
QFile f(testFile);
QVERIFY(f.exists());
f.open(QIODevice::ReadOnly);
int linenum = 0;
while (!f.atEnd()) {
linenum++;
QByteArray line = f.readLine();
if (line.startsWith('#'))
continue;
QString test = QString::fromUtf8(line);
QString comments;
int hash = test.indexOf('#');
if (hash > 0)
if (hash > 0) {
comments = test.mid(hash + 1).simplified();
test = test.left(hash);
test = test.simplified();
test = test.replace(QLatin1String(" "), QString());
}
QList<int> breakPositions;
QString testString;
int pos = 0;
int strPos = 0;
while (pos < test.length()) {
if (test.at(pos).unicode() == 0xf7)
breakPositions.append(strPos);
else
QVERIFY(test.at(pos).unicode() == 0xd7);
++pos;
if (pos < test.length()) {
QVERIFY(pos < test.length() - 4);
QString hex = test.mid(pos, 4);
bool ok = true;
testString.append(QChar(hex.toInt(&ok, 16)));
QVERIFY(ok);
pos += 4;
QList<int> expectedBreakPositions;
foreach (const QString &part, test.simplified().split(QLatin1Char(' '), QString::SkipEmptyParts)) {
if (part.size() == 1) {
if (part.at(0).unicode() == 0xf7)
expectedBreakPositions.append(testString.size());
else
QVERIFY(part.at(0).unicode() == 0xd7);
continue;
}
bool ok = true;
uint ucs4 = part.toInt(&ok, 16);
QVERIFY(ok && ucs4 > 0);
QVERIFY(!QChar::requiresSurrogates(ucs4));
testString.append(QChar(ucs4));
}
QVERIFY(!testString.isEmpty());
QVERIFY(!expectedBreakPositions.isEmpty());
if (!comments.isEmpty()) {
const QStringList lst = comments.simplified().split(QLatin1Char(' '), QString::SkipEmptyParts);
comments.clear();
foreach (const QString &part, lst) {
if (part.size() == 1) {
if (part.at(0).unicode() == 0xf7)
comments += QLatin1Char('+');
else if (part.at(0).unicode() == 0xd7)
comments += QLatin1Char('x');
continue;
}
if (part.startsWith(QLatin1Char('(')) && part.endsWith(QLatin1Char(')')))
comments += part;
}
++strPos;
}
QTextBoundaryFinder finder(QTextBoundaryFinder::Word, testString);
for (int i = 0; i < breakPositions.size(); ++i) {
QCOMPARE(finder.position(), breakPositions.at(i));
finder.toNextBoundary();
}
QCOMPARE(finder.toNextBoundary(), -1);
for (int i = 0; i < testString.length(); ++i) {
finder.setPosition(i);
QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true);
}
QString nm = QString("line #%1: %2").arg(linenum).arg(comments);
QTest::newRow(nm.toLatin1()) << testString << expectedBreakPositions;
}
}
void tst_QTextBoundaryFinder::sentenceBoundaries()
#ifdef QT_BUILD_INTERNAL
QT_BEGIN_NAMESPACE
extern Q_AUTOTEST_EXPORT int qt_initcharattributes_default_algorithm_only;
QT_END_NAMESPACE
#endif
static void doTestData(const QString &testString, const QList<int> &expectedBreakPositions,
QTextBoundaryFinder::BoundaryType type, bool default_algorithm_only = false)
{
QFile file("data/SentenceBreakTest.txt");
file.open(QFile::ReadOnly);
#ifdef QT_BUILD_INTERNAL
QScopedValueRollback<int> default_algorithm(qt_initcharattributes_default_algorithm_only);
if (default_algorithm_only)
qt_initcharattributes_default_algorithm_only++;
#else
Q_UNUSED(default_algorithm_only)
#endif
while (!file.atEnd()) {
QByteArray line = file.readLine();
if (line.startsWith('#'))
continue;
QTextBoundaryFinder boundaryFinder(type, testString);
QString test = QString::fromUtf8(line);
int hash = test.indexOf('#');
if (hash > 0)
test = test.left(hash);
test = test.simplified();
test = test.replace(QLatin1String(" "), QString());
QList<int> breakPositions;
QString testString;
int pos = 0;
int strPos = 0;
while (pos < test.length()) {
if (test.at(pos).unicode() == 0xf7)
breakPositions.append(strPos);
else
QVERIFY(test.at(pos).unicode() == 0xd7);
++pos;
if (pos < test.length()) {
QVERIFY(pos < test.length() - 4);
QString hex = test.mid(pos, 4);
bool ok = true;
testString.append(QChar(hex.toInt(&ok, 16)));
QVERIFY(ok);
pos += 4;
}
++strPos;
// test toNextBoundary()
{
QList<int> actualBreakPositions;
if (boundaryFinder.isAtBoundary())
actualBreakPositions.append(boundaryFinder.position());
while (boundaryFinder.toNextBoundary() != -1) {
QVERIFY(boundaryFinder.isAtBoundary());
actualBreakPositions.append(boundaryFinder.position());
}
QCOMPARE(actualBreakPositions, expectedBreakPositions);
}
QTextBoundaryFinder finder(QTextBoundaryFinder::Sentence, testString);
for (int i = 0; i < breakPositions.size(); ++i) {
QCOMPARE(finder.position(), breakPositions.at(i));
finder.toNextBoundary();
}
QCOMPARE(finder.toNextBoundary(), -1);
// test toPreviousBoundary()
{
QList<int> expectedBreakPositionsRev = expectedBreakPositions;
qSort(expectedBreakPositionsRev.begin(), expectedBreakPositionsRev.end(), qGreater<int>());
for (int i = 0; i < testString.length(); ++i) {
finder.setPosition(i);
QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true);
QList<int> actualBreakPositions;
boundaryFinder.toEnd();
if (boundaryFinder.isAtBoundary())
actualBreakPositions.append(boundaryFinder.position());
while (boundaryFinder.toPreviousBoundary() != -1) {
QVERIFY(boundaryFinder.isAtBoundary());
actualBreakPositions.append(boundaryFinder.position());
}
QCOMPARE(actualBreakPositions, expectedBreakPositionsRev);
}
// test isAtBoundary()
for (int i = 0; i < testString.length(); ++i) {
boundaryFinder.setPosition(i);
QCOMPARE(boundaryFinder.isAtBoundary(), expectedBreakPositions.contains(i));
}
}
void tst_QTextBoundaryFinder::isAtWordStart()
#ifdef QT_BUILD_INTERNAL
void tst_QTextBoundaryFinder::graphemeBoundariesDefault_data()
{
QString txt("The quick brown fox jumped over $the lazy. dog I win!");
QList<int> start, end;
start << 0 << 4 << 10 << 16 << 20 << 27 << 32 << 33 << 37 << 41 << 43 << 48 << 50 << 53;
end << 3 << 9 << 15 << 19 << 26 << 31 << 33 << 36 << 41 << 42 << 46 << 49 << 53 << 54;
QTextBoundaryFinder finder(QTextBoundaryFinder::Word, txt);
for(int i=0; i < txt.length(); ++i) {
finder.setPosition(i);
QTextBoundaryFinder::BoundaryReasons r = finder.boundaryReasons();
QCOMPARE((r & QTextBoundaryFinder::StartWord) != 0, start.contains(i) == true);
QCOMPARE((r & QTextBoundaryFinder::EndWord) != 0, end.contains(i) == true);
generateDataFromFile("data/GraphemeBreakTest.txt");
}
void tst_QTextBoundaryFinder::graphemeBoundariesDefault()
{
QFETCH(QString, testString);
QFETCH(QList<int>, expectedBreakPositions);
doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Grapheme, true);
}
void tst_QTextBoundaryFinder::wordBoundariesDefault_data()
{
generateDataFromFile("data/WordBreakTest.txt");
}
void tst_QTextBoundaryFinder::wordBoundariesDefault()
{
QFETCH(QString, testString);
QFETCH(QList<int>, expectedBreakPositions);
doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Word, true);
}
void tst_QTextBoundaryFinder::sentenceBoundariesDefault_data()
{
generateDataFromFile("data/SentenceBreakTest.txt");
}
void tst_QTextBoundaryFinder::sentenceBoundariesDefault()
{
QFETCH(QString, testString);
QFETCH(QList<int>, expectedBreakPositions);
doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Sentence, true);
}
#endif // QT_BUILD_INTERNAL
void tst_QTextBoundaryFinder::wordBoundaries_manual_data()
{
QTest::addColumn<QString>("testString");
QTest::addColumn<QList<int> >("expectedBreakPositions");
{
QChar s[] = { 0x000D, 0x000A, 0x000A };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 2 << 3;
QTest::newRow("+CRxLF+LF+") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x000D, 0x0308, 0x000A, 0x000A };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 2 << 3 << 4;
QTest::newRow("+CR+FE+LF+LF+") << testString << expectedBreakPositions;
}
{
QString testString(QString::fromUtf8("Aaa bbb ccc.\r\nDdd eee fff."));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 3 << 4 << 7 << 8 << 11 << 12 << 14 << 17 << 18 << 21 << 22 << 25 << 26;
QTest::newRow("data1") << testString << expectedBreakPositions;
}
// Sample Strings from WordBreakTest.html
{
QChar s[] = { 0x0063, 0x0061, 0x006E, 0x0027, 0x0074 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 5;
QTest::newRow("ts 1") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x0063, 0x0061, 0x006E, 0x2019, 0x0074 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 5;
QTest::newRow("ts 2") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x0061, 0x0062, 0x00AD, 0x0062, 0x0061 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 5;
QTest::newRow("ts 3") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x0061, 0x0024, 0x002D, 0x0033, 0x0034, 0x002C, 0x0035, 0x0036,
0x0037, 0x002E, 0x0031, 0x0034, 0x0025, 0x0062 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 2 << 3 << 12 << 13 << 14;
QTest::newRow("ts 4") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x0033, 0x0061 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 2;
QTest::newRow("ts 5") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x2060, 0x0063, 0x2060, 0x0061, 0x2060, 0x006E, 0x2060, 0x0027,
0x2060, 0x0074, 0x2060, 0x2060 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 12;
QTest::newRow("ts 1e") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x2060, 0x0063, 0x2060, 0x0061, 0x2060, 0x006E, 0x2060, 0x2019,
0x2060, 0x0074, 0x2060, 0x2060 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 12;
QTest::newRow("ts 2e") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x2060, 0x0061, 0x2060, 0x0062, 0x2060, 0x00AD, 0x2060, 0x0062,
0x2060, 0x0061, 0x2060, 0x2060 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 12;
QTest::newRow("ts 3e") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x2060, 0x0061, 0x2060, 0x0024, 0x2060, 0x002D, 0x2060, 0x0033,
0x2060, 0x0034, 0x2060, 0x002C, 0x2060, 0x0035, 0x2060, 0x0036,
0x2060, 0x0037, 0x2060, 0x002E, 0x2060, 0x0031, 0x2060, 0x0034,
0x2060, 0x0025, 0x2060, 0x0062, 0x2060, 0x2060 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 3 << 5 << 7 << 25 << 27 << 30;
QTest::newRow("ts 4e") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x2060, 0x0033, 0x2060, 0x0061, 0x2060, 0x2060 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 6;
QTest::newRow("ts 5e") << testString << expectedBreakPositions;
}
}
void tst_QTextBoundaryFinder::wordBoundaries_manual()
{
QFETCH(QString, testString);
QFETCH(QList<int>, expectedBreakPositions);
doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Word);
}
void tst_QTextBoundaryFinder::sentenceBoundaries_manual_data()
{
QTest::addColumn<QString>("testString");
QTest::addColumn<QList<int> >("expectedBreakPositions");
{
QChar s[] = { 0x000D, 0x000A, 0x000A };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 2 << 3;
QTest::newRow("+CRxLF+LF+") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x000D, 0x0308, 0x000A, 0x000A };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 3 << 4;
QTest::newRow("+CR+FExLF+LF+") << testString << expectedBreakPositions;
}
{
QString testString(QString::fromUtf8("Aaa bbb ccc.\r\nDdd eee fff."));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 14 << 26;
QTest::newRow("data1") << testString << expectedBreakPositions;
}
{
QString testString(QString::fromUtf8("Diga-nos qualé a sua opinião"));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 28;
QTest::newRow("data2") << testString << expectedBreakPositions;
}
}
void tst_QTextBoundaryFinder::sentenceBoundaries_manual()
{
QFETCH(QString, testString);
QFETCH(QList<int>, expectedBreakPositions);
doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Sentence);
}
void tst_QTextBoundaryFinder::lineBoundaries_manual_data()
{
QTest::addColumn<QString>("testString");
QTest::addColumn<QList<int> >("expectedBreakPositions");
{
QString testString(QString::fromUtf8("Aaa bbb ccc.\r\nDdd eee fff."));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 4 << 8 << 14 << 18 << 22 << 26;
QTest::newRow("data1") << testString << expectedBreakPositions;
}
{
QString testString(QString::fromUtf8("Diga-nos qualé a sua opinião"));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 5 << 9 << 15 << 17 << 21 << 28;
QTest::newRow("data2") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x000A, 0x2E80, 0x0308, 0x0023, 0x0023 };
QString testString(s, sizeof(s)/sizeof(QChar));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 3 << 5;
QTest::newRow("x(LF)+(ID)x(CM)+(AL)x(AL)+") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x000A, 0x0308, 0x0023, 0x0023 };
QString testString(s, sizeof(s)/sizeof(QChar));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 1 << 4;
QTest::newRow("x(LF)+(CM)x(AL)x(AL)+") << testString << expectedBreakPositions;
}
{
QChar s[] = { 0x0061, 0x00AD, 0x0062, 0x0009, 0x0063, 0x0064 };
QString testString(s, sizeof(s)/sizeof(s[0]));
QList<int> expectedBreakPositions;
expectedBreakPositions << 0 << 4 << 6;
QTest::newRow("x(AL)x(BA)+(AL)x(BA)+(AL)x(AL)+") << testString << expectedBreakPositions;
}
}
void tst_QTextBoundaryFinder::lineBoundaries_manual()
{
QFETCH(QString, testString);
QFETCH(QList<int>, expectedBreakPositions);
doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Line);
}
void tst_QTextBoundaryFinder::fastConstructor()
{
QString text("Hello World");
QTextBoundaryFinder finder(QTextBoundaryFinder::Word, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0);
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::StartWord);
QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::StartWord);
finder.toNextBoundary();
QCOMPARE(finder.position(), 5);
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::EndWord);
QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::EndWord);
finder.toNextBoundary();
QCOMPARE(finder.position(), 6);
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::StartWord);
QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::StartWord);
finder.toNextBoundary();
QCOMPARE(finder.position(), text.length());
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::EndWord);
QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::EndWord);
finder.toNextBoundary();
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::NotAtBoundary);
QCOMPARE(finder.position(), -1);
}
void tst_QTextBoundaryFinder::isAtBoundaryLine()
{
// idx 0 1 2 3 4 5 6
// break? - - - - + - +
QChar s[] = { 0x0061, 0x00AD, 0x0062, 0x0009, 0x0063, 0x0064 };
QString text(s, sizeof(s)/sizeof(s[0]));
QTextBoundaryFinder finder(QTextBoundaryFinder::Line, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0);
finder.setPosition(0);
QVERIFY(finder.isAtBoundary());
finder.setPosition(1);
QVERIFY(!finder.isAtBoundary());
finder.setPosition(2);
QVERIFY(!finder.isAtBoundary());
finder.setPosition(3);
QVERIFY(!finder.isAtBoundary());
finder.setPosition(4);
QVERIFY(finder.isAtBoundary());
finder.setPosition(5);
QVERIFY(!finder.isAtBoundary());
finder.setPosition(6);
QVERIFY(finder.isAtBoundary());
}
Q_DECLARE_METATYPE(QList<int>)
void tst_QTextBoundaryFinder::toNextBoundary_data()
{
QTest::addColumn<QString>("text");
QTest::addColumn<int>("type");
QTest::addColumn< QList<int> >("boundaries");
QList<int> boundaries;
boundaries << 0 << 3 << 4 << 7 << 8 << 11 << 12 << 13 << 16 << 17 << 20 << 21 << 24 << 25;
QTest::newRow("data1") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Word) \
<< boundaries;
boundaries.clear();
boundaries << 0 << 13 << 25;
QTest::newRow("data2") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Sentence) \
<< boundaries;
boundaries.clear();
boundaries << 0 << 4 << 8 << 13 << 17 << 21 << 25;
QTest::newRow("data3") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Line) \
<< boundaries;
boundaries.clear();
boundaries << 0 << 5 << 9 << 15 << 17 << 21 << 28;
QTest::newRow("data4") << QString::fromUtf8("Diga-nos qualé a sua opinião") << int(QTextBoundaryFinder::Line)
<< boundaries;
}
void tst_QTextBoundaryFinder::toNextBoundary()
{
QFETCH(QString, text);
QFETCH(int, type);
QFETCH(QList<int>, boundaries);
QList<int> foundBoundaries;
QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::BoundaryType(type), text);
boundaryFinder.toStart();
for(int next = 0; next != -1; next = boundaryFinder.toNextBoundary())
foundBoundaries << next;
QCOMPARE(boundaries, foundBoundaries);
}
void tst_QTextBoundaryFinder::toPreviousBoundary_data()
{
QTest::addColumn<QString>("text");
QTest::addColumn<int>("type");
QTest::addColumn< QList<int> >("boundaries");
QList<int> boundaries;
boundaries << 25 << 24 << 21 << 20 << 17 << 16 << 13 << 12 << 11 << 8 << 7 << 4 << 3 << 0;
QTest::newRow("data1") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Word)
<< boundaries;
boundaries.clear();
boundaries << 25 << 13 << 0;
QTest::newRow("data2") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Sentence)
<< boundaries;
boundaries.clear();
boundaries << 25 << 21 << 17 << 13 << 8 << 4 << 0;
QTest::newRow("data3") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Line)
<< boundaries;
boundaries.clear();
boundaries << 28 << 21 << 17 << 15 << 9 << 5 << 0;
QTest::newRow("data4") << QString::fromUtf8("Diga-nos qualé a sua opinião") << int(QTextBoundaryFinder::Line)
<< boundaries;
}
void tst_QTextBoundaryFinder::toPreviousBoundary()
{
QFETCH(QString, text);
QFETCH(int, type);
QFETCH(QList<int>, boundaries);
QList<int> foundBoundaries;
QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::BoundaryType(type), text);
boundaryFinder.toEnd();
for (int previous = boundaryFinder.position();
previous != -1;
previous = boundaryFinder.toPreviousBoundary())
{
foundBoundaries << previous;
}
QCOMPARE(boundaries, foundBoundaries);
QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::NotAtBoundary);
}
#include <qlibrary.h>