Skip to content

Commit

Permalink
Create a DateTimeValidator + tests.
Browse files Browse the repository at this point in the history
Refs #9227
  • Loading branch information
martyngigg committed Mar 31, 2014
1 parent 4844da5 commit 9d51c79
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Expand Up @@ -12,6 +12,7 @@ set ( SRC_FILES
src/DataItem.cpp
src/DateAndTime.cpp
src/DateValidator.cpp
src/DateTimeValidator.cpp
src/DeltaEMode.cpp
src/DirectoryValidator.cpp
src/DiskBuffer.cpp
Expand Down Expand Up @@ -127,6 +128,7 @@ set ( INC_FILES
inc/MantidKernel/DataService.h
inc/MantidKernel/DateAndTime.h
inc/MantidKernel/DateValidator.h
inc/MantidKernel/DateTimeValidator.h
inc/MantidKernel/DeltaEMode.h
inc/MantidKernel/DirectoryValidator.h
inc/MantidKernel/DiskBuffer.h
Expand Down Expand Up @@ -252,6 +254,7 @@ set ( TEST_FILES
ConfigServiceTest.h
DataServiceTest.h
DateAndTimeTest.h
DateTimeValidatorTest.h
DateValidatorTest.h
DeltaEModeTest.h
DirectoryValidatorTest.h
Expand Down
47 changes: 47 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/DateTimeValidator.h
@@ -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 */
45 changes: 45 additions & 0 deletions Code/Mantid/Framework/Kernel/src/DateTimeValidator.cpp
@@ -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;
}

}
}

69 changes: 69 additions & 0 deletions Code/Mantid/Framework/Kernel/test/DateTimeValidatorTest.h
@@ -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_ */

0 comments on commit 9d51c79

Please sign in to comment.