-
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 CompositeValidator. Refs #4399
Documentation had revealed it was missing.
- Loading branch information
1 parent
e8ad872
commit 5caab86
Showing
5 changed files
with
105 additions
and
3 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
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
49 changes: 49 additions & 0 deletions
49
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/CompositeValidator.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,49 @@ | ||
#include "MantidKernel/CompositeValidator.h" | ||
#include <boost/python/class.hpp> | ||
#include <boost/python/list.hpp> | ||
#include <boost/python/make_constructor.hpp> | ||
#include <boost/python/default_call_policies.hpp> | ||
|
||
using Mantid::Kernel::CompositeValidator; | ||
using Mantid::Kernel::IValidator; | ||
using Mantid::Kernel::IValidator_sptr; | ||
using namespace boost::python; | ||
|
||
namespace | ||
{ | ||
/** | ||
* Creates a composite validator from a python list of validators | ||
*/ | ||
CompositeValidator * createCompositeValidator(const boost::python::list & validators) | ||
{ | ||
namespace bpl = boost::python; | ||
CompositeValidator *composite = new CompositeValidator; | ||
const bpl::ssize_t nitems = bpl::len(validators); | ||
for(bpl::ssize_t i = 0; i < nitems; ++i) | ||
{ | ||
try | ||
{ | ||
composite->add(bpl::extract<IValidator_sptr>(validators[i])); | ||
} | ||
catch(boost::python::error_already_set &) | ||
{ | ||
std::stringstream os; | ||
os << "Cannot extract Validator from element " << i; | ||
throw std::invalid_argument(os.str()); | ||
} | ||
} | ||
return composite; | ||
} | ||
} | ||
|
||
void export_CompositeValidator() | ||
{ | ||
class_<CompositeValidator, bases<IValidator>, boost::noncopyable>("CompositeValidator") | ||
.def("__init__", make_constructor(&createCompositeValidator, default_call_policies(), | ||
(arg("validators")) | ||
)) | ||
|
||
.def("add", &CompositeValidator::add, "Add another validator to the list") | ||
; | ||
} | ||
|
49 changes: 49 additions & 0 deletions
49
Code/Mantid/Framework/PythonInterface/test/python/CompositeValidatorTest.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,49 @@ | ||
import unittest | ||
from mantid.kernel import CompositeValidator, FloatBoundedValidator | ||
from mantid.api import PythonAlgorithm | ||
|
||
class CompositeValidatorTest(unittest.TestCase): | ||
|
||
def test_creation_with_add_succeeds_correctly_in_algorithm(self): | ||
""" | ||
Tests that a composite validator created with the add | ||
method validates correctly | ||
""" | ||
validation = CompositeValidator() | ||
validation.add(FloatBoundedValidator(lower=5)) | ||
validation.add(FloatBoundedValidator(upper=10)) | ||
self._do_validation_test(validation) | ||
|
||
def test_creation_with_constructor_and_list(self): | ||
""" | ||
Tests that a composite validator created with the constructor method | ||
""" | ||
validation = CompositeValidator([FloatBoundedValidator(lower=5), FloatBoundedValidator(upper=10)]) | ||
self._do_validation_test(validation) | ||
|
||
def _do_validation_test(self, validation): | ||
"""Run the validator tests""" | ||
test_alg = self._create_test_algorithm(validation) | ||
prop = test_alg.getProperty("Input") | ||
self.assertNotEquals(prop.isValid(), "") | ||
test_alg.setProperty("Input", 6.8) | ||
self.assertEquals(prop.isValid(), "") | ||
self.assertRaises(ValueError, test_alg.setProperty, "Input", 15) | ||
|
||
def _create_test_algorithm(self, validator): | ||
"""Create a test algorithm""" | ||
class TestAlgorithm(PythonAlgorithm): | ||
|
||
def PyInit(self): | ||
self.declareProperty("Input", -1.0, validator) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = TestAlgorithm() | ||
alg.initialize() | ||
return alg | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |