test_icy_parser.cxx 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Unit tests for class IcyMetaDataParser.
 */

#include "config.h"

/* include the .cxx file to get access to internal functions */
#include "IcyMetaDataParser.cxx"

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>

#include <string>

17 18 19 20 21 22 23 24 25 26 27
#include <string.h>

static Tag *
icy_parse_tag(const char *p)
{
	char *q = strdup(p);
	Tag *tag = icy_parse_tag(q, q + strlen(q));
	free(q);
	return tag;
}

28 29 30
static void
CompareTagTitle(const Tag &tag, const std::string &title)
{
Max Kellermann's avatar
Max Kellermann committed
31
	CPPUNIT_ASSERT_EQUAL(uint16_t(1), tag.num_items);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

	const TagItem &item = *tag.items[0];
	CPPUNIT_ASSERT_EQUAL(TAG_TITLE, item.type);
	CPPUNIT_ASSERT_EQUAL(title, std::string(item.value));
}

static void
TestIcyParserTitle(const char *input, const char *title)
{
	Tag *tag = icy_parse_tag(input);
	CompareTagTitle(*tag, title);
	delete tag;
}

static void
TestIcyParserEmpty(const char *input)
{
	Tag *tag = icy_parse_tag(input);
Max Kellermann's avatar
Max Kellermann committed
50
	CPPUNIT_ASSERT_EQUAL(uint16_t(0), tag->num_items);
51 52 53 54 55 56 57 58 59 60 61 62 63 64
	delete tag;
}

class IcyTest : public CppUnit::TestFixture {
	CPPUNIT_TEST_SUITE(IcyTest);
	CPPUNIT_TEST(TestIcyMetadataParser);
	CPPUNIT_TEST_SUITE_END();

public:
	void TestIcyMetadataParser() {
		TestIcyParserEmpty("foo=bar;");
		TestIcyParserTitle("StreamTitle='foo bar'", "foo bar");
		TestIcyParserTitle("StreamTitle='foo bar';", "foo bar");
		TestIcyParserTitle("StreamTitle='foo\"bar';", "foo\"bar");
65
		TestIcyParserTitle("StreamTitle='foo=bar';", "foo=bar");
66 67 68 69
		TestIcyParserTitle("a=b;StreamTitle='foo';", "foo");
		TestIcyParserTitle("a=;StreamTitle='foo';", "foo");
		TestIcyParserTitle("a=b;StreamTitle='foo';c=d", "foo");
		TestIcyParserTitle("a=b;StreamTitle='foo'", "foo");
70 71 72
		TestIcyParserTitle("a='b;c';StreamTitle='foo;bar'", "foo;bar");
		TestIcyParserTitle("a='b'c';StreamTitle='foo'bar'", "foo'bar");
		TestIcyParserTitle("StreamTitle='fo'o'b'ar';a='b'c'd'", "fo'o'b'ar");
73 74 75 76 77 78 79 80 81 82 83 84 85
	}
};

CPPUNIT_TEST_SUITE_REGISTRATION(IcyTest);

int
main(gcc_unused int argc, gcc_unused char **argv)
{
	CppUnit::TextUi::TestRunner runner;
	auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
	runner.addTest(registry.makeTest());
	return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}