def test_default(self) -> None: class Foo(HasProps): y = bcpd.NumberSpec(default=12) f = Foo() assert f.y == 12 assert Foo.__dict__["y"].get_value(f) == Value(12) f.y = "y1" assert f.y == "y1" # Once we set a concrete value, the default is ignored, because it is unused f.y = 32 assert f.y == 32 assert Foo.__dict__["y"].get_value(f) == Value(32)
def test_case_insensitive_named_value(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() f.col = "RED" assert f.col == "RED" assert desc.get_value(f) == Value("RED") f.col = "ForestGreen" assert f.col == "ForestGreen" assert desc.get_value(f) == Value("ForestGreen")
def test_named_value(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() f.col = "red" assert f.col == "red" assert desc.get_value(f) == Value("red") f.col = "forestgreen" assert f.col == "forestgreen" assert desc.get_value(f) == Value("forestgreen")
def test_font_size_from_string(self) -> None: class Foo(HasProps): x = bcpd.FontSizeSpec(default="0px") css_units = "%|em|ex|ch|ic|rem|vw|vh|vi|vb|vmin|vmax|cm|mm|q|in|pc|pt|px" a = Foo() assert a.x == "0px" for unit in css_units.split("|"): v = '10%s' % unit a.x = v assert a.x == v assert a.lookup('x').get_value(a) == Value(v) v = '10.2%s' % unit a.x = v assert a.x == v assert a.lookup('x').get_value(a) == Value(v) f = '_10%s' % unit a.x = f assert a.x == f assert a.lookup('x').get_value(a) == Field(f) f = '_10.2%s' % unit a.x = f assert a.x == f assert a.lookup('x').get_value(a) == Field(f) for unit in css_units.upper().split("|"): v = '10%s' % unit a.x = v assert a.x == v assert a.lookup('x').get_value(a) == Value(v) v = '10.2%s' % unit a.x = v assert a.x == v assert a.lookup('x').get_value(a) == Value(v) f = '_10%s' % unit a.x = f assert a.x == f assert a.lookup('x').get_value(a) == Field(f) f = '_10.2%s' % unit a.x = f assert a.x == f assert a.lookup('x').get_value(a) == Field(f)
def test_value(self) -> None: class Foo(HasProps): x = bcpd.NumberSpec("xfield") f = Foo() assert f.x == "xfield" f.x = 12 assert f.x == 12 assert Foo.__dict__["x"].get_value(f) == Value(12) f.x = 15 assert f.x == 15 assert Foo.__dict__["x"].get_value(f) == Value(15) f.x = dict(value=32) assert Foo.__dict__["x"].get_value(f) == Value(32)
def test_multiple_instances(self) -> None: class Foo(HasProps): x = bcpd.NumberSpec("xfield") a = Foo() b = Foo() a.x = 13 b.x = 14 assert a.x == 13 assert b.x == 14 assert Foo.__dict__["x"].get_value(a) == Value(13) assert Foo.__dict__["x"].get_value(b) == Value(14) b.x = {"field": "x3"} assert Foo.__dict__["x"].get_value(a) == Value(13) assert Foo.__dict__["x"].get_value(b) == Field("x3")
def test_tuple_value(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() f.col = (128, 200, 255) assert f.col == (128, 200, 255) assert desc.get_value(f) == Value((128, 200, 255)) f.col = "myfield" assert f.col == "myfield" assert desc.get_value(f) == Field("myfield") f.col = (100, 150, 200, 0.5) assert f.col == (100, 150, 200, 0.5) assert desc.get_value(f) == Value((100, 150, 200, 0.5))
def test_named_value_set_none(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() f.col = None assert desc.get_value(f) == Value(None)
def test_fixed_value(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("gray") desc = Foo.__dict__["col"] f = Foo() assert f.col == "gray" assert desc.get_value(f) == Value("gray")
def test_default_tuple(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec(default=(128, 255, 124)) desc = Foo.__dict__["col"] f = Foo() assert f.col == (128, 255, 124) assert desc.get_value(f) == Value((128, 255, 124))
def test_field_default(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec(default="red") desc = Foo.__dict__["col"] f = Foo() assert f.col == "red" assert desc.get_value(f) == Value("red") f.col = "myfield" assert f.col == "myfield" assert desc.get_value(f) == Field("myfield")
def test_field(self) -> None: class Foo(HasProps): col = bcpd.HatchPatternSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() assert f.col == "colorfield" assert desc.get_value(f) == Field("colorfield") f.col = "myfield" assert f.col == "myfield" assert desc.get_value(f) == Field("myfield") f.col = "dot" assert f.col == "dot" assert desc.get_value(f) == Value("dot") f.col = "/" assert f.col == "/" assert desc.get_value(f) == Value("/")
def test_hex_value(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() f.col = "#FF004A" assert f.col == "#FF004A" assert desc.get_value(f) == Value("#FF004A") f.col = "myfield" assert f.col == "myfield" assert desc.get_value(f) == Field("myfield")
def test_named_color_overriding_default(self) -> None: class Foo(HasProps): col = bcpd.ColorSpec("colorfield") desc = Foo.__dict__["col"] f = Foo() f.col = "forestgreen" assert f.col == "forestgreen" assert desc.get_value(f) == Value("forestgreen") f.col = "myfield" assert f.col == "myfield" assert desc.get_value(f) == Field("myfield")
def test_patch_spec_property(self) -> None: d = document.Document() assert not d.roots assert len(d.models) == 0 root1 = ModelWithSpecInTestDocument(foo=42) d.add_root(root1) assert len(d.roots) == 1 def patch_test(new_value: Any): event1 = ModelChangedEvent(d, root1, 'foo', new_value) patch1 = patch_doc.create([event1]).content d.apply_json_patch(patch1) if isinstance(new_value, dict): return root1.lookup('foo').get_value(root1) else: return root1.foo assert patch_test(57) == 57 assert 'data' == root1.foo_units assert patch_test(dict(value=58)) == Value(58) assert 'data' == root1.foo_units assert patch_test(dict(value=58, units='screen')) == Value(58, units='screen') assert 'screen' == root1.foo_units assert patch_test(dict(value=59, units='screen')) == Value(59, units='screen') assert 'screen' == root1.foo_units assert patch_test(dict(value=59, units='data')) == Value(59) assert 'data' == root1.foo_units assert patch_test(dict(value=60, units='data')) == Value(60) assert 'data' == root1.foo_units assert patch_test(dict(value=60, units='data')) == Value(60) assert 'data' == root1.foo_units assert patch_test(61) == 61 assert 'data' == root1.foo_units root1.foo = "a_string" # so "woot" gets set as a string assert patch_test("woot") == "woot" assert 'data' == root1.foo_units assert patch_test(dict(field="woot2")) == Field("woot2") assert 'data' == root1.foo_units assert patch_test(dict(field="woot2", units='screen')) == Field("woot2", units='screen') assert 'screen' == root1.foo_units assert patch_test(dict(field="woot3")) == Field("woot3", units="screen") assert 'screen' == root1.foo_units assert patch_test(dict(value=70)) == Value(70, units="screen") assert 'screen' == root1.foo_units root1.foo = 123 # so 71 gets set as a number assert patch_test(71) == 71 assert 'screen' == root1.foo_units