From 3911b7eff447014e5e26ff0c69b1403bb0dd19c6 Mon Sep 17 00:00:00 2001 From: Harry Jeffery <henry.jeffery@stfc.ac.uk> Date: Wed, 10 Jun 2015 14:43:59 +0100 Subject: [PATCH] Refs #11683 Move grouping pattern validation to validateInputs --- .../inc/MantidDataHandling/GroupDetectors2.h | 3 ++ .../DataHandling/src/GroupDetectors2.cpp | 29 +++++++++++++------ .../DataHandling/test/GroupDetectors2Test.h | 5 +++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/GroupDetectors2.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/GroupDetectors2.h index 60204375a48..31d65508540 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/GroupDetectors2.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/GroupDetectors2.h @@ -128,6 +128,9 @@ public: /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "Transforms\\Grouping"; } + /// Validate inputs + virtual std::map<std::string, std::string> validateInputs(); + private: /// provides a function that expands pairs of integers separated with a hyphen /// into a list of all the integers between those values diff --git a/Code/Mantid/Framework/DataHandling/src/GroupDetectors2.cpp b/Code/Mantid/Framework/DataHandling/src/GroupDetectors2.cpp index e2a7fa70743..9d5bde2db69 100644 --- a/Code/Mantid/Framework/DataHandling/src/GroupDetectors2.cpp +++ b/Code/Mantid/Framework/DataHandling/src/GroupDetectors2.cpp @@ -1382,6 +1382,26 @@ void translateRange(const std::string &instructions, } } // anonymous namespace +/** + * Used to validate the inputs for GroupDetectors2 + * + * @returns : A map of the invalid property names to what the problem is. + */ +std::map<std::string, std::string> GroupDetectors2::validateInputs() { + std::map<std::string, std::string> errors; + + const std::string pattern = getPropertyValue("GroupingPattern"); + + boost::regex re( + "^\\s*[0-9]+\\s*$|^(\\s*,*[0-9]+(\\s*(,|:|\\+|\\-)\\s*)*[0-9]*)*$"); + if (!pattern.empty() && !boost::regex_match(pattern, re)) { + errors["GroupingPattern"] = + "GroupingPattern is not well formed: " + pattern; + } + + return errors; +} + /** * Translate the PerformIndexOperations processing instructions into a format * usable by GroupDetectors. @@ -1391,15 +1411,6 @@ void translateRange(const std::string &instructions, */ void GroupDetectors2::translateInstructions(const std::string &instructions, std::stringstream &commands) { - - // first check that the instructions/pattern makes sense - boost::regex re( - "^\\s*[0-9]+\\s*$|^(\\s*,*[0-9]+(\\s*(,|:|\\+|\\-)\\s*)*[0-9]*)*$"); - if (!boost::regex_match(instructions, re)) { - throw std::invalid_argument("GroupingPattern is not well formed: " + - instructions); - } - // vector of groups, each group being a vector of its spectra std::vector<std::vector<int>> outGroups; diff --git a/Code/Mantid/Framework/DataHandling/test/GroupDetectors2Test.h b/Code/Mantid/Framework/DataHandling/test/GroupDetectors2Test.h index 12cfce00a51..652754a430e 100644 --- a/Code/Mantid/Framework/DataHandling/test/GroupDetectors2Test.h +++ b/Code/Mantid/Framework/DataHandling/test/GroupDetectors2Test.h @@ -753,7 +753,10 @@ public: groupAlg.setPropertyValue("InputWorkspace", inputWS); groupAlg.setPropertyValue("OutputWorkspace", outputBase); groupAlg.setPropertyValue("GroupingPattern", "-1, 0"); - TS_ASSERT_THROWS(groupAlg.execute(), std::invalid_argument); + //Check that the GroupingPattern was recognised as invalid + TS_ASSERT(!groupAlg.validateInputs()["GroupingPattern"].empty()); + //And that we're not allowed to run + TS_ASSERT_THROWS(groupAlg.execute(), std::runtime_error); } private: -- GitLab