Skip to content
Snippets Groups Projects
Commit a05d9e5d authored by Stefano Borini's avatar Stefano Borini
Browse files

Adds positive int trait

parent 61b6a8f8
No related branches found
No related tags found
1 merge request!164Adds positive int trait
......@@ -43,4 +43,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_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
#: 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)")
#: Identifies a CUBA type with its key. At the moment a String with
#: no validation, but will come later.
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
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):
val = Identifier()
cuba = CUBAType()
positive_int = PositiveInt()
class TestLocalTraits(unittest.TestCase):
......@@ -25,3 +26,13 @@ class TestLocalTraits(unittest.TestCase):
c = Traited()
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