Convert BUrl tests to the UnitTester framework.

This commit is contained in:
Adrien Destugues
2014-06-03 13:34:35 +02:00
parent 5abe892c76
commit 68c13f94fb
5 changed files with 196 additions and 118 deletions
+5 -2
View File
@@ -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++)
;
@@ -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
View File
@@ -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:[email protected]:80/path?query#fragment",
"http://user:[email protected]:80/path?query#",
"http://user:[email protected]:80/path?query",
"http://user:[email protected]:80/path?",
"http://user:[email protected]:80/path",
"http://user:[email protected]:80/",
"http://user:[email protected]",
"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
View File
@@ -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
View File
@@ -1,116 +0,0 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <NetworkKit.h>
#ifdef ASSERT
#undef ASSERT
#endif
#define REPORT(assert, line) cout << "ASSERT() failed at line " << line << ": " << #assert << endl
#define ASSERT(assertion) { if (!(assertion)) { REPORT(assertion, __LINE__ ); } }
using namespace std;
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 char* kTestLength[] =
{
"http://user:[email protected]:80/path?query#fragment",
"http://user:[email protected]:80/path?query#",
"http://user:[email protected]:80/path?query",
"http://user:[email protected]:80/path?",
"http://user:[email protected]:80/path",
"http://user:[email protected]:80/",
"http://user:[email protected]",
"http://user:pass@",
"http://www.foo.com",
"http://",
"http:"
};
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" } }
};
// Test if rebuild url are the same length of parsed one
void lengthTest()
{
int8 testIndex;
BUrl testUrl;
for (testIndex = 0; kTestLength[testIndex] != NULL; testIndex++)
{
testUrl.SetUrlString(kTestLength[testIndex]);
cout << "`" << kTestLength[testIndex] << "' vs `" << testUrl.UrlString() << "'" << endl;
ASSERT(strlen(kTestLength[testIndex]) == strlen(testUrl.UrlString()));
}
}
void explodeImplodeTest()
{
uint8 testIndex;
BUrl testUrl;
for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
{
testUrl.SetUrlString(kTestExplode[testIndex].url);
ASSERT(BString(kTestExplode[testIndex].url) == BString(testUrl.UrlString()));
ASSERT(BString(kTestExplode[testIndex].expected.protocol) == BString(testUrl.Protocol()));
ASSERT(BString(kTestExplode[testIndex].expected.userName) == BString(testUrl.UserName()));
ASSERT(BString(kTestExplode[testIndex].expected.password) == BString(testUrl.Password()));
ASSERT(BString(kTestExplode[testIndex].expected.host) == BString(testUrl.Host()));
ASSERT(kTestExplode[testIndex].expected.port == testUrl.Port());
ASSERT(BString(kTestExplode[testIndex].expected.path) == BString(testUrl.Path()));
ASSERT(BString(kTestExplode[testIndex].expected.request) == BString(testUrl.Request()));
ASSERT(BString(kTestExplode[testIndex].expected.fragment) == BString(testUrl.Fragment()));
}
}
using std::cout;
using std::endl;
int
main(int, char**)
{
cout << "Running lengthTest:" << endl;
lengthTest();
cout << endl;
cout << "Running explodeImplodeTest:" << endl;
explodeImplodeTest();
cout << endl;
BUrl test = "lol";
cout << "Path: " << test.HasPath() << " -> " << test.Path() << endl;
cout << "Rebuild: " << test.UrlString() << endl;
return EXIT_SUCCESS;
}