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