3 #include "../id3/id3genres.h" 4 #include "../tagvalue.h" 6 #include <c++utilities/chrono/format.h> 7 #include <c++utilities/conversion/conversionexception.h> 11 #include <cppunit/TestFixture.h> 12 #include <cppunit/extensions/HelperMacros.h> 26 CPPUNIT_TEST(testBasics);
27 CPPUNIT_TEST(testBinary);
28 CPPUNIT_TEST(testInteger);
29 CPPUNIT_TEST(testPositionInSet);
30 CPPUNIT_TEST(testTimeSpan);
31 CPPUNIT_TEST(testDateTime);
32 CPPUNIT_TEST(testString);
33 CPPUNIT_TEST(testEqualityOperator);
34 CPPUNIT_TEST_SUITE_END();
43 void testPositionInSet();
47 void testEqualityOperator();
62 CPPUNIT_ASSERT(TagValue::empty().isEmpty());
68 const TagValue binary(
"123", 3, TagDataType::Binary);
69 CPPUNIT_ASSERT_EQUAL(TagDataType::Binary, binary.
type());
71 CPPUNIT_ASSERT_THROW(binary.
toString(), ConversionException);
72 CPPUNIT_ASSERT_THROW(binary.
toInteger(), ConversionException);
81 CPPUNIT_ASSERT(!integer.
isEmpty());
82 CPPUNIT_ASSERT_EQUAL(TagDataType::Integer, integer.
type());
83 CPPUNIT_ASSERT_EQUAL(static_cast<int32>(42), integer.
toInteger());
84 CPPUNIT_ASSERT_EQUAL(
"42"s, integer.
toString());
86 CPPUNIT_ASSERT_EQUAL(
"Country"s,
string(Id3Genres::stringFromIndex(integer.
toStandardGenreIndex())));
90 CPPUNIT_ASSERT_EQUAL(
"-25"s, integer.
toString());
96 CPPUNIT_ASSERT_MESSAGE(
"explicitely assigned zero not considered empty", !integer.
isEmpty());
97 CPPUNIT_ASSERT_EQUAL(
"0"s, integer.
toString());
98 CPPUNIT_ASSERT_EQUAL(DateTime(), integer.
toDateTime());
103 CPPUNIT_ASSERT_MESSAGE(
"cleared vale considered empty", integer.
isEmpty());
104 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"only date (but not type) cleared"s, TagDataType::Integer, integer.
type());
105 CPPUNIT_ASSERT_EQUAL(static_cast<int32>(0), integer.
toInteger());
106 CPPUNIT_ASSERT_EQUAL(
string(), integer.
toString());
107 CPPUNIT_ASSERT_EQUAL(DateTime(), integer.
toDateTime());
115 CPPUNIT_ASSERT_THROW(test.
toInteger(), ConversionException);
116 CPPUNIT_ASSERT_EQUAL(
"4/23"s, test.
toString());
118 CPPUNIT_ASSERT_THROW(test.
toDateTime(), ConversionException);
119 CPPUNIT_ASSERT_THROW(test.
toTimeSpan(), ConversionException);
124 const TimeSpan fiveMinutes(TimeSpan::fromMinutes(5));
127 CPPUNIT_ASSERT_EQUAL(fiveMinutes, timeSpan.
toTimeSpan());
128 CPPUNIT_ASSERT_EQUAL(fiveMinutes.toString(), timeSpan.
toString());
129 CPPUNIT_ASSERT_THROW(timeSpan.
toInteger(), ConversionException);
130 CPPUNIT_ASSERT_THROW(timeSpan.
toDateTime(), ConversionException);
136 const DateTime now(DateTime::now());
139 CPPUNIT_ASSERT_EQUAL(now, dateTime.
toDateTime());
140 CPPUNIT_ASSERT_EQUAL(now.toString(), dateTime.
toString());
141 CPPUNIT_ASSERT_THROW(dateTime.
toInteger(), ConversionException);
142 CPPUNIT_ASSERT_THROW(dateTime.
toTimeSpan(), ConversionException);
150 CPPUNIT_ASSERT_EQUAL(
"\x31\0\x35\0"s,
TagValue(15).toString(TagTextEncoding::Utf16LittleEndian));
151 CPPUNIT_ASSERT_EQUAL(
"\0\x31\0\x35"s,
TagValue(15).toString(TagTextEncoding::Utf16BigEndian));
152 CPPUNIT_ASSERT_EQUAL(15,
TagValue(
"\0\x31\0\x35"s, TagTextEncoding::Utf16BigEndian).toInteger());
153 CPPUNIT_ASSERT_EQUAL_MESSAGE(
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"original encoding preserved",
"\0\x31\0\x35"s,
156 TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toString(TagTextEncoding::Unspecified));
157 CPPUNIT_ASSERT_EQUAL_MESSAGE(
159 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"UTF-16 LE BOM truncated",
"\0t\0\xe4\0s\0t"s,
160 TagValue(
"\xff\xfe\0t\0\xe4\0s\0t", 10, TagTextEncoding::Utf16LittleEndian).toString(TagTextEncoding::Unspecified));
161 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"UTF-16 BE BOM truncated",
"t\0\xe4\0s\0t\0"s,
162 TagValue(
"\xfe\xfft\0\xe4\0s\0t\0", 10, TagTextEncoding::Utf16BigEndian).toString(TagTextEncoding::Unspecified));
163 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion via c'tor",
"15\xe4"s,
166 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to int", 15,
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toInteger());
169 CPPUNIT_ASSERT_EQUAL_MESSAGE(
170 "conversion to pos",
PositionInSet(15),
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toPositionInSet());
171 CPPUNIT_ASSERT_THROW_MESSAGE(
"failing conversion pos",
TagValue(
"a4 / 15", 7,
TagTextEncoding::Utf8).toPositionInSet(), ConversionException);
172 CPPUNIT_ASSERT_EQUAL_MESSAGE(
175 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to time span", TimeSpan::fromHours(1.5),
TagValue(
"01:30:00", 10,
TagTextEncoding::Utf8).toTimeSpan());
177 CPPUNIT_ASSERT_EQUAL_MESSAGE(
178 "conversion to genre from index", 15,
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toStandardGenreIndex());
179 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to genre from name", 2,
TagValue(
"Country", 7, TagTextEncoding::Latin1).toStandardGenreIndex());
180 CPPUNIT_ASSERT_THROW_MESSAGE(
181 "failing conversion to genre",
TagValue(
"Kountry", 7, TagTextEncoding::Latin1).toStandardGenreIndex(), ConversionException);
186 CPPUNIT_ASSERT_MESSAGE(
"equality requires identical types or identical string representation"s,
TagValue(0) != TagValue::empty());
187 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"types might differ"s,
TagValue(15),
TagValue(15));
188 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"types might differ"s,
TagValue(
"15", 2, TagTextEncoding::Latin1),
TagValue(15));
189 CPPUNIT_ASSERT_MESSAGE(
"encoding must be equal if relevant for types"s,
190 TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian) !=
TagValue(
"15", 2, TagTextEncoding::Latin1));
191 CPPUNIT_ASSERT_EQUAL_MESSAGE(
192 "encoding is ignored when not relevant for types"s,
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian),
TagValue(15));
197 CPPUNIT_ASSERT_MESSAGE(
"meta-data must be equal"s, withDescription !=
TagValue(15));
198 TagValue withDescription2(withDescription);
199 CPPUNIT_ASSERT_EQUAL(withDescription, withDescription2);
201 CPPUNIT_ASSERT(withDescription != withDescription2);
203 CPPUNIT_ASSERT_EQUAL(withDescription, withDescription2);
void setMimeType(const std::string &mimeType)
Sets the MIME type.
std::size_t dataSize() const
Returns the size of the assigned value in bytes.
The TagValueTests class tests the TagValue class.
int toStandardGenreIndex() const
Converts the value of the current TagValue object to its equivalent standard genre index...
bool isEmpty() const
Returns an indication whether an value is assigned.
const std::string & mimeType() const
Returns the MIME type.
void assignDateTime(ChronoUtilities::DateTime value)
Assigns the given DateTime value.
ChronoUtilities::TimeSpan toTimeSpan() const
Converts the value of the current TagValue object to its equivalent TimeSpan representation.
PositionInSet toPositionInSet() const
Converts the value of the current TagValue object to its equivalent PositionInSet representation...
char * dataPointer()
Returns a pointer to the raw data assigned to the current instance.
TagDataType type() const
Returns the type of the assigned value.
ChronoUtilities::DateTime toDateTime() const
Converts the value of the current TagValue object to its equivalent DateTime representation.
int32 toInteger() const
Converts the value of the current TagValue object to its equivalent integer representation.
void assignInteger(int value)
Assigns the given integer value.
void clearData()
Clears the assigned data.
CPPUNIT_TEST_SUITE_REGISTRATION(TagValueTests)
void setDescription(const std::string &value, TagTextEncoding encoding=TagTextEncoding::Latin1)
Sets the description.
std::string toString(TagTextEncoding encoding=TagTextEncoding::Unspecified) const
Converts the value of the current TagValue object to its equivalent std::string representation.
void assignTimeSpan(ChronoUtilities::TimeSpan value)
Assigns the given TimeSpan value.
void testEqualityOperator()