Integration tests for the command line (#26)

* WIP

* first test

* format

* test frames

* also 0:
This commit is contained in:
Erik Fröjdh
2019-05-31 14:40:45 +02:00
committed by Dhanya Thattil
parent 0eec57f8a2
commit 4b1a9bea32
4 changed files with 67 additions and 22 deletions

View File

@ -5,10 +5,10 @@
#include "CmdLineParser.h"
#include "CmdProxy.h"
#include "container_utils.h"
#include "string_utils.h"
#include "multiSlsDetector.h"
#include "slsDetectorCommand.h"
#include "sls_detector_exceptions.h"
#include "string_utils.h"
#include <cstdlib>
#include <memory>
@ -23,14 +23,17 @@ inline int dummyCallback(detectorData *d, int p, void *) {
class multiSlsDetectorClient {
public:
multiSlsDetectorClient(int argc, char *argv[], int action,
multiSlsDetector *myDetector = nullptr)
: action_(action), detPtr(myDetector) {
multiSlsDetector *myDetector = nullptr,
std::ostream &output = std::cout)
: action_(action), detPtr(myDetector), os(output) {
parser.Parse(argc, argv);
runCommand();
}
multiSlsDetectorClient(const std::string &args, int action,
multiSlsDetector *myDetector = nullptr)
: action_(action), detPtr(myDetector) {
multiSlsDetector *myDetector = nullptr,
std::ostream &output = std::cout)
: action_(action), detPtr(myDetector), os(output) {
parser.Parse(args);
runCommand();
}
@ -39,30 +42,31 @@ class multiSlsDetectorClient {
int action_;
CmdLineParser parser;
multiSlsDetector *detPtr = nullptr;
std::ostream &os;
void runCommand() {
bool verify = true;
bool update = true;
if (action_ == slsDetectorDefs::PUT_ACTION &&
parser.n_arguments() == 0) {
std::cout << "Wrong usage - should be: " << parser.executable()
<< "[id-][pos:]channel arg" << std::endl;
std::cout << std::endl;
os << "Wrong usage - should be: " << parser.executable()
<< "[id-][pos:]channel arg" << std::endl;
os << std::endl;
return;
};
if (action_ == slsDetectorDefs::GET_ACTION &&
parser.command().empty()) {
std::cout << "Wrong usage - should be: " << parser.executable()
<< "[id-][pos:]channel arg" << std::endl;
std::cout << std::endl;
os << "Wrong usage - should be: " << parser.executable()
<< "[id-][pos:]channel arg" << std::endl;
os << std::endl;
return;
};
if (action_ == slsDetectorDefs::READOUT_ACTION &&
parser.detector_id() != -1) {
std::cout << "detector_id: " << parser.detector_id()
<< " ,readout of individual detectors is not allowed!"
<< std::endl;
os << "detector_id: " << parser.detector_id()
<< " ,readout of individual detectors is not allowed!"
<< std::endl;
return;
}
@ -78,7 +82,6 @@ class multiSlsDetectorClient {
update = false;
}
// std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl;
// create multiSlsDetector class if required
std::unique_ptr<multiSlsDetector> localDet;
if (detPtr == nullptr) {
@ -87,15 +90,15 @@ class multiSlsDetectorClient {
verify, update);
detPtr = localDet.get();
} catch (const RuntimeError &e) {
/*std::cout << e.GetMessage() << std::endl;*/
/*os << e.GetMessage() << std::endl;*/
return;
} catch (...) {
std::cout << " caught exception\n";
os << " caught exception\n";
return;
}
}
if (parser.detector_id() >= detPtr->getNumberOfDetectors()) {
std::cout << "position is out of bounds.\n";
os << "position is out of bounds.\n";
return;
}
@ -120,13 +123,13 @@ class multiSlsDetectorClient {
action_, parser.detector_id());
if (parser.multi_id() != 0)
std::cout << parser.multi_id() << '-';
os << parser.multi_id() << '-';
if (parser.detector_id() != -1)
std::cout << parser.detector_id() << ':';
os << parser.detector_id() << ':';
if (action_ != slsDetectorDefs::READOUT_ACTION) {
std::cout << parser.command() << " ";
os << parser.command() << " ";
}
std::cout << answer << std::endl;
os << answer << std::endl;
}
};

View File

@ -26,5 +26,6 @@ int main(int argc, char *argv[]) {
#ifdef HELP
int action = slsDetectorDefs::HELP_ACTION;
#endif
multiSlsDetectorClient(argc, argv, action);
}

View File

@ -2,4 +2,5 @@ target_sources(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/test-SharedMemory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-slsDetector.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-multiSlsDetector.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-multiSlsDetectorClient.cpp
)

View File

@ -0,0 +1,40 @@
#include "catch.hpp"
#include "multiSlsDetectorClient.h"
#include "sls_detector_defs.h"
#include <sstream>
auto GET = slsDetectorDefs::GET_ACTION;
auto PUT = slsDetectorDefs::PUT_ACTION;
TEST_CASE("rx_fifodepth", "[.cmd]") {
auto oss = std::ostringstream{};
multiSlsDetectorClient("rx_fifodepth 10", PUT, nullptr, oss);
REQUIRE(oss.str() == "rx_fifodepth 10\n");
oss = std::ostringstream{};
multiSlsDetectorClient("rx_fifodepth 100", PUT, nullptr, oss);
REQUIRE(oss.str() == "rx_fifodepth 100\n");
oss = std::ostringstream{};
multiSlsDetectorClient("rx_fifodepth", GET, nullptr, oss);
REQUIRE(oss.str() == "rx_fifodepth 100\n");
oss = std::ostringstream{};
multiSlsDetectorClient("0:rx_fifodepth", GET, nullptr, oss);
REQUIRE(oss.str() == "0:rx_fifodepth 100\n");
}
TEST_CASE("frames", "[.cmd]"){
auto oss = std::ostringstream{};
multiSlsDetectorClient("frames 1000", PUT, nullptr, oss);
REQUIRE(oss.str() == "frames 1000\n");
oss = std::ostringstream{};
multiSlsDetectorClient("frames", GET, nullptr, oss);
REQUIRE(oss.str() == "frames 1000\n");
oss = std::ostringstream{};
multiSlsDetectorClient("frames 1", PUT, nullptr, oss);
REQUIRE(oss.str() == "frames 1\n");
}