1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-18 12:28:31 +01:00

Add more tests to m3uparser including one windows-only test.

Fix code review comments from r291.
This commit is contained in:
John Maguire 2010-03-01 16:31:19 +00:00
parent 0c49e0dd94
commit 84786c7bc8
3 changed files with 47 additions and 13 deletions

View File

@ -2,7 +2,7 @@
#include <QtDebug> #include <QtDebug>
M3UParser::M3UParser(QIODevice* device, QDir directory, QObject* parent) M3UParser::M3UParser(QIODevice* device, const QDir& directory, QObject* parent)
: QObject(parent), : QObject(parent),
device_(device), device_(device),
type_(STANDARD), type_(STANDARD),
@ -38,7 +38,7 @@ const QList<Song>& M3UParser::Parse() {
return songs_; return songs_;
} }
bool M3UParser::ParseMetadata(QString line, M3UParser::Metadata* metadata) const { bool M3UParser::ParseMetadata(const QString& line, M3UParser::Metadata* metadata) const {
// Extended info, eg. // Extended info, eg.
// #EXTINF:123,Sample Artist - Sample title // #EXTINF:123,Sample Artist - Sample title
QString info = line.section(':', 1); QString info = line.section(':', 1);
@ -59,7 +59,7 @@ bool M3UParser::ParseMetadata(QString line, M3UParser::Metadata* metadata) const
return true; return true;
} }
bool M3UParser::ParseTrackLocation(QString line, QUrl* url) const { bool M3UParser::ParseTrackLocation(const QString& line, QUrl* url) const {
if (line.contains(QRegExp("^[a-z]+://"))) { if (line.contains(QRegExp("^[a-z]+://"))) {
// Looks like a url. // Looks like a url.
QUrl temp(line); QUrl temp(line);
@ -72,10 +72,11 @@ bool M3UParser::ParseTrackLocation(QString line, QUrl* url) const {
} }
// Should be a local path. // Should be a local path.
if (line.contains(QRegExp("^([a-zA-Z]:\\)|(/)"))) { if (QDir::isAbsolutePath(line)) {
// Absolute path. // Absolute path.
// Fix windows \. // Fix windows \, eg. C:\foo -> C:/foo.
QString proper_path = line.replace('\\', '/'); QString proper_path = QDir::fromNativeSeparators(line);
// C:/foo -> /C:/foo.
if (!proper_path.startsWith('/')) { if (!proper_path.startsWith('/')) {
proper_path.prepend("/"); proper_path.prepend("/");
} }
@ -83,8 +84,12 @@ bool M3UParser::ParseTrackLocation(QString line, QUrl* url) const {
return true; return true;
} else { } else {
// Relative path. // Relative path.
QString proper_path = line.replace('\\', '/'); QString proper_path = QDir::fromNativeSeparators(line);
QString absolute_path = directory_.absoluteFilePath(proper_path); QString absolute_path = directory_.absoluteFilePath(proper_path);
// C:/foo -> /C:/foo.
if (!absolute_path.startsWith('/')) {
absolute_path.prepend("/");
}
*url = "file://" + absolute_path; *url = "file://" + absolute_path;
return true; return true;
} }

View File

@ -14,10 +14,10 @@ class QIODevice;
class M3UParser : public QObject { class M3UParser : public QObject {
Q_OBJECT Q_OBJECT
public: public:
M3UParser(QIODevice* device, QDir directory = QDir(), QObject* parent = 0); M3UParser(QIODevice* device, const QDir& directory = QDir(), QObject* parent = 0);
virtual ~M3UParser() {} virtual ~M3UParser() {}
const QList<Song>& Parse(); const SongList& Parse();
struct Metadata { struct Metadata {
QString artist; QString artist;
@ -29,21 +29,26 @@ class M3UParser : public QObject {
enum M3UType { enum M3UType {
STANDARD = 0, STANDARD = 0,
EXTENDED, // Includes extended info (track, artist, etc.) EXTENDED, // Includes extended info (track, artist, etc.)
LINK // Points to a directory. LINK, // Points to a directory.
}; };
bool ParseMetadata(QString line, Metadata* metadata) const; bool ParseMetadata(const QString& line, Metadata* metadata) const;
bool ParseTrackLocation(QString line, QUrl* url) const; bool ParseTrackLocation(const QString& line, QUrl* url) const;
FRIEND_TEST(M3UParserTest, ParsesMetadata); FRIEND_TEST(M3UParserTest, ParsesMetadata);
FRIEND_TEST(M3UParserTest, ParsesTrackLocation); FRIEND_TEST(M3UParserTest, ParsesTrackLocation);
FRIEND_TEST(M3UParserTest, ParsesTrackLocationRelative);
FRIEND_TEST(M3UParserTest, ParsesTrackLocationHttp);
#ifdef Q_OS_WIN32
FRIEND_TEST(M3UParserTest, ParsesTrackLocationAbsoluteWindows);
#endif // Q_OS_WIN32
QIODevice* device_; QIODevice* device_;
M3UType type_; M3UType type_;
QDir directory_; QDir directory_;
Metadata current_metadata_; Metadata current_metadata_;
QList<Song> songs_; SongList songs_;
}; };
#endif // M3UPARSER_H #endif // M3UPARSER_H

View File

@ -27,3 +27,27 @@ TEST_F(M3UParserTest, ParsesTrackLocation) {
ASSERT_TRUE(parser_.ParseTrackLocation(line, &url)); ASSERT_TRUE(parser_.ParseTrackLocation(line, &url));
EXPECT_EQ(QUrl("file:///foo/bar.mp3"), url); EXPECT_EQ(QUrl("file:///foo/bar.mp3"), url);
} }
TEST_F(M3UParserTest, ParsesTrackLocationRelative) {
M3UParser parser(NULL, QDir("/tmp"));
QString line("foo/bar.mp3");
QUrl url;
ASSERT_TRUE(parser.ParseTrackLocation(line, &url));
EXPECT_EQ(QUrl("file:///tmp/foo/bar.mp3"), url);
}
TEST_F(M3UParserTest, ParsesTrackLocationHttp) {
QString line("http://example.com/foo/bar.mp3");
QUrl url;
ASSERT_TRUE(parser_.ParseTrackLocation(line, &url));
EXPECT_EQ(QUrl("http://example.com/foo/bar.mp3"), url);
}
#ifdef Q_OS_WIN32
TEST_F(M3UParserTest, ParsesTrackLocationHttp) {
QString line("c:/foo/bar.mp3");
QUrl url;
ASSERT_TRUE(parser_.ParseTrackLocation(line, &url));
EXPECT_EQ(QUrl("file:///c:/foo/bar.mp3"), url);
}
#endif // Q_OS_WIN32