Skip to content
Snippets Groups Projects
Unverified Commit d305d31f authored by Stefano Borini's avatar Stefano Borini Committed by GitHub
Browse files

Merge pull request #164 from force-h2020/positive-int-trait

Adds positive int trait
parents 2b03fb7b a05d9e5d
No related branches found
No related tags found
No related merge requests found
...@@ -45,4 +45,4 @@ from .ui_hooks.i_ui_hooks_factory import IUIHooksFactory # noqa ...@@ -45,4 +45,4 @@ from .ui_hooks.i_ui_hooks_factory import IUIHooksFactory # noqa
from .ui_hooks.base_ui_hooks_factory import BaseUIHooksFactory # noqa from .ui_hooks.base_ui_hooks_factory import BaseUIHooksFactory # noqa
from .ui_hooks.base_ui_hooks_manager import BaseUIHooksManager # noqa from .ui_hooks.base_ui_hooks_manager import BaseUIHooksManager # noqa
from .local_traits import Identifier # noqa from .local_traits import Identifier, PositiveInt # noqa
from traits.api import Regex, String from traits.api import Regex, String, BaseInt
#: Used for variable names, but allow also empty string as it's the default #: Used for variable names, but allow also empty string as it's the default
#: case and it will be present if the workflow is saved before actually #: case and it will be present if the workflow is saved before actually
...@@ -8,3 +8,19 @@ Identifier = Regex(regex="(^[^\d\W]\w*\Z|^\Z)") ...@@ -8,3 +8,19 @@ Identifier = Regex(regex="(^[^\d\W]\w*\Z|^\Z)")
#: Identifies a CUBA type with its key. At the moment a String with #: Identifies a CUBA type with its key. At the moment a String with
#: no validation, but will come later. #: no validation, but will come later.
CUBAType = String() CUBAType = String()
class PositiveInt(BaseInt):
"""A positive integer trait."""
info_text = 'a positive integer'
default_value = 1
def validate(self, object, name, value):
int_value = super(PositiveInt, self).validate(object, name, value)
if int_value > 0:
return int_value
self.error(object, name, value)
import unittest import unittest
from traits.api import HasStrictTraits, TraitError from traits.api import HasStrictTraits, TraitError
from force_bdss.local_traits import Identifier, CUBAType from force_bdss.local_traits import Identifier, CUBAType, PositiveInt
class Traited(HasStrictTraits): class Traited(HasStrictTraits):
val = Identifier() val = Identifier()
cuba = CUBAType() cuba = CUBAType()
positive_int = PositiveInt()
class TestLocalTraits(unittest.TestCase): class TestLocalTraits(unittest.TestCase):
...@@ -25,3 +26,13 @@ class TestLocalTraits(unittest.TestCase): ...@@ -25,3 +26,13 @@ class TestLocalTraits(unittest.TestCase):
c = Traited() c = Traited()
c.cuba = "PRESSURE" c.cuba = "PRESSURE"
self.assertEqual(c.cuba, "PRESSURE") self.assertEqual(c.cuba, "PRESSURE")
def test_positive_int(self):
c = Traited()
with self.assertRaises(TraitError):
c.positive_int = 0
with self.assertRaises(TraitError):
c.positive_int = -1
c.positive_int = 3
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment