Skip to content

Commit

Permalink
Export CompositeValidator. Refs #4399
Browse files Browse the repository at this point in the history
Documentation had revealed it was missing.
  • Loading branch information
martyngigg committed Apr 25, 2012
1 parent e8ad872 commit 5caab86
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Code/Mantid/Build/python_export_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_frameworkdir(headerfile):
Returns the Framework directory
"""
if 'Framework' in headerfile:
matches = re.match(r"(.*Framework/).*\.h", headerfile)
matches = re.match(r"(.*Framework(/|\\)).*\.h", headerfile)
if matches:
frameworkdir = matches.group(1)
else:
Expand All @@ -71,10 +71,12 @@ def get_include(headerfile):
"""
matches = re.match(r".*inc(/|\\)(Mantid[a-z,A-z]*(/|\\).*\.h)", headerfile)
if matches:
return matches.group(2)
includefile = matches.group(2)
else:
raise RuntimeError("Unable to determine include path from given header")

# Make sure the include only has forward slases
includefile = includefile.replace("\\", "/")
return includefile
def get_modulepath(frameworkdir, submodule):
"""Creates a path to the requested submodule
"""
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set ( TEST_PY_FILES
test/python/ArrayLengthValidatorTest.py
test/python/AxisTest.py
test/python/BoundedValidatorTest.py
test/python/CompositeValidatorTest.py
test/python/ConfigServiceTest.py
test/python/CreateWorkspaceTest.py
test/python/ExperimentInfoTest.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set ( EXPORT_FILES
src/Exports/ArrayLengthValidator.cpp
src/Exports/ArrayBoundedValidator.cpp
src/Exports/MandatoryValidator.cpp
src/Exports/CompositeValidator.cpp
)

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

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

0 comments on commit 5caab86

Please sign in to comment.