-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export ListValidator to python. Refs #4399
- Loading branch information
1 parent
d62796c
commit ae56ffc
Showing
5 changed files
with
176 additions
and
1 deletion.
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
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
46 changes: 46 additions & 0 deletions
46
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ListValidator.cpp
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,46 @@ | ||
#include "MantidKernel/ListValidator.h" | ||
#include "MantidPythonInterface/kernel/Converters/PySequenceToVector.h" | ||
|
||
#include <boost/python/class.hpp> | ||
#include <boost/python/list.hpp> | ||
#include <boost/python/make_constructor.hpp> | ||
#include <boost/python/default_call_policies.hpp> | ||
|
||
#include <string> | ||
|
||
using Mantid::Kernel::ListValidator; | ||
using Mantid::Kernel::IValidator; | ||
namespace Converters = Mantid::PythonInterface::Converters; | ||
using namespace boost::python; | ||
|
||
namespace | ||
{ | ||
|
||
/** | ||
* Factory function to allow the allowed values to be specified as a python list | ||
* @param allowedValues :: The list of allowed values | ||
* @return A new ListValidator instance | ||
*/ | ||
template<typename T> | ||
ListValidator<T> * createListValidator(const boost::python::list & allowedValues) | ||
{ | ||
return new ListValidator<T>(Converters::PySequenceToVector<T>(allowedValues)()); | ||
} | ||
|
||
|
||
#define EXPORT_LISTVALIDATOR(type, prefix) \ | ||
class_<ListValidator<type>, bases<IValidator>, \ | ||
boost::noncopyable>(#prefix"ListValidator") \ | ||
.def("__init__", make_constructor(&createListValidator<type>, default_call_policies(), \ | ||
arg("allowedValues"))) \ | ||
.def("addAllowedValue", &ListValidator<type>::addAllowedValue, \ | ||
"Adds a value to the list of accepted values") \ | ||
; | ||
|
||
} | ||
|
||
void export_ListValidator() | ||
{ | ||
EXPORT_LISTVALIDATOR(std::string, String); | ||
} | ||
|
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
76 changes: 76 additions & 0 deletions
76
Code/Mantid/Framework/PythonInterface/test/python/ListValidatorTest.py
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,76 @@ | ||
import unittest | ||
import testhelpers | ||
|
||
from mantid import (StringListValidator, Direction, PythonAlgorithm) | ||
|
||
|
||
class ListValidatorTest(unittest.TestCase): | ||
|
||
def test_empty_ListValidator_allows_nothing(self): | ||
""" | ||
Test that a list validator restricts the values | ||
for a property | ||
""" | ||
|
||
class EmptyListValidator(PythonAlgorithm): | ||
|
||
def PyInit(self): | ||
validator = StringListValidator() | ||
self.declareProperty("Input", "", validator) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = EmptyListValidator() | ||
alg.initialize() | ||
self.assertRaises(ValueError, alg.setProperty, "Input", "AnyOldString") | ||
|
||
def test_ListValidator_plus_addAllowedValued_allows_that_value(self): | ||
""" | ||
Test that a list validator restricts the values | ||
for a property | ||
""" | ||
|
||
class SingleItemListValidator(PythonAlgorithm): | ||
|
||
_allowed = "OnlyThis" | ||
|
||
def PyInit(self): | ||
validator = StringListValidator() | ||
validator.addAllowedValue(self._allowed) | ||
self.declareProperty("Input", "", validator) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = SingleItemListValidator() | ||
alg.initialize() | ||
self.assertRaises(ValueError, alg.setProperty, "Input", "NotValid") | ||
testhelpers.assertRaisesNothing(self, alg.setProperty, "Input", alg._allowed) | ||
|
||
def test_ListValidator_with_values_in_constructor_restricts_property_values(self): | ||
""" | ||
Test that a list validator restricts the values | ||
for a property | ||
""" | ||
|
||
class MultiValueValidator(PythonAlgorithm): | ||
|
||
_allowed_vals = ["Val1", "Val2","Val3"] | ||
|
||
def PyInit(self): | ||
validator = StringListValidator(self._allowed_vals) | ||
self.declareProperty("Input", "", validator) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = MultiValueValidator() | ||
alg.initialize() | ||
self.assertRaises(ValueError, alg.setProperty, "Input", "NotValid") | ||
for val in alg._allowed_vals: | ||
testhelpers.assertRaisesNothing(self, alg.setProperty, "Input", val) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |