Commit 49d854c2 authored by legoc's avatar legoc
Browse files

(split) Finished tests for C++ Strings classes

parent 4e686cd5
This diff is collapsed.
......@@ -19,6 +19,7 @@
#include <string>
#include <vector>
#include <optional>
namespace cameo {
......@@ -52,14 +53,19 @@ public:
/**
* Constructor.
*/
NameId(const std::string& name, int id = -1);
NameId(const std::string& name);
/**
* Constructor.
*/
NameId(const std::string& name, int id);
const std::string& getName() const;
/**
* Return the id. A -1 value indicates that there is no id.
* Return the id which may not exist.
*/
int getId() const;
const std::optional<int>& getId() const;
static NameId parse(const std::string& str);
......@@ -67,7 +73,7 @@ public:
private:
std::string m_name;
int m_id;
std::optional<int> m_id;
};
class ApplicationIdentity {
......@@ -90,10 +96,11 @@ private:
class ApplicationAndStarterIdentities {
public:
ApplicationAndStarterIdentities(const ApplicationIdentity& application);
ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter);
const ApplicationIdentity& getApplication() const;
const ApplicationIdentity& getStarter() const;
const std::optional<ApplicationIdentity>& getStarter() const;
static ApplicationAndStarterIdentities parse(const std::string& str);
......@@ -101,7 +108,7 @@ public:
private:
ApplicationIdentity m_application;
ApplicationIdentity m_starter;
std::optional<ApplicationIdentity> m_starter;
};
}
......
......@@ -89,6 +89,10 @@ std::string Endpoint::toString() const {
return string("tcp://") + m_address + ":" + to_string(m_port);
}
NameId::NameId(const std::string& name) :
m_name(name) {
}
NameId::NameId(const std::string& name, int id) :
m_name(name),
m_id(id) {
......@@ -98,7 +102,7 @@ const std::string& NameId::getName() const {
return m_name;
}
int NameId::getId() const {
const std::optional<int>& NameId::getId() const {
return m_id;
}
......@@ -111,24 +115,23 @@ NameId NameId::parse(const std::string& str) {
}
string name = tokens[0];
int id = -1;
if (tokens.size() == 2) {
try {
id = stoi(tokens[1]);
int id = stoi(tokens[1]);
return NameId(name, id);
}
catch (...) {
throw new BadFormatException("Bad format for endpoint " + str);
}
}
return NameId(name, id);
return NameId(name);
}
std::string NameId::toString() const {
if (m_id > 0) {
return m_name + "." + to_string(m_id);
return m_name + "." + to_string(m_id.value());
}
return m_name;
}
......@@ -161,6 +164,10 @@ std::string ApplicationIdentity::toString() const {
return m_nameId.toString() + "@" + m_endpoint.toString();
}
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application) :
m_application(application) {
}
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter) :
m_application(application),
m_starter(starter) {
......@@ -170,7 +177,7 @@ const ApplicationIdentity& ApplicationAndStarterIdentities::getApplication() con
return m_application;
}
const ApplicationIdentity& ApplicationAndStarterIdentities::getStarter() const {
const std::optional<ApplicationIdentity>& ApplicationAndStarterIdentities::getStarter() const {
return m_starter;
}
......@@ -198,7 +205,7 @@ ApplicationAndStarterIdentities ApplicationAndStarterIdentities::parse(const std
ApplicationIdentity application = ApplicationIdentity::parse(applicationString);
return ApplicationAndStarterIdentities(application, application);
return ApplicationAndStarterIdentities(application);
}
else {
// Format <name>@<endpoint>:<name>@<endpoint>
......@@ -220,12 +227,10 @@ ApplicationAndStarterIdentities ApplicationAndStarterIdentities::parse(const std
}
std::string ApplicationAndStarterIdentities::toString() const {
// if (starter != null) {
// return application + ":" + starter;
// }
// return application + ":";
return "";
if (m_starter.has_value()) {
return m_application.toString() + ":" + m_starter.value().toString();
}
return m_application.toString() + ":";
}
}
......
......@@ -8,6 +8,7 @@ target_link_libraries(testendpoint PUBLIC
add_test(endpoint testendpoint)
add_executable(testname TestName.cpp)
target_link_libraries(testname PUBLIC
......@@ -16,10 +17,29 @@ target_link_libraries(testname PUBLIC
add_test(name testname)
add_executable(testnameid TestNameId.cpp)
target_link_libraries(testnameid PUBLIC
cameo
)
add_test(nameid testnameid)
\ No newline at end of file
add_test(nameid testnameid)
add_executable(testapplicationidentity TestApplicationIdentity.cpp)
target_link_libraries(testapplicationidentity PUBLIC
cameo
)
add_test(applicationidentity testapplicationidentity)
add_executable(testapplicationandstarteridentities TestApplicationAndStarterIdentities.cpp)
target_link_libraries(testapplicationandstarteridentities PUBLIC
cameo
)
add_test(applicationandstarteridentities testapplicationandstarteridentities)
\ No newline at end of file
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
ApplicationAndStarterIdentities identities = ApplicationAndStarterIdentities::parse("my-app.31@tcp://gamma75:9999:your-app.15@tcp://gamma63:789");
CAMEO_ASSERT_TRUE("my-app.31@tcp://gamma75:9999" == identities.getApplication().toString());
CAMEO_ASSERT_TRUE(identities.getStarter().has_value());
CAMEO_ASSERT_TRUE("your-app.15@tcp://gamma63:789" == identities.getStarter().value().toString());
identities = ApplicationAndStarterIdentities::parse("my-app.31@tcp://gamma75:9999:");
CAMEO_ASSERT_TRUE("my-app.31@tcp://gamma75:9999" == identities.getApplication().toString());
CAMEO_ASSERT_TRUE(!identities.getStarter().has_value());
bool error = false;
try {
ApplicationAndStarterIdentities::parse("my-app.31@tcp://gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
CAMEO_ASSERT_TRUE("my-app.31@tcp://gamma75:9999" == ApplicationIdentity(NameId("my-app", 31), Endpoint("gamma75", 9999)).toString());
CAMEO_ASSERT_TRUE("my-app@tcp://gamma75:9999" == ApplicationIdentity(NameId("my-app"), Endpoint("gamma75", 9999)).toString());
ApplicationIdentity identity = ApplicationIdentity::parse("my-app.31@tcp://gamma75:9999");
CAMEO_ASSERT_TRUE("my-app" == identity.getNameId().getName());
CAMEO_ASSERT_TRUE(identity.getNameId().getId().has_value());
CAMEO_ASSERT_EQUAL(31, identity.getNameId().getId().value());
CAMEO_ASSERT_TRUE("gamma75" == identity.getEndpoint().getAddress());
CAMEO_ASSERT_EQUAL(9999, identity.getEndpoint().getPort());
identity = ApplicationIdentity::parse("my-app.31@tcp://127.65.198.1:9999");
CAMEO_ASSERT_TRUE("my-app" == identity.getNameId().getName());
CAMEO_ASSERT_TRUE(identity.getNameId().getId().has_value());
CAMEO_ASSERT_EQUAL(31, identity.getNameId().getId().value());
CAMEO_ASSERT_TRUE("127.65.198.1" == identity.getEndpoint().getAddress());
CAMEO_ASSERT_EQUAL(9999, identity.getEndpoint().getPort());
identity = ApplicationIdentity::parse("my-app@tcp://gamma75:9999");
CAMEO_ASSERT_TRUE("my-app" == identity.getNameId().getName());
CAMEO_ASSERT_TRUE(!identity.getNameId().getId().has_value());
CAMEO_ASSERT_TRUE("gamma75" == identity.getEndpoint().getAddress());
CAMEO_ASSERT_EQUAL(9999, identity.getEndpoint().getPort());
bool error = false;
try {
ApplicationIdentity::parse("my-app.ff@tcp://gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
error = false;
try {
ApplicationIdentity::parse("my-app.ff@tcp:/gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
error = false;
try {
ApplicationIdentity::parse("my-app.ff@tcp://gamma75:99G");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
......@@ -12,10 +12,11 @@ int main(int argc, char *argv[]) {
NameId nameId = NameId::parse("my-app.31");
CAMEO_ASSERT_TRUE("my-app" == nameId.getName());
CAMEO_ASSERT_EQUAL(31, nameId.getId());
CAMEO_ASSERT_TRUE(nameId.getId().has_value());
CAMEO_ASSERT_EQUAL(31, nameId.getId().value());
nameId = NameId::parse("my-app32");
CAMEO_ASSERT_EQUAL(-1, nameId.getId());
CAMEO_ASSERT_TRUE(!nameId.getId().has_value());
bool error = false;
try {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment