-
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 MandatoryValidator to Python. Refs #4399
In doing so the isEmpty check has been moved to be easier to extend for additional types.
- Loading branch information
1 parent
5411139
commit 5846a2a
Showing
9 changed files
with
156 additions
and
75 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
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
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
30 changes: 30 additions & 0 deletions
30
Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/MandatoryValidator.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,30 @@ | ||
#include "MantidKernel/MandatoryValidator.h" | ||
#include <boost/python/class.hpp> | ||
#include <string> | ||
|
||
using Mantid::Kernel::MandatoryValidator; | ||
using Mantid::Kernel::IValidator; | ||
using namespace boost::python; | ||
|
||
namespace | ||
{ | ||
/// A macro for generating exports for each type | ||
#define EXPORT_MANDATORYVALIDATOR(ElementType, prefix) \ | ||
class_<MandatoryValidator<ElementType>, bases<IValidator>, \ | ||
boost::noncopyable>(#prefix"MandatoryValidator") \ | ||
; | ||
} | ||
|
||
void export_MandatoryValidator() | ||
{ | ||
EXPORT_MANDATORYVALIDATOR(double, Float); | ||
EXPORT_MANDATORYVALIDATOR(long, Int); | ||
EXPORT_MANDATORYVALIDATOR(std::string, String); | ||
|
||
// Array types | ||
EXPORT_MANDATORYVALIDATOR(std::vector<double>, FloatArray); | ||
EXPORT_MANDATORYVALIDATOR(std::vector<long>, IntArray); | ||
EXPORT_MANDATORYVALIDATOR(std::vector<std::string>, StringArray); | ||
} | ||
|
||
|
31 changes: 31 additions & 0 deletions
31
Code/Mantid/Framework/PythonInterface/test/python/MandatoryValidatorTest.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,31 @@ | ||
import unittest | ||
import testhelpers | ||
from mantid import (FloatArrayProperty, StringMandatoryValidator, FloatArrayMandatoryValidator, | ||
PythonAlgorithm) | ||
|
||
class MandatoryValidatorTest(unittest.TestCase): | ||
|
||
def test_constructor_does_not_raise_error(self): | ||
testhelpers.assertRaisesNothing(self, StringMandatoryValidator) | ||
|
||
def test_validator_restricts_property_values_to_non_empty(self): | ||
class TestAlgorithm(PythonAlgorithm): | ||
|
||
def PyInit(self): | ||
self.declareProperty("StringInput", "", StringMandatoryValidator()) | ||
self.declareProperty(FloatArrayProperty("ArrayInput", FloatArrayMandatoryValidator())) | ||
|
||
def PyExec(self): | ||
pass | ||
|
||
alg = TestAlgorithm() | ||
alg.initialize() | ||
|
||
self.assertRaises(ValueError, alg.setProperty, "StringInput", "") | ||
testhelpers.assertRaisesNothing(self, alg.setProperty, "StringInput", "value") | ||
|
||
self.assertRaises(ValueError, alg.setProperty, "ArrayInput", []) | ||
testhelpers.assertRaisesNothing(self, alg.setProperty, "ArrayInput", [1.2,3.4]) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |