def test_multiple_choices(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices( 'a', 'b', None, number=True, color=True ), initial=None) obj = MyObject() obj.prop = 10 self.assert_property(obj, 10.0) obj.prop = 3.14159 self.assert_property(obj, 3.14159) obj.prop = REBECCAPURPLE self.assert_property(obj, NAMED_COLOR[REBECCAPURPLE]) obj.prop = '#112233' self.assert_property(obj, rgb(0x11, 0x22, 0x33)) obj.prop = None self.assert_property(obj, None) obj.prop = 'a' self.assert_property(obj, 'a') obj.prop = 'none' self.assert_property(obj, None) obj.prop = 'b' self.assert_property(obj, 'b') # Check the error message try: obj.prop = 'invalid' self.fail('Should raise ValueError') except ValueError as v: self.assertEqual( str(v), "Invalid value 'invalid' for property 'prop'; " "Valid values are: a, b, none, <number>, <color>" )
def test_string_symbol(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices(TOP, None), initial=None) obj = MyObject() # Set a symbolic value using the string value of the symbol # We can't just use the string directly, though - that would # get optimized by the compiler. So we create a string and # transform it into the value we want. val = 'TOP' obj.prop = val.lower() # Both equality and instance checking should work. self.assertEqual(obj.prop, TOP) self.assertIs(obj.prop, TOP)
def test_allow_integer(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices(integer=True), initial=0) obj = MyObject() self.assertEqual(obj.prop, 0) obj.prop = 10 self.assert_property(obj, 10) # This is an odd case; Python happily rounds floats to integers. # It's more trouble than it's worth to correct this. obj.prop = 3.14159 self.assert_property(obj, 3) with self.assertRaises(ValueError): obj.prop = REBECCAPURPLE with self.assertRaises(ValueError): obj.prop = '#112233' with self.assertRaises(ValueError): obj.prop = 'a' with self.assertRaises(ValueError): obj.prop = 'b' with self.assertRaises(ValueError): obj.prop = None with self.assertRaises(ValueError): obj.prop = 'none' # Check the error message try: obj.prop = 'invalid' self.fail('Should raise ValueError') except ValueError as v: self.assertEqual( str(v), "Invalid value 'invalid' for property 'prop'; Valid values are: <integer>" )
def test_values(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices('a', 'b', None), initial='a') obj = MyObject() self.assertEqual(obj.prop, 'a') with self.assertRaises(ValueError): obj.prop = 10 with self.assertRaises(ValueError): obj.prop = 3.14159 with self.assertRaises(ValueError): obj.prop = REBECCAPURPLE with self.assertRaises(ValueError): obj.prop = '#112233' obj.prop = None self.assert_property(obj, None) obj.prop = 'a' self.assert_property(obj, 'a') obj.prop = 'none' self.assert_property(obj, None) obj.prop = 'b' self.assert_property(obj, 'b') # Check the error message try: obj.prop = 'invalid' self.fail('Should raise ValueError') except ValueError as v: self.assertEqual( str(v), "Invalid value 'invalid' for property 'prop'; Valid values are: a, b, none" )
def test_allow_string(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices(string=True), initial='start') obj = MyObject() self.assertEqual(obj.prop, 'start') with self.assertRaises(ValueError): obj.prop = 10 with self.assertRaises(ValueError): obj.prop = 3.14159 obj.prop = REBECCAPURPLE self.assert_property(obj, 'rebeccapurple') obj.prop = '#112233' self.assert_property(obj, '#112233') obj.prop = 'a' self.assert_property(obj, 'a') obj.prop = 'b' self.assert_property(obj, 'b') with self.assertRaises(ValueError): obj.prop = None obj.prop = 'none' self.assert_property(obj, 'none') # Check the error message try: obj.prop = 99 self.fail('Should raise ValueError') except ValueError as v: self.assertEqual( str(v), "Invalid value '99' for property 'prop'; Valid values are: <string>" )
def test_allow_color(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices(color=True), initial='goldenrod') obj = MyObject() self.assertEqual(obj.prop, NAMED_COLOR[GOLDENROD]) with self.assertRaises(ValueError): obj.prop = 10 with self.assertRaises(ValueError): obj.prop = 3.14159 obj.prop = REBECCAPURPLE self.assert_property(obj, NAMED_COLOR[REBECCAPURPLE]) obj.prop = '#112233' self.assert_property(obj, rgb(0x11, 0x22, 0x33)) with self.assertRaises(ValueError): obj.prop = 'a' with self.assertRaises(ValueError): obj.prop = 'b' with self.assertRaises(ValueError): obj.prop = None with self.assertRaises(ValueError): obj.prop = 'none' # Check the error message try: obj.prop = 'invalid' self.fail('Should raise ValueError') except ValueError as v: self.assertEqual( str(v), "Invalid value 'invalid' for property 'prop'; Valid values are: <color>" )
def test_allow_number(self): class MyObject(BaseStyle): def __init__(self): self.apply = Mock() MyObject.validated_property('prop', choices=Choices(number=True), initial=0) obj = MyObject() self.assertEqual(obj.prop, 0) obj.prop = 10 self.assert_property(obj, 10.0) obj.prop = 3.14159 self.assert_property(obj, 3.14159) with self.assertRaises(ValueError): obj.prop = REBECCAPURPLE with self.assertRaises(ValueError): obj.prop = '#112233' with self.assertRaises(ValueError): obj.prop = 'a' with self.assertRaises(ValueError): obj.prop = 'b' with self.assertRaises(ValueError): obj.prop = None with self.assertRaises(ValueError): obj.prop = 'none' # Check the error message try: obj.prop = 'invalid' self.fail('Should raise ValueError') except ValueError as v: self.assertEqual( str(v), "Invalid value 'invalid' for property 'prop'; Valid values are: <number>" )
from travertino.size import BaseIntrinsicSize from toga.fonts import Font, SYSTEM_DEFAULT_FONT_SIZE ###################################################################### # Display ###################################################################### PACK = 'pack' ###################################################################### # Declaration choices ###################################################################### DISPLAY_CHOICES = Choices(PACK, NONE) VISIBILITY_CHOICES = Choices(VISIBLE, HIDDEN, NONE) DIRECTION_CHOICES = Choices(ROW, COLUMN) ALIGNMENT_CHOICES = Choices(LEFT, RIGHT, TOP, BOTTOM, CENTER, default=True) SIZE_CHOICES = Choices(NONE, integer=True) FLEX_CHOICES = Choices(number=True) PADDING_CHOICES = Choices(integer=True) TEXT_ALIGN_CHOICES = Choices(LEFT, RIGHT, CENTER, JUSTIFY, default=True) TEXT_DIRECTION_CHOICES = Choices(RTL, LTR) COLOR_CHOICES = Choices(color=True, default=True) BACKGROUND_COLOR_CHOICES = Choices(TRANSPARENT, color=True, default=True)
from unittest import TestCase from unittest.mock import Mock, call from travertino.declaration import BaseStyle, Choices VALUE1 = 'value1' VALUE2 = 'value2' VALUE3 = 'value3' VALUE_CHOICES = Choices(VALUE1, VALUE2, VALUE3, None, integer=True) DEFAULT_VALUE_CHOICES = Choices(VALUE1, VALUE2, VALUE3, integer=True, default=True) class Style(BaseStyle): def __init__(self, **kwargs): self.apply = Mock() super().__init__(**kwargs) # Some properties with explicit initial values Style.validated_property('explicit_const', choices=VALUE_CHOICES, initial=VALUE1) Style.validated_property('explicit_value', choices=VALUE_CHOICES, initial=0) Style.validated_property('explicit_none', choices=VALUE_CHOICES, initial=None) # A property with an implicit default value. # This usually means the default is platform specific. Style.validated_property('implicit', choices=DEFAULT_VALUE_CHOICES) # A set of directional properties Style.validated_property('thing_top', choices=VALUE_CHOICES, initial=0) Style.validated_property('thing_right', choices=VALUE_CHOICES, initial=0)