2017-07-26 14:08:26 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2017 BlackBerry Limited <qt@blackberry.com>
|
|
|
|
|
** Copyright (C) 2017 Andre Hartmann <aha_1980@gmx.de>
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
#include <utils/fuzzymatcher.h>
|
2017-07-26 14:08:26 +02:00
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
class tst_FuzzyMatcher : public QObject
|
2017-07-26 14:08:26 +02:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
private slots:
|
2017-10-30 20:50:11 +02:00
|
|
|
void fuzzyMatcher();
|
|
|
|
|
void fuzzyMatcher_data();
|
2017-07-24 20:55:47 +02:00
|
|
|
void highlighting();
|
|
|
|
|
void highlighting_data();
|
2017-07-26 14:08:26 +02:00
|
|
|
};
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
void tst_FuzzyMatcher::fuzzyMatcher()
|
2017-07-26 14:08:26 +02:00
|
|
|
{
|
|
|
|
|
QFETCH(QString, pattern);
|
|
|
|
|
QFETCH(QString, candidate);
|
|
|
|
|
QFETCH(int, expectedIndex);
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
const QRegularExpression regExp = FuzzyMatcher::createRegExp(pattern);
|
2017-07-27 06:44:35 +02:00
|
|
|
const QRegularExpressionMatch match = regExp.match(candidate);
|
|
|
|
|
QCOMPARE(match.capturedStart(), expectedIndex);
|
2017-07-26 14:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
void tst_FuzzyMatcher::fuzzyMatcher_data()
|
2017-07-26 14:08:26 +02:00
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("pattern");
|
|
|
|
|
QTest::addColumn<QString>("candidate");
|
|
|
|
|
QTest::addColumn<int>("expectedIndex");
|
|
|
|
|
|
|
|
|
|
QTest::newRow("underscore") << "vl" << "very_long_camel_hump" << 0;
|
2017-09-09 22:15:00 +02:00
|
|
|
QTest::newRow("underscore-uppercase") << "vl" << "VERY_LONG_CAMEL_HUMP" << 0;
|
2017-07-26 14:08:26 +02:00
|
|
|
QTest::newRow("exact") << "VeryLongCamelHump" << "VeryLongCamelHump" << 0;
|
|
|
|
|
QTest::newRow("prefix-segments") << "velo" << "very_long_Camel_hump" << 0;
|
|
|
|
|
QTest::newRow("humps") << "vlc" << "VeryLongCamelHump" << 0;
|
|
|
|
|
QTest::newRow("case-insensitive-humps") << "VlCh" << "VeryLongCamelHump" << 0;
|
|
|
|
|
QTest::newRow("incorrect-hump") << "vxc" << "VeryLongCamelHump" << -1;
|
|
|
|
|
QTest::newRow("skipped-hump") << "vc" << "VeryLongCamelHump" << -1;
|
|
|
|
|
QTest::newRow("middle-humps") << "lc" << "VeryLongCamelHump" << 4;
|
|
|
|
|
QTest::newRow("incorrect-hump") << "lyn" << "VeryLongCamelHump" << -1;
|
|
|
|
|
QTest::newRow("humps") << "VL" << "VeryLongCamelHump" << 0;
|
|
|
|
|
QTest::newRow("skipped-humps-upper") << "VH" << "VeryLongCamelHump" << -1;
|
2019-07-13 08:45:26 +02:00
|
|
|
QTest::newRow("numbers-searched") << "4" << "Test4Fun" << 4;
|
|
|
|
|
QTest::newRow("numbers-skipped") << "fpt" << "Fancy4PartThingy" << 0;
|
|
|
|
|
QTest::newRow("numbers-camel") << "f4pt" << "Fancy4PartThingy" << 0;
|
|
|
|
|
QTest::newRow("multi-numbers-camel") << "f4pt" << "Fancy4567PartThingy" << 0;
|
|
|
|
|
QTest::newRow("numbers-underscore") << "f4pt" << "fancy_4_part_thingy" << 0;
|
|
|
|
|
QTest::newRow("numbers-underscore-upper") << "f4pt" << "FANCY_4_PART_THINGY" << 0;
|
2017-07-26 14:08:26 +02:00
|
|
|
QTest::newRow("question-wildcard") << "Lon?Ca" << "VeryLongCamelHump" << 4;
|
|
|
|
|
QTest::newRow("unmatched-question-wildcard") << "Long?Ca" << "VeryLongCamelHump" << -1;
|
2017-10-04 04:44:21 +02:00
|
|
|
QTest::newRow("asterisk-wildcard") << "Long*Ca" << "VeryLongCamelHump" << 4;
|
|
|
|
|
QTest::newRow("empty-asterisk-wildcard") << "Lo*Ca" << "VeryLongCamelHump" << 4;
|
2017-09-10 22:05:11 +03:00
|
|
|
QTest::newRow("no-partial") << "NCH" << "LongCamelHump" << -1;
|
|
|
|
|
QTest::newRow("middle-after-number") << "CH" << "Long1CamelHump" << 5;
|
|
|
|
|
QTest::newRow("middle-after-underscore") << "CH" << "long_camel_hump" << 5;
|
2017-09-09 22:15:00 +02:00
|
|
|
QTest::newRow("middle-after-underscore-uppercase") << "CH" << "LONG_CAMEL_HUMP" << 5;
|
2017-09-10 22:05:11 +03:00
|
|
|
QTest::newRow("middle-continued") << "cahu" << "LongCamelHump" << 4;
|
2017-09-20 21:55:39 +02:00
|
|
|
QTest::newRow("middle-no-hump") << "window" << "mainwindow.cpp" << 4;
|
2017-10-02 23:50:10 +03:00
|
|
|
QTest::newRow("case-insensitive") << "window" << "MAINWINDOW.cpp" << 4;
|
|
|
|
|
QTest::newRow("case-insensitive-2") << "wINDow" << "MainwiNdow.cpp" << 4;
|
2017-07-26 14:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-30 22:04:20 +02:00
|
|
|
typedef QVector<QPair<int, int>> Matches;
|
2017-07-24 20:55:47 +02:00
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
void tst_FuzzyMatcher::highlighting()
|
2017-07-24 20:55:47 +02:00
|
|
|
{
|
|
|
|
|
QFETCH(QString, pattern);
|
|
|
|
|
QFETCH(QString, candidate);
|
2017-10-30 22:04:20 +02:00
|
|
|
QFETCH(Matches, matches);
|
2017-07-24 20:55:47 +02:00
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
const QRegularExpression regExp = FuzzyMatcher::createRegExp(pattern);
|
2017-07-24 20:55:47 +02:00
|
|
|
const QRegularExpressionMatch match = regExp.match(candidate);
|
2017-10-30 20:50:11 +02:00
|
|
|
const FuzzyMatcher::HighlightingPositions positions =
|
|
|
|
|
FuzzyMatcher::highlightingPositions(match);
|
2017-07-24 20:55:47 +02:00
|
|
|
|
2017-10-30 22:04:20 +02:00
|
|
|
QCOMPARE(positions.starts.size(), matches.size());
|
2017-07-24 20:55:47 +02:00
|
|
|
for (int i = 0; i < positions.starts.size(); ++i) {
|
2017-10-30 22:04:20 +02:00
|
|
|
const QPair<int, int> &match = matches.at(i);
|
|
|
|
|
QCOMPARE(positions.starts.at(i), match.first);
|
|
|
|
|
QCOMPARE(positions.lengths.at(i), match.second);
|
2017-07-24 20:55:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
void tst_FuzzyMatcher::highlighting_data()
|
2017-07-24 20:55:47 +02:00
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("pattern");
|
|
|
|
|
QTest::addColumn<QString>("candidate");
|
2017-10-30 22:04:20 +02:00
|
|
|
QTest::addColumn<Matches>("matches");
|
2017-07-24 20:55:47 +02:00
|
|
|
|
|
|
|
|
QTest::newRow("prefix-snake") << "very" << "very_long_camel_hump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{0, 4}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("middle-snake") << "long" << "very_long_camel_hump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{5, 4}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("suffix-snake") << "hump" << "very_long_camel_hump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{16, 4}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("prefix-camel") << "very" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{0, 4}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("middle-camel") << "Long" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{4, 4}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("suffix-camel") << "Hump" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{13, 4}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("humps-camel") << "vlch" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{0, 1}, {4, 1}, {8, 1}, {13, 1}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("humps-camel-lower") << "vlch" << "veryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{0, 1}, {4, 1}, {8, 1}, {13, 1}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("humps-snake") << "vlch" << "very_long_camel_hump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{0, 1}, {5, 1}, {10, 1}, {16, 1}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("humps-middle") << "lc" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{4, 1}, {8, 1}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("humps-last") << "h" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{13, 1}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("humps-continued") << "LoCa" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{4, 2}, {8, 2}};
|
2017-10-28 20:32:21 +03:00
|
|
|
QTest::newRow("duplicate-match") << "som" << "SomeMatch"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{0, 3}};
|
2019-07-13 08:45:26 +02:00
|
|
|
QTest::newRow("numbers-searched") << "4" << "TestJust4Fun"
|
|
|
|
|
<< Matches{{8, 1}};
|
|
|
|
|
QTest::newRow("numbers-plain") << "boot2qt" << "use_boot2qt_for_embedded"
|
|
|
|
|
<< Matches{{4, 7}};
|
|
|
|
|
QTest::newRow("numbers-skipped") << "fpt" << "Fancy4PartThingy"
|
|
|
|
|
<< Matches{{0, 1}, {6, 1}, {10, 1}};
|
|
|
|
|
QTest::newRow("numbers-camel") << "f4pt" << "Fancy4PartThingy"
|
|
|
|
|
<< Matches{{0, 1}, {5, 2}, {10, 1}};
|
|
|
|
|
QTest::newRow("numbers-snake") << "f4pt" << "fancy_4_part_thingy"
|
|
|
|
|
<< Matches{{0, 1}, {6, 1}, {8, 1}, {13, 1}};
|
|
|
|
|
QTest::newRow("numbers-snake-upper") << "f4pt" << "FANCY_4_PART_THINGY"
|
|
|
|
|
<< Matches{{0, 1}, {6, 1}, {8, 1}, {13, 1}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("wildcard-asterisk") << "Lo*Hu" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{4, 2}, {13, 2}};
|
2017-07-24 20:55:47 +02:00
|
|
|
QTest::newRow("wildcard-question") << "Lo?g" << "VeryLongCamelHump"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{4, 2}, {7, 1}};
|
2017-09-20 21:55:39 +02:00
|
|
|
QTest::newRow("middle-no-hump") << "window" << "mainwindow.cpp"
|
2017-10-30 22:04:20 +02:00
|
|
|
<< Matches{{4, 6}};
|
2017-07-24 20:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
QTEST_APPLESS_MAIN(tst_FuzzyMatcher)
|
|
|
|
|
#include "tst_fuzzymatcher.moc"
|