-
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 ArrayLengthValidator to Python. Refs #4399
- Loading branch information
1 parent
ae56ffc
commit c2e6785
Showing
4 changed files
with
95 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
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
27 changes: 27 additions & 0 deletions
27
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ArrayLengthValidator.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,27 @@ | ||
#include "MantidKernel/ArrayLengthValidator.h" | ||
#include <boost/python/class.hpp> | ||
|
||
using Mantid::Kernel::ArrayLengthValidator; | ||
using Mantid::Kernel::IValidator; | ||
using namespace boost::python; | ||
|
||
namespace | ||
{ | ||
|
||
#define EXPORT_LENGTHVALIDATOR(type, prefix) \ | ||
class_<ArrayLengthValidator<type>, bases<IValidator>, \ | ||
boost::noncopyable \ | ||
>(#prefix"ArrayLengthValidator") \ | ||
.def(init<int>(arg("length"), "Constructs a validator verifying that an array is of the exact length given"))\ | ||
.def(init<int,int>((arg("lenmin"),arg("lenmax")), "Constructs a validator verifying that the length of an array is within the range given"))\ | ||
; | ||
|
||
} | ||
|
||
void export_ArrayLengthValidator() | ||
{ | ||
EXPORT_LENGTHVALIDATOR(double, Float); | ||
EXPORT_LENGTHVALIDATOR(int, Int); | ||
EXPORT_LENGTHVALIDATOR(std::string, String); | ||
} | ||
|
66 changes: 66 additions & 0 deletions
66
Code/Mantid/Framework/PythonInterface/test/python/ArrayLengthValidatorTest.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,66 @@ | ||
import unittest | ||
import testhelpers | ||
|
||
from mantid import FloatArrayLengthValidator, PythonAlgorithm, FloatArrayProperty | ||
|
||
class ArrayLengthValidatorTest(unittest.TestCase): | ||
|
||
def test_Validator_on_ArrayProperty_accepts_array_of_specified_length(self): | ||
fixedlength = 6 | ||
alg = self._create_alg_with_fixedlength_validator(fixedlength) | ||
input_vals = [1.,2.4,5.6,8.0,4.6,6.] | ||
testhelpers.assertRaisesNothing(self, alg.setProperty, "Input", input_vals) | ||
|
||
def test_Validator_on_ArrayProperty_rejects_array_of_without_correct_length(self): | ||
fixedlength = 6 | ||
alg = self._create_alg_with_fixedlength_validator(fixedlength) | ||
input_vals = [1.,2.4,5.6] | ||
self.assertRaises(ValueError, alg.setProperty, "Input", input_vals) | ||
|
||
def test_Validator_on_ArrayProperty_accepts_array_with_length_in_range(self): | ||
alg = self._create_alg_with_range_validator(3,5) | ||
input_vals = [] | ||
for i in range(1,7): | ||
input_vals.append(float(1)) | ||
if i < 3 or i > 5: | ||
self.assertRaises(ValueError, alg.setProperty, "Input", input_vals) | ||
else: | ||
testhelpers.assertRaisesNothing(self, alg.setProperty, "Input", input_vals) | ||
|
||
def _create_alg_with_fixedlength_validator(self, fixedlength): | ||
""" | ||
Creates a test algorithm with a fixed length validator | ||
""" | ||
class TestAlgorithm(PythonAlgorithm): | ||
|
||
def PyInit(self): | ||
validator = FloatArrayLengthValidator(fixedlength) | ||
self.declareProperty(FloatArrayProperty("Input", validator)) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = TestAlgorithm() | ||
alg.initialize() | ||
return alg | ||
|
||
def _create_alg_with_range_validator(self, minlength, maxlength): | ||
""" | ||
Creates a test algorithm with a range length validator | ||
""" | ||
class TestAlgorithm(PythonAlgorithm): | ||
|
||
def PyInit(self): | ||
validator = FloatArrayLengthValidator(minlength, maxlength) | ||
self.declareProperty(FloatArrayProperty("Input", validator)) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = TestAlgorithm() | ||
alg.initialize() | ||
return alg | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |