Skip to content

Commit

Permalink
Export ArrayLengthValidator to Python. Refs #4399
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Apr 16, 2012
1 parent ae56ffc commit c2e6785
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set ( TEST_PY_FILES
test/python/AlgorithmManagerTest.py
test/python/AnalysisDataServiceTest.py
test/python/ArrayPropertyTest.py
test/python/ArrayLengthValidatorTest.py
test/python/AxisTest.py
test/python/BoundedValidatorTest.py
test/python/ConfigServiceTest.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set ( EXPORT_FILES
src/Exports/FacilityInfo.cpp
src/Exports/NullValidator.cpp
src/Exports/ListValidator.cpp
src/Exports/ArrayLengthValidator.cpp
)

set ( SRC_FILES
Expand Down
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);
}

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()

0 comments on commit c2e6785

Please sign in to comment.