Skip to content
Snippets Groups Projects
Commit 66388159 authored by Matthew D Jones's avatar Matthew D Jones
Browse files

Re #14178 Fixed logic and added tests

parent 15c7a2f2
No related branches found
No related tags found
No related merge requests found
...@@ -41,9 +41,11 @@ void AddSampleLog::init() { ...@@ -41,9 +41,11 @@ void AddSampleLog::init() {
std::vector<std::string> typeOptions; std::vector<std::string> typeOptions;
typeOptions.push_back("Int"); typeOptions.push_back("Int");
typeOptions.push_back("Double"); typeOptions.push_back("Double");
declareProperty("NumberType", "String", typeOptions.push_back("");
declareProperty("NumberType", "",
boost::make_shared<StringListValidator>(typeOptions), boost::make_shared<StringListValidator>(typeOptions),
"Force LogText to be interpreted as a number of type \"int\" or \"double\"."); "Force LogText to be interpreted as a number of type \"int\" "
"or \"double\".");
} }
void AddSampleLog::exec() { void AddSampleLog::exec() {
...@@ -60,8 +62,10 @@ void AddSampleLog::exec() { ...@@ -60,8 +62,10 @@ void AddSampleLog::exec() {
std::string propType = getPropertyValue("LogType"); std::string propType = getPropertyValue("LogType");
std::string propNumberType = getPropertyValue("NumberType"); std::string propNumberType = getPropertyValue("NumberType");
if (!propNumberType.empty() && (propType != "Number")) { if (!propNumberType.empty() &&
throw std::invalid_argument("You may only use NumberType property if LogType is \"Number\""); ((propType != "Number") && (propType != "Number Series"))) {
throw std::invalid_argument("You may only use NumberType property if "
"LogType is 'Number' or 'Number Series'");
} }
// Remove any existing log // Remove any existing log
...@@ -75,18 +79,32 @@ void AddSampleLog::exec() { ...@@ -75,18 +79,32 @@ void AddSampleLog::exec() {
return; return;
} }
bool valueIsInt(false);
int intVal; int intVal;
double dblVal; double dblVal;
if (Strings::convert(propValue, intVal)) { bool value_is_int = false;
valueIsInt = true;
} else if (!Strings::convert(propValue, dblVal)) { if (!propNumberType.empty()) {
throw std::invalid_argument("Error interpreting string '" + propValue + value_is_int = (propNumberType == "Int");
"' as a number."); if (value_is_int) {
if (!Strings::convert(propValue, intVal)) {
throw std::invalid_argument("Error interpreting string '" + propValue +
"' as NumberType Int.");
}
} else if (!Strings::convert(propValue, dblVal)) {
throw std::invalid_argument("Error interpreting string '" + propValue +
"' as NumberType Double.");
}
} else {
if (Strings::convert(propValue, intVal)) {
value_is_int = true;
} else if (!Strings::convert(propValue, dblVal)) {
throw std::invalid_argument("Error interpreting string '" + propValue +
"' as a number.");
}
} }
if (propType == "Number") { if (propType == "Number") {
if (valueIsInt) if (value_is_int)
theRun.addLogData(new PropertyWithValue<int>(propName, intVal)); theRun.addLogData(new PropertyWithValue<int>(propName, intVal));
else else
theRun.addLogData(new PropertyWithValue<double>(propName, dblVal)); theRun.addLogData(new PropertyWithValue<double>(propName, dblVal));
...@@ -98,7 +116,7 @@ void AddSampleLog::exec() { ...@@ -98,7 +116,7 @@ void AddSampleLog::exec() {
// Swallow the error - startTime will just be 0 // Swallow the error - startTime will just be 0
} }
if (valueIsInt) { if (value_is_int) {
auto tsp = new TimeSeriesProperty<int>(propName); auto tsp = new TimeSeriesProperty<int>(propName);
tsp->addValue(startTime, intVal); tsp->addValue(startTime, intVal);
theRun.addLogData(tsp); theRun.addLogData(tsp);
......
...@@ -84,11 +84,32 @@ public: ...@@ -84,11 +84,32 @@ public:
"stringUnit"); "stringUnit");
} }
void test_number_type() {
MatrixWorkspace_sptr ws =
WorkspaceCreationHelper::Create2DWorkspace(10, 10);
ws->mutableRun().setStartAndEndTime(DateAndTime("2013-12-18T13:40:00"),
DateAndTime("2013-12-18T13:42:00"));
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234, false,
"myUnit", "Double");
ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963, false,
"differentUnit", "Int");
// Can force '963' to be interpreted as a double
ExecuteAlgorithm(ws, "My New Name", "Number", "963", 963.0, false,
"differentUnit", "Double");
// Should throw error as NumberType defined for a String
ExecuteAlgorithm(ws, "My Name", "String", "My Value", 0.0, true,
"stringUnit", "Double", true);
// Should throw error trying to interpret '1.234' as Int
ExecuteAlgorithm(ws, "My Name", "Number Series", "1.234", 1.234, true,
"myUnit", "Int", true);
}
template <typename T> template <typename T>
void ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName, void ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName,
std::string LogType, std::string LogText, std::string LogType, std::string LogText,
T expectedValue, bool fails = false, T expectedValue, bool fails = false,
std::string LogUnit = "") { std::string LogUnit = "", std::string NumberType = "",
bool throws = false) {
// add the workspace to the ADS // add the workspace to the ADS
AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary", AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary",
testWS); testWS);
...@@ -96,6 +117,8 @@ public: ...@@ -96,6 +117,8 @@ public:
// execute algorithm // execute algorithm
AddSampleLog alg; AddSampleLog alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize()); TS_ASSERT_THROWS_NOTHING(alg.initialize());
if (throws)
alg.setRethrows(true);
TS_ASSERT(alg.isInitialized()) TS_ASSERT(alg.isInitialized())
alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary"); alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary");
...@@ -103,6 +126,11 @@ public: ...@@ -103,6 +126,11 @@ public:
alg.setPropertyValue("LogText", LogText); alg.setPropertyValue("LogText", LogText);
alg.setPropertyValue("LogUnit", LogUnit); alg.setPropertyValue("LogUnit", LogUnit);
alg.setPropertyValue("LogType", LogType); alg.setPropertyValue("LogType", LogType);
alg.setPropertyValue("NumberType", NumberType);
if (throws) {
TS_ASSERT_THROWS_ANYTHING(alg.execute())
return;
}
TS_ASSERT_THROWS_NOTHING(alg.execute()) TS_ASSERT_THROWS_NOTHING(alg.execute())
if (fails) { if (fails) {
TS_ASSERT(!alg.isExecuted()) TS_ASSERT(!alg.isExecuted())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment