def test_enforcement_of_class_methods(self) -> None: with EnforcedConditions(Pep316Parser(), {'DerivedFooable': BaseFooable}): with self.assertRaises(PreconditionFailed): BaseFooable.class_foo(50) with EnforcedConditions(Pep316Parser(), {'DerivedFooable': DerivedFooable}): DerivedFooable.class_foo(50)
def test_enforcement_of_class_methods(self) -> None: env = {"DerivedFooable": BaseFooable} with EnforcedConditions(Pep316Parser(), env) as enf, enf.enabled_enforcement(): with self.assertRaises(PreconditionFailed): BaseFooable.class_foo(50) env = {"DerivedFooable": DerivedFooable} with EnforcedConditions(Pep316Parser(), env) as enf, enf.enabled_enforcement(): DerivedFooable.class_foo(50)
def __enter__(self): super().__enter__() enforced_conditions = EnforcedConditions(Pep316Parser()) self.enter_context(COMPOSITE_TRACER) self.enter_context(enforced_conditions) self.enter_context(enforced_conditions.enabled_enforcement()) COMPOSITE_TRACER.trace_caller()
def test_attrs_restored_properly(self) -> None: orig_class_dict = DerivedFooable.__dict__.copy() with EnforcedConditions(Pep316Parser(), {'DerivedFooable': DerivedFooable}): pass for k, v in orig_class_dict.items(): self.assertIs(DerivedFooable.__dict__[k], v, f'member "{k}" changed afer encforcement')
def test_class_enforce(self) -> None: env = {"Pokeable": Pokeable} old_id = id(Pokeable.poke) Pokeable().pokeby(-1) # no exception (yet!) with EnforcedConditions(Pep316Parser(), env) as enf, enf.enabled_enforcement(): Pokeable().poke() with self.assertRaises(PreconditionFailed): Pokeable().pokeby(-1)
def test_enforce_conditions(self) -> None: env = {'foo': foo} self.assertEqual(foo(-1), -2) # unchecked with EnforcedConditions(Pep316Parser(), env): self.assertEqual(env['foo'](50), 100) with self.assertRaises(PreconditionFailed): env['foo'](-1) with self.assertRaises(PostconditionFailed): env['foo'](0)
def test_super_method_enforced(self) -> None: with EnforcedConditions(Pep316Parser(), {'DerivedFooable': DerivedFooable}): with self.assertRaises(PreconditionFailed): DerivedFooable().foo_only_in_super(50) with self.assertRaises(PreconditionFailed): DerivedFooable().foo(-1) # Derived class has a weaker precondition, so this is OK: DerivedFooable().foo(50)
def test_enforce_conditions(self) -> None: env = {"foo": foo} self.assertEqual(foo(-1), -2) # unchecked with EnforcedConditions(Pep316Parser(), env) as enf, enf.enabled_enforcement(): self.assertEqual(env["foo"](50), 100) with self.assertRaises(PreconditionFailed): env["foo"](-1) with self.assertRaises(PostconditionFailed): env["foo"](0)
def test_super_method_enforced(self) -> None: env = {"DerivedFooable": DerivedFooable} with EnforcedConditions(Pep316Parser(), env) as enf, enf.enabled_enforcement(): with self.assertRaises(PreconditionFailed): DerivedFooable().foo_only_in_super(50) with self.assertRaises(PreconditionFailed): DerivedFooable().foo(-1) # Derived class has a weaker precondition, so this is OK: DerivedFooable().foo(50)
def test_class_enforce(self) -> None: env = {'Pokeable': Pokeable} old_id = id(Pokeable.poke) Pokeable().pokeby(-1) # no exception (yet!) with EnforcedConditions(Pep316Parser(), env): self.assertNotEqual(id(env['Pokeable'].poke), old_id) Pokeable().poke() with self.assertRaises(PreconditionFailed): Pokeable().pokeby(-1) self.assertEqual(id(env['Pokeable'].poke), old_id)
def test_enforce_on_uncopyable_value(self) -> None: class NotCopyable: def __copy__(self): raise TypeError("not copyable") not_copyable = NotCopyable() env = {"same_thing": same_thing} with EnforcedConditions(Pep316Parser(), env): with self.assertRaises(AttributeError): env["same_thing"](not_copyable)
def test_enforce_and_unenforce(self) -> None: env = {'foo': foo, 'bar': lambda x: x, 'baz': 42} backup = env.copy() with EnforcedConditions(Pep316Parser(), env, interceptor=lambda f: (lambda x: x * 3)): self.assertIs(env['bar'], backup['bar']) self.assertIs(env['baz'], 42) self.assertIsNot(env['foo'], backup['foo']) self.assertEqual(env['foo'](50), 150) # type:ignore self.assertIs(env['foo'], backup['foo'])
def test_enforce_and_unenforce(self) -> None: env = {"foo": foo, "bar": lambda x: x, "baz": 42} backup = env.copy() with EnforcedConditions(Pep316Parser(), env, interceptor=lambda f: (lambda x: x * 3)): self.assertIs(env["bar"], backup["bar"]) self.assertIs(env["baz"], 42) self.assertIsNot(env["foo"], backup["foo"]) self.assertEqual(env["foo"](50), 150) # type:ignore self.assertIs(env["foo"], backup["foo"])
def test_enforce_on_uncopyable_value(self) -> None: env = {'l': same_stream} self.assertIs(same_stream(sys.stdout), sys.stdout) with EnforcedConditions(Pep316Parser(), env): with self.assertRaises(AttributeError): env['l'](sys.stdout)
def test_enforcement_of_static_methods(self) -> None: with EnforcedConditions(Pep316Parser(), {"DerivedFooable": DerivedFooable}): DerivedFooable.static_foo(50) with self.assertRaises(PreconditionFailed): BaseFooable.static_foo(50)