Skip to content

Commit

Permalink
Refs #4577. Cover case where FindRuns was throwing bad lexical cast.
Browse files Browse the repository at this point in the history
Covered case using regex.  Added a couple more assertions to unit test.
  • Loading branch information
PeterParker committed Jan 20, 2012
1 parent b21a11a commit 7dfa9a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Code/Mantid/Framework/API/src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/StringTokenizer.h>
#include <Poco/RegularExpression.h>
#include <boost/lexical_cast.hpp>

#include <cctype>
Expand Down Expand Up @@ -566,6 +567,12 @@ namespace Mantid
int runNumber = boost::lexical_cast<int>(run);
std::string runEnd = run;
runEnd.replace(runEnd.end() - range[1].size(), runEnd.end(), range[1]);

// Throw if runEnd contains something else other than a digit.
Poco::RegularExpression digits("[0-9]+");
if (!digits.match(runEnd))
throw std::invalid_argument("Malformed range of runs: Part of the run has a non-digit character in it.");

int runEndNumber = boost::lexical_cast<int>(runEnd);
if (runEndNumber < runNumber)
{
Expand Down
10 changes: 9 additions & 1 deletion Code/Mantid/Framework/API/test/FileFinderTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cxxtest/TestSuite.h>
#include "MantidAPI/FileFinder.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"

#include <Poco/Path.h>
#include <Poco/File.h>
Expand Down Expand Up @@ -186,9 +187,16 @@ class FileFinderTest: public CxxTest::TestSuite
void testFindFiles()
{
ConfigService::Instance().setString("default.facility","ISIS");
std::vector<std::string> files = FileFinder::Instance().findRuns("MUSR15189-15199");
std::vector<std::string> files;
TS_ASSERT_THROWS(files = FileFinder::Instance().findRuns("MUSR15189-n15199"), std::invalid_argument);
TS_ASSERT_THROWS(files = FileFinder::Instance().findRuns("MUSR15189n-15199"), std::invalid_argument);
TS_ASSERT_THROWS(files = FileFinder::Instance().findRuns("MUSR15189-15199n"), std::invalid_argument);
TS_ASSERT_THROWS(files = FileFinder::Instance().findRuns("MUSR15189-151n99"), std::invalid_argument);
TS_ASSERT_THROWS(files = FileFinder::Instance().findRuns("MUSR15n189-151n99"), Exception::NotFoundError);
TS_ASSERT_THROWS_NOTHING(files = FileFinder::Instance().findRuns("MUSR15189-15199"));
TS_ASSERT_EQUALS(files.size(), 11);
std::vector<std::string>::iterator it = files.begin();

for (; it != files.end(); ++it)
{
if (it != files.begin())
Expand Down

0 comments on commit 7dfa9a0

Please sign in to comment.