Skip to content

Commit

Permalink
Add unit tests for C bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ufleisch committed Oct 20, 2023
1 parent 1438a9c commit d95e9f4
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
5 changes: 5 additions & 0 deletions taglib/fileref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ const FileRef::FileTypeResolver *FileRef::addFileTypeResolver(const FileRef::Fil
return resolver;
}

void FileRef::clearFileTypeResolvers() // static
{
fileTypeResolvers.clear();
}

StringList FileRef::defaultFileExtensions()
{
StringList l;
Expand Down
5 changes: 5 additions & 0 deletions taglib/fileref.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ namespace TagLib {
*/
static const FileTypeResolver *addFileTypeResolver(const FileTypeResolver *resolver);

/*!
* Remove all resolvers added by addFileTypeResolver().
*/
static void clearFileTypeResolvers();

/*!
* As is mentioned elsewhere in this class's documentation, the default file
* type resolution code provided by TagLib only works by comparing file
Expand Down
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}/../taglib
${CMAKE_CURRENT_SOURCE_DIR}/../taglib/toolkit
${CMAKE_CURRENT_SOURCE_DIR}/../bindings/c
${CMAKE_CURRENT_SOURCE_DIR}/../taglib/ape
${CMAKE_CURRENT_SOURCE_DIR}/../taglib/asf
${CMAKE_CURRENT_SOURCE_DIR}/../taglib/mpeg/id3v1
Expand Down Expand Up @@ -72,12 +73,13 @@ SET(test_runner_SRCS
test_dsf.cpp
test_sizes.cpp
test_versionnumber.cpp
test_tag_c.cpp
)

INCLUDE_DIRECTORIES(${CPPUNIT_INCLUDE_DIR})

ADD_EXECUTABLE(test_runner ${test_runner_SRCS})
TARGET_LINK_LIBRARIES(test_runner tag ${CPPUNIT_LIBRARIES})
TARGET_LINK_LIBRARIES(test_runner tag tag_c ${CPPUNIT_LIBRARIES})

ADD_TEST(test_runner test_runner)
ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND} -V
Expand Down
2 changes: 2 additions & 0 deletions tests/test_fileref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ class TestFileRef : public CppUnit::TestFixture
FileRef f(&s);
CPPUNIT_ASSERT(dynamic_cast<MP4::File *>(f.file()) != nullptr);
}

FileRef::clearFileTypeResolvers();
}

};
Expand Down
148 changes: 148 additions & 0 deletions tests/test_tag_c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/***************************************************************************
copyright : (C) 2023 by Urs Fleisch
email : [email protected]
***************************************************************************/

/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/

#include <string>
#include <unordered_map>
#include <list>

#include "tag_c.h"
#include "tbytevector.h"
#include "tstring.h"
#include <cppunit/extensions/HelperMacros.h>
#include "utils.h"

using namespace TagLib;

class TestTagC : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTagC);
CPPUNIT_TEST(testMp3);
CPPUNIT_TEST_SUITE_END();

public:
void testMp3()
{
ScopedFileCopy copy("xing", ".mp3");

{
TagLib_File *file = taglib_file_new(copy.fileName().c_str());
CPPUNIT_ASSERT(taglib_file_is_valid(file));
const TagLib_AudioProperties *audioProperties = taglib_file_audioproperties(file);
CPPUNIT_ASSERT_EQUAL(32, taglib_audioproperties_bitrate(audioProperties));
CPPUNIT_ASSERT_EQUAL(2, taglib_audioproperties_channels(audioProperties));
CPPUNIT_ASSERT_EQUAL(2, taglib_audioproperties_length(audioProperties));
CPPUNIT_ASSERT_EQUAL(44100, taglib_audioproperties_samplerate(audioProperties));

TagLib_Tag *tag = taglib_file_tag(file);
CPPUNIT_ASSERT_EQUAL(""s, std::string(taglib_tag_album(tag)));
taglib_tag_set_album(tag, "Album");
taglib_tag_set_artist(tag, "Artist");
taglib_tag_set_comment(tag, "Comment");
taglib_tag_set_genre(tag, "Genre");
taglib_tag_set_title(tag, "Title");
taglib_tag_set_track(tag, 2);
taglib_tag_set_year(tag, 2023);

taglib_property_set(file, "COMPOSER", "Composer 1");
taglib_property_set_append(file, "COMPOSER", "Composer 2");
taglib_property_set(file, "ALBUMARTIST", "Album Artist");

TAGLIB_COMPLEX_PROPERTY_PICTURE(props, "JPEG Data", 9, "Written by TagLib",
"image/jpeg", "Front Cover");
taglib_complex_property_set(file, "PICTURE", props);

taglib_file_save(file);
taglib_file_free(file);
}
{
TagLib_File *file = taglib_file_new(copy.fileName().c_str());
CPPUNIT_ASSERT(taglib_file_is_valid(file));

TagLib_Tag *tag = taglib_file_tag(file);
CPPUNIT_ASSERT_EQUAL("Album"s, std::string(taglib_tag_album(tag)));
CPPUNIT_ASSERT_EQUAL("Artist"s, std::string(taglib_tag_artist(tag)));
CPPUNIT_ASSERT_EQUAL("Comment"s, std::string(taglib_tag_comment(tag)));
CPPUNIT_ASSERT_EQUAL("Genre"s, std::string(taglib_tag_genre(tag)));
CPPUNIT_ASSERT_EQUAL("Title"s, std::string(taglib_tag_title(tag)));
CPPUNIT_ASSERT_EQUAL(2U, taglib_tag_track(tag));
CPPUNIT_ASSERT_EQUAL(2023U, taglib_tag_year(tag));

char **keys = taglib_property_keys(file);
CPPUNIT_ASSERT(keys);
std::unordered_map<std::string, std::list<std::string>> propertyMap;
char **keyPtr = keys;
while(*keyPtr) {
char **values = taglib_property_get(file, *keyPtr);
char **valuePtr = values;
std::list<std::string> valueList;
while(*valuePtr) {
valueList.push_back(*valuePtr++);
}
taglib_property_free(values);
propertyMap[*keyPtr++] = valueList;
}
taglib_property_free(keys);
const std::unordered_map<std::string, std::list<std::string>> expected {
{"TRACKNUMBER"s, {"2"s}},
{"TITLE"s, {"Title"s}},
{"GENRE"s, {"Genre"s}},
{"DATE"s, {"2023"s}},
{"COMPOSER"s, {"Composer 1"s, "Composer 2"s}},
{"COMMENT"s, {"Comment"s}},
{"ARTIST"s, {"Artist"s}},
{"ALBUMARTIST"s, {"Album Artist"s}},
{"ALBUM"s, {"Album"s}}
};
CPPUNIT_ASSERT(expected == propertyMap);

char **complexKeys = taglib_complex_property_keys(file);
CPPUNIT_ASSERT(complexKeys);
std::list<std::string> keyList;
char **complexKeyPtr = complexKeys;
while(*complexKeyPtr) {
keyList.push_back(*complexKeyPtr++);
}
taglib_complex_property_free_keys(complexKeys);
CPPUNIT_ASSERT(std::list{"PICTURE"s} == keyList);

TagLib_Complex_Property_Attribute*** properties =
taglib_complex_property_get(file, "PICTURE");
TagLib_Complex_Property_Picture_Data picture;
taglib_picture_from_complex_property(properties, &picture);
CPPUNIT_ASSERT_EQUAL("image/jpeg"s, std::string(picture.mimeType));
CPPUNIT_ASSERT_EQUAL("Written by TagLib"s, std::string(picture.description));
CPPUNIT_ASSERT_EQUAL("Front Cover"s, std::string(picture.pictureType));
CPPUNIT_ASSERT_EQUAL(ByteVector("JPEG Data"), ByteVector(picture.data, 9));
CPPUNIT_ASSERT_EQUAL(9U, picture.size);
taglib_complex_property_free(properties);

taglib_file_free(file);
}

taglib_tag_free_strings();
}
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestTagC);

0 comments on commit d95e9f4

Please sign in to comment.