Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Code/Mantid/Framework/Kernel/inc/MantidKernel/DateTimeValidator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef MANTID_KERNEL_DATETIMEVALIDATOR_H_ | ||
#define MANTID_KERNEL_DATETIMEVALIDATOR_H_ | ||
|
||
#include "MantidKernel/TypedValidator.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace Kernel | ||
{ | ||
/** | ||
Checks that a string contains a timestamp in ISO 8601 format (YYYY-MM-DDTHH:MM:SS.mmmmmm) | ||
Copyright © 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://github.com/mantidproject/mantid>. | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class MANTID_KERNEL_DLL DateTimeValidator : public TypedValidator<std::string> | ||
{ | ||
public: | ||
/// Clone the current state | ||
IValidator_sptr clone() const; | ||
|
||
private: | ||
/// Checks the value is valid | ||
std::string checkValidity(const std::string& value) const; | ||
}; | ||
|
||
} | ||
} | ||
|
||
#endif /** DATETIMEVALIDATOR */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidKernel/DateTimeValidator.h" | ||
#include "MantidKernel/DateAndTime.h" | ||
|
||
|
||
namespace Mantid | ||
{ | ||
namespace Kernel | ||
{ | ||
|
||
/** | ||
* @return A clone of the current state of the validator | ||
*/ | ||
IValidator_sptr DateTimeValidator::clone() const | ||
{ | ||
return boost::make_shared<DateTimeValidator>(*this); | ||
} | ||
|
||
|
||
/** | ||
* @param value A string to check for an ISO formatted timestamp | ||
* @return An empty string if the value is valid or an string containing | ||
* a description of the error otherwise | ||
*/ | ||
std::string DateTimeValidator::checkValidity(const std::string& value) const | ||
{ | ||
// simply pass off the work DateAndTime constructor | ||
std::string error(""); | ||
try | ||
{ | ||
DateAndTime timestamp(value); | ||
UNUSED_ARG(timestamp); | ||
} | ||
catch(std::invalid_argument& exc) | ||
{ | ||
error = exc.what(); | ||
} | ||
return error; | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#ifndef MANTID_KERNEL_DATETIMEVALIDATORTEST_H_ | ||
#define MANTID_KERNEL_DATETIMEVALIDATORTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidKernel/DateTimeValidator.h" | ||
|
||
using Mantid::Kernel::DateTimeValidator; | ||
|
||
class DateTimeValidatorTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
// This pair of boilerplate methods prevent the suite being created statically | ||
// This means the constructor isn't called when running other tests | ||
static DateTimeValidatorTest *createSuite() { return new DateTimeValidatorTest(); } | ||
static void destroySuite( DateTimeValidatorTest *suite ) { delete suite; } | ||
|
||
//---------------------------- Success cases -------------------------------------- | ||
|
||
void test_date_alone_is_valid() | ||
{ | ||
DateTimeValidator validator; | ||
const std::string input = "2014-03-21"; | ||
TS_ASSERT_EQUALS("", validator.isValid(input)); | ||
} | ||
|
||
void test_full_extended_iso_format_is_accepted() | ||
{ | ||
DateTimeValidator validator; | ||
const std::string input = "2014-03-21T00:01:30.52"; | ||
TS_ASSERT_EQUALS("", validator.isValid(input)); | ||
} | ||
|
||
void test_time_without_fractional_seconds_in_iso_format_is_accepted() | ||
{ | ||
DateTimeValidator validator; | ||
const std::string input = "2014-03-21T00:01:30"; | ||
TS_ASSERT_EQUALS("", validator.isValid(input)); | ||
} | ||
|
||
|
||
//---------------------------- Failure cases -------------------------------------- | ||
|
||
void test_empty_string_is_invalid() | ||
{ | ||
DateTimeValidator validator; | ||
TS_ASSERT_EQUALS("Error interpreting string '' as a date/time.", validator.isValid("")); | ||
} | ||
|
||
void test_text_string_is_invalid() | ||
{ | ||
DateTimeValidator validator; | ||
const std::string input = "not a timestamp"; | ||
const std::string error = "Error interpreting string '" + input + "' as a date/time."; | ||
TS_ASSERT_EQUALS(error, validator.isValid(input)); | ||
} | ||
|
||
void test_time_alone_is_invalid() | ||
{ | ||
DateTimeValidator validator; | ||
const std::string input = "09:03:30"; | ||
const std::string error = "Error interpreting string '" + input + "' as a date/time."; | ||
TS_ASSERT_EQUALS(error, validator.isValid(input)); | ||
} | ||
|
||
}; | ||
|
||
|
||
#endif /* MANTID_KERNEL_DATETIMEVALIDATORTEST_H_ */ |