3 #include "../tagvalue.h" 4 #include "../id3/id3genres.h" 6 #include <c++utilities/conversion/conversionexception.h> 7 #include <c++utilities/chrono/format.h> 10 #include <cppunit/TestFixture.h> 11 #include <cppunit/extensions/HelperMacros.h> 14 using namespace Media;
25 CPPUNIT_TEST(testBasics);
26 CPPUNIT_TEST(testBinary);
27 CPPUNIT_TEST(testInteger);
28 CPPUNIT_TEST(testPositionInSet);
29 CPPUNIT_TEST(testTimeSpan);
30 CPPUNIT_TEST(testDateTime);
31 CPPUNIT_TEST(testString);
32 CPPUNIT_TEST(testEqualityOperator);
33 CPPUNIT_TEST_SUITE_END();
42 void testPositionInSet();
46 void testEqualityOperator();
61 CPPUNIT_ASSERT(TagValue::empty().isEmpty());
67 const TagValue binary(
"123", 3, TagDataType::Binary);
68 CPPUNIT_ASSERT_EQUAL(TagDataType::Binary, binary.
type());
70 CPPUNIT_ASSERT_THROW(binary.
toString(), ConversionException);
71 CPPUNIT_ASSERT_THROW(binary.
toInteger(), ConversionException);
80 CPPUNIT_ASSERT(!integer.
isEmpty());
81 CPPUNIT_ASSERT_EQUAL(TagDataType::Integer, integer.
type());
82 CPPUNIT_ASSERT_EQUAL(static_cast<int32>(42), integer.
toInteger());
83 CPPUNIT_ASSERT_EQUAL(
"42"s, integer.
toString());
85 CPPUNIT_ASSERT_EQUAL(
"Country"s,
string(Id3Genres::stringFromIndex(integer.
toStandardGenreIndex())));
89 CPPUNIT_ASSERT_EQUAL(
"-25"s, integer.
toString());
95 CPPUNIT_ASSERT_MESSAGE(
"explicitely assigned zero not considered empty", !integer.
isEmpty());
96 CPPUNIT_ASSERT_EQUAL(
"0"s, integer.
toString());
97 CPPUNIT_ASSERT_EQUAL(DateTime(), integer.
toDateTime());
102 CPPUNIT_ASSERT_MESSAGE(
"cleared vale considered empty", integer.
isEmpty());
103 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"only date (but not type) cleared"s, TagDataType::Integer, integer.
type());
104 CPPUNIT_ASSERT_EQUAL(static_cast<int32>(0), integer.
toInteger());
105 CPPUNIT_ASSERT_EQUAL(
string(), integer.
toString());
106 CPPUNIT_ASSERT_EQUAL(DateTime(), integer.
toDateTime());
114 CPPUNIT_ASSERT_THROW(test.
toInteger(), ConversionException);
115 CPPUNIT_ASSERT_EQUAL(
"4/23"s, test.
toString());
117 CPPUNIT_ASSERT_THROW(test.
toDateTime(), ConversionException);
118 CPPUNIT_ASSERT_THROW(test.
toTimeSpan(), ConversionException);
123 const TimeSpan fiveMinutes(TimeSpan::fromMinutes(5));
126 CPPUNIT_ASSERT_EQUAL(fiveMinutes, timeSpan.
toTimeSpan());
127 CPPUNIT_ASSERT_EQUAL(fiveMinutes.toString(), timeSpan.
toString());
128 CPPUNIT_ASSERT_THROW(timeSpan.
toInteger(), ConversionException);
129 CPPUNIT_ASSERT_THROW(timeSpan.
toDateTime(), ConversionException);
135 const DateTime now(DateTime::now());
138 CPPUNIT_ASSERT_EQUAL(now, dateTime.
toDateTime());
139 CPPUNIT_ASSERT_EQUAL(now.toString(), dateTime.
toString());
140 CPPUNIT_ASSERT_THROW(dateTime.
toInteger(), ConversionException);
141 CPPUNIT_ASSERT_THROW(dateTime.
toTimeSpan(), ConversionException);
149 CPPUNIT_ASSERT_EQUAL(
"\x31\0\x35\0"s,
TagValue(15).toString(TagTextEncoding::Utf16LittleEndian));
150 CPPUNIT_ASSERT_EQUAL(
"\0\x31\0\x35"s,
TagValue(15).toString(TagTextEncoding::Utf16BigEndian));
151 CPPUNIT_ASSERT_EQUAL(15,
TagValue(
"\0\x31\0\x35"s, TagTextEncoding::Utf16BigEndian).toInteger());
152 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"original encoding preserved",
154 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"original encoding preserved",
155 "\0\x31\0\x35"s,
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toString(TagTextEncoding::Unspecified));
156 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"UTF-8 BOM truncated",
158 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"UTF-16 LE BOM truncated",
159 "\0t\0\xe4\0s\0t"s,
TagValue(
"\xff\xfe\0t\0\xe4\0s\0t", 10, TagTextEncoding::Utf16LittleEndian).toString(TagTextEncoding::Unspecified));
160 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"UTF-16 BE BOM truncated",
161 "t\0\xe4\0s\0t\0"s,
TagValue(
"\xfe\xfft\0\xe4\0s\0t\0", 10, TagTextEncoding::Utf16BigEndian).toString(TagTextEncoding::Unspecified));
162 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion via c'tor",
165 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to int", 15,
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toInteger());
168 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to pos",
PositionInSet(15),
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toPositionInSet());
169 CPPUNIT_ASSERT_THROW_MESSAGE(
"failing conversion pos",
TagValue(
"a4 / 15", 7,
TagTextEncoding::Utf8).toPositionInSet(), ConversionException);
170 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to date", DateTime::fromDate(2004, 4, 15),
TagValue(
"2004-04-15", 10,
TagTextEncoding::Utf8).toDateTime());
172 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to time span", TimeSpan::fromHours(1.5),
TagValue(
"01:30:00", 10,
TagTextEncoding::Utf8).toTimeSpan());
174 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to genre from index", 15,
TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian).toStandardGenreIndex());
175 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"conversion to genre from name", 2,
TagValue(
"Country", 7, TagTextEncoding::Latin1).toStandardGenreIndex());
176 CPPUNIT_ASSERT_THROW_MESSAGE(
"failing conversion to genre",
TagValue(
"Kountry", 7, TagTextEncoding::Latin1).toStandardGenreIndex(), ConversionException);
181 CPPUNIT_ASSERT_MESSAGE(
"equality requires identical types or identical string representation"s,
183 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"types might differ"s,
185 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"types might differ"s,
187 CPPUNIT_ASSERT_MESSAGE(
"encoding must be equal if relevant for types"s,
188 TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian) !=
TagValue(
"15", 2, TagTextEncoding::Latin1));
189 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"encoding is ignored when not relevant for types"s,
190 TagValue(
"\0\x31\0\x35", 4, TagTextEncoding::Utf16BigEndian),
TagValue(15));
195 CPPUNIT_ASSERT_MESSAGE(
"meta-data must be equal"s, withDescription !=
TagValue(15));
196 TagValue withDescription2(withDescription);
197 CPPUNIT_ASSERT_EQUAL(withDescription, withDescription2);
199 CPPUNIT_ASSERT(withDescription != withDescription2);
201 CPPUNIT_ASSERT_EQUAL(withDescription, withDescription2);
The TagValueTests class tests the TagValue class.
CPPUNIT_TEST_SUITE_REGISTRATION(TagValueTests)
void testEqualityOperator()