From 663881597bc4f5bba77f425e87ff73be39c1e820 Mon Sep 17 00:00:00 2001
From: Matthew D Jones <matthew.d.jones@tessella.com>
Date: Fri, 30 Oct 2015 09:43:00 +0000
Subject: [PATCH] Re #14178 Fixed logic and added tests

---
 Framework/Algorithms/src/AddSampleLog.cpp    | 42 ++++++++++++++------
 Framework/Algorithms/test/AddSampleLogTest.h | 30 +++++++++++++-
 2 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/Framework/Algorithms/src/AddSampleLog.cpp b/Framework/Algorithms/src/AddSampleLog.cpp
index a2176cbb319..4e78503a038 100644
--- a/Framework/Algorithms/src/AddSampleLog.cpp
+++ b/Framework/Algorithms/src/AddSampleLog.cpp
@@ -41,9 +41,11 @@ void AddSampleLog::init() {
   std::vector<std::string> typeOptions;
   typeOptions.push_back("Int");
   typeOptions.push_back("Double");
-  declareProperty("NumberType", "String",
+  typeOptions.push_back("");
+  declareProperty("NumberType", "",
                   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() {
@@ -60,8 +62,10 @@ void AddSampleLog::exec() {
   std::string propType = getPropertyValue("LogType");
   std::string propNumberType = getPropertyValue("NumberType");
 
-  if (!propNumberType.empty() && (propType != "Number")) {
-    throw std::invalid_argument("You may only use NumberType property if LogType is \"Number\"");
+  if (!propNumberType.empty() &&
+      ((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
@@ -75,18 +79,32 @@ void AddSampleLog::exec() {
     return;
   }
 
-  bool valueIsInt(false);
   int intVal;
   double dblVal;
-  if (Strings::convert(propValue, intVal)) {
-    valueIsInt = true;
-  } else if (!Strings::convert(propValue, dblVal)) {
-    throw std::invalid_argument("Error interpreting string '" + propValue +
-                                "' as a number.");
+  bool value_is_int = false;
+
+  if (!propNumberType.empty()) {
+    value_is_int = (propNumberType == "Int");
+    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 (valueIsInt)
+    if (value_is_int)
       theRun.addLogData(new PropertyWithValue<int>(propName, intVal));
     else
       theRun.addLogData(new PropertyWithValue<double>(propName, dblVal));
@@ -98,7 +116,7 @@ void AddSampleLog::exec() {
       // Swallow the error - startTime will just be 0
     }
 
-    if (valueIsInt) {
+    if (value_is_int) {
       auto tsp = new TimeSeriesProperty<int>(propName);
       tsp->addValue(startTime, intVal);
       theRun.addLogData(tsp);
diff --git a/Framework/Algorithms/test/AddSampleLogTest.h b/Framework/Algorithms/test/AddSampleLogTest.h
index 8342b030b28..32b74538f00 100644
--- a/Framework/Algorithms/test/AddSampleLogTest.h
+++ b/Framework/Algorithms/test/AddSampleLogTest.h
@@ -84,11 +84,32 @@ public:
                      "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>
   void ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string LogName,
                         std::string LogType, std::string LogText,
                         T expectedValue, bool fails = false,
-                        std::string LogUnit = "") {
+                        std::string LogUnit = "", std::string NumberType = "",
+                        bool throws = false) {
     // add the workspace to the ADS
     AnalysisDataService::Instance().addOrReplace("AddSampleLogTest_Temporary",
                                                  testWS);
@@ -96,6 +117,8 @@ public:
     // execute algorithm
     AddSampleLog alg;
     TS_ASSERT_THROWS_NOTHING(alg.initialize());
+    if (throws)
+      alg.setRethrows(true);
     TS_ASSERT(alg.isInitialized())
 
     alg.setPropertyValue("Workspace", "AddSampleLogTest_Temporary");
@@ -103,6 +126,11 @@ public:
     alg.setPropertyValue("LogText", LogText);
     alg.setPropertyValue("LogUnit", LogUnit);
     alg.setPropertyValue("LogType", LogType);
+    alg.setPropertyValue("NumberType", NumberType);
+    if (throws) {
+      TS_ASSERT_THROWS_ANYTHING(alg.execute())
+      return;
+    }
     TS_ASSERT_THROWS_NOTHING(alg.execute())
     if (fails) {
       TS_ASSERT(!alg.isExecuted())
-- 
GitLab