Skip to content

Commit

Permalink
Convert BUrl tests to the UnitTester framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkomandy committed Jun 3, 2014
1 parent 5abe892 commit 68c13f9
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 118 deletions.
7 changes: 5 additions & 2 deletions src/tests/kits/net/url/Jamfile
@@ -1,6 +1,9 @@
SubDir HAIKU_TOP src tests kits net url ;

Application url_test
: url_test.cpp
UnitTestLib servicekittest.so :
ServiceKitTestAddon.cpp

UrlTest.cpp

: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
21 changes: 21 additions & 0 deletions src/tests/kits/net/url/ServiceKitTestAddon.cpp
@@ -0,0 +1,21 @@
/*
* Copyright 2014 Haiku, Inc.
* Distributed under the terms of the MIT License.
*/


#include <TestSuite.h>
#include <TestSuiteAddon.h>

#include "UrlTest.h"


BTestSuite*
getTestSuite()
{
BTestSuite* suite = new BTestSuite("ServicesKit");

UrlTest::AddTests(*suite);

return suite;
}
144 changes: 144 additions & 0 deletions src/tests/kits/net/url/UrlTest.cpp
@@ -0,0 +1,144 @@
/*
* Copyright 2010, Christophe Huriaux
* Copyright 2014, Haiku, inc.
* Distributed under the terms of the MIT licence
*/


#include "UrlTest.h"


#include <cstdlib>
#include <cstring>
#include <cstdio>

#include <NetworkKit.h>

#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>


UrlTest::UrlTest()
{
}


UrlTest::~UrlTest()
{
}


// Test if rebuild url are the same length of parsed one
void UrlTest::LengthTest()
{
int8 testIndex;
BUrl testUrl;

const char* kTestLength[] =
{
"http://user:pass@www.foo.com:80/path?query#fragment",
"http://user:pass@www.foo.com:80/path?query#",
"http://user:pass@www.foo.com:80/path?query",
"http://user:pass@www.foo.com:80/path?",
"http://user:pass@www.foo.com:80/path",
"http://user:pass@www.foo.com:80/",
"http://user:pass@www.foo.com",
"http://user:pass@",
"http://www.foo.com",
"http://",
"http:"
};

for (testIndex = 0; kTestLength[testIndex] != NULL; testIndex++)
{
NextSubTest();
testUrl.SetUrlString(kTestLength[testIndex]);

CPPUNIT_ASSERT(strlen(kTestLength[testIndex]) == strlen(testUrl.UrlString()));
}
}

typedef struct
{
const char* url;

struct
{
const char* protocol;
const char* userName;
const char* password;
const char* host;
int16 port;
const char* path;
const char* request;
const char* fragment;
} expected;
} ExplodeTest;


const ExplodeTest kTestExplode[] =
// Url
// Protocol User Password Hostname Port Path Request Fragment
// -------- --------- --------- --------- ---- ---------- ---------- ------------
{
{ "http://user:pass@host:80/path?query#fragment",
{ "http", "user", "pass", "host", 80, "/path", "query", "fragment" } },
{ "http://www.host.tld/path?query#fragment",
{ "http", "", "", "www.host.tld",0, "/path", "query", "fragment" } }
};

void UrlTest::ExplodeImplodeTest()
{
uint8 testIndex;
BUrl testUrl;

for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
{
NextSubTest();
testUrl.SetUrlString(kTestExplode[testIndex].url);

CPPUNIT_ASSERT(BString(kTestExplode[testIndex].url)
== BString(testUrl.UrlString()));
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.protocol)
== BString(testUrl.Protocol()));
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.userName)
== BString(testUrl.UserName()));
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.password)
== BString(testUrl.Password()));
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.host)
== BString(testUrl.Host()));
CPPUNIT_ASSERT(kTestExplode[testIndex].expected.port == testUrl.Port());
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.path)
== BString(testUrl.Path()));
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.request)
== BString(testUrl.Request()));
CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.fragment)
== BString(testUrl.Fragment()));
}
}


void
UrlTest::PathOnly()
{
BUrl test = "lol";
CPPUNIT_ASSERT(test.HasPath());
CPPUNIT_ASSERT(test.Path() == "lol");
CPPUNIT_ASSERT(test.UrlString() == "lol");
}


/* static */ void
UrlTest::AddTests(BTestSuite& parent)
{
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UrlTest");

suite.addTest(new CppUnit::TestCaller<UrlTest>(
"UrlTest::LengthTest", &UrlTest::LengthTest));
suite.addTest(new CppUnit::TestCaller<UrlTest>(
"UrlTest::ExplodeImplodeTest", &UrlTest::ExplodeImplodeTest));
suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::PathOnly",
&UrlTest::PathOnly));

parent.addTest("UrlTest", &suite);
}
26 changes: 26 additions & 0 deletions src/tests/kits/net/url/UrlTest.h
@@ -0,0 +1,26 @@
/*
* Copyright 2014 Haiku, inc.
* Distributed under the terms of the MIT License.
*/
#ifndef URL_TEST_H
#define URL_TEST_H


#include <TestCase.h>
#include <TestSuite.h>


class UrlTest: public BTestCase {
public:
UrlTest();
virtual ~UrlTest();

void LengthTest();
void ExplodeImplodeTest();
void PathOnly();

static void AddTests(BTestSuite& suite);
};


#endif
116 changes: 0 additions & 116 deletions src/tests/kits/net/url/url_test.cpp

This file was deleted.

0 comments on commit 68c13f9

Please sign in to comment.