Commit c81eaee8 authored by Milian Wolff's avatar Milian Wolff
Browse files

Add a Path::hasParent method with tests.

This is useful to loop over a path and all its ancestors.
parent 9e3f30d9
......@@ -372,6 +372,12 @@ Path Path::parent() const
return ret;
}
bool Path::hasParent() const
{
const int rootIdx = isRemote() ? 1 : 0;
return m_data.size() > rootIdx && !m_data[rootIdx].isEmpty();
}
void Path::clear()
{
m_data.clear();
......
......@@ -311,6 +311,11 @@ public:
*/
Path parent() const;
/**
* @return true when this path has a parent and false if this is a root or invalid path.
*/
bool hasParent() const;
/**
* Clear the path, i.e. make it invalid and empty.
*/
......
......@@ -496,4 +496,27 @@ void TestPath::testPathCd_data()
}
}
void TestPath::testHasParent_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<bool>("hasParent");
QTest::newRow("empty") << QString() << false;
QTest::newRow("/") << QString("/") << false;
QTest::newRow("/foo") << QString("/foo") << true;
QTest::newRow("/foo/bar") << QString("/foo/bar") << true;
QTest::newRow("//foo/bar") << QString("//foo/bar") << true;
QTest::newRow("http://foo.bar") << QString("http://foo.bar") << false;
QTest::newRow("http://foo.bar/") << QString("http://foo.bar/") << false;
QTest::newRow("http://foo.bar/asdf") << QString("http://foo.bar/asdf") << true;
QTest::newRow("http://foo.bar/asdf/asdf") << QString("http://foo.bar/asdf/asdf") << true;
}
void TestPath::testHasParent()
{
QFETCH(QString, input);
Path path(input);
QTEST(path.hasParent(), "hasParent");
}
#include "test_path.moc"
......@@ -50,6 +50,8 @@ private slots:
void testPathBaseCtor_data();
void testPathCd();
void testPathCd_data();
void testHasParent_data();
void testHasParent();
};
#endif // TESTPATH_H
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