Skip to content

Commit

Permalink
Fix CommandResponse constructor, add tests
Browse files Browse the repository at this point in the history
There is a problem with object creation from proper url_like strings.
  • Loading branch information
not7cd committed Mar 29, 2019
1 parent 3f1dc64 commit 6050ac6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
40 changes: 20 additions & 20 deletions indi-starbook/starbook_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,30 @@ std::istream &starbook::operator>>(std::istream &is, starbook::DateTime &utc) {

starbook::CommandResponse::CommandResponse(const std::string &url_like) : status{OK}, raw{url_like} {
if (url_like.empty()) throw runtime_error("parsing error, no payload");
if (url_like.rfind("OK", 0) == 0 || url_like.rfind("ERROR", 0) == 0) {
if (url_like == "OK")
status = OK;
else if (url_like == "ERROR:FORMAT")
if (url_like.rfind("OK", 0) == 0) {
status = OK;
} else if (url_like.rfind("ERROR", 0) == 0) {
if (url_like.rfind("ERROR:FORMAT", 0) == 0)
status = ERROR_FORMAT;
else if (url_like == "ERROR:ILLEGAL STATE")
else if (url_like.rfind("ERROR:ILLEGAL STATE", 0) == 0)
status = ERROR_ILLEGAL_STATE;
else if (url_like == "ERROR:BELOW HORIZONE") /* it's not a typo */
else if (url_like.rfind("ERROR:BELOW HORIZONE", 0) == 0) /* it's not a typo */
status = ERROR_BELOW_HORIZON;
status = ERROR_UNKNOWN;
return; /* commands with status codes don't contain payload */
}

std::string str_remaining = url_like;
std::regex param_re(R"((\w+)=(\-?[\w\+\.]+))");
std::smatch sm;
else
status = ERROR_UNKNOWN;
} else {
std::string str_remaining = url_like;
std::regex param_re(R"((\w+)=(\-?[\w\+\.]+))");
std::smatch sm;

while (regex_search(str_remaining, sm, param_re)) {
std::string key = sm[1].str();
std::string value = sm[2].str();
while (regex_search(str_remaining, sm, param_re)) {
std::string key = sm[1].str();
std::string value = sm[2].str();

payload[key] = value;
str_remaining = sm.suffix();
payload[key] = value;
str_remaining = sm.suffix();
}
if (!str_remaining.empty()) throw std::runtime_error("parsing error, couldn't parse full payload");
status = OK;
}
if (!str_remaining.empty()) throw std::runtime_error("parsing error, couldn't parse full response");

}
16 changes: 16 additions & 0 deletions indi-starbook/test_starbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
#include <gtest/gtest.h>
#include "starbook_types.h"

TEST(StarbookDriver, cmd_res) {
starbook::CommandResponse res1("OK");
ASSERT_EQ(res1.status, starbook::OK);
std::cerr << res1.status << " " << res1.raw;

starbook::CommandResponse res2("ERROR");
ASSERT_EQ(res2.status, starbook::ERROR_UNKNOWN);

starbook::CommandResponse res3("OK ");
ASSERT_EQ(res3.status, starbook::OK);
std::cerr << res3.status << " " << res3.raw;

starbook::CommandResponse res4(" OK");
ASSERT_EQ(res4.status, starbook::OK);
std::cerr << res4.status << " " << res4.raw;
}

TEST(StarbookDriver, time) {
std::ostringstream result;
Expand Down

0 comments on commit 6050ac6

Please sign in to comment.