def test_code_114(managenamespace): class Point: def __init__(self, x=None, y=None): self._x = ConstrainedValue() self._y = ConstrainedValue() if x is not None: self.x = x if y is not None: self.y = y @property def x(self): return self._x @x.setter def x(self, value): self._x.constrain_with(FixedValueConstraint(value)) @property def y(self): return self._y @y.setter def y(self, value): self._y.constrain_with(FixedValueConstraint(value)) # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_1145(managenamespace): class FixedValueConstraint(Constraint): #... {{% skip %}} def __init__(self, value): self._value = value @property def value(self): return self._value def __repr__(self): return f"{self.__class__.__name__}<{self.value}>" #... {{% /skip %}} def validate_object(self, instance): pass def apply_reciprocal_constraint(self, instance): pass def cascade_constraints(self, instance): pass # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_1177(managenamespace): class InfluencedConstraint(Constraint): #... {{% skip %}} def __init__(self, constraint): self._constraint = constraint @property def constraint(self): return self._constraint def __repr__(self): return f"{self.__class__.__name__}<{self.constraint}>" #... {{% /skip %}} def validate_object(self, instance): if not isinstance(self, ConstraintSet): raise InvalidConstraintException( f"{self.__class__.__name__} can only" f" be applied to `ConstraintSet`, it cannot be applied to `{point.__class__.__name__}`" ) def apply_reciprocal_constraint(self, instance): pass def cascade_constraints(self, instance): pass # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_check_attribute_name_asserts(managenamespace): """Update asserts if an item is in the original module namespace.""" items = {"verify": None} with pytest.raises(AssertionError) as exc_info: managenamespace(operation="update", additions=items) want = "Not allowed to replace module level name verify because" assert want in str(exc_info.value)
def test_code_797(managenamespace): class LinkedValueConstraint(Constraint): #... {{% skip %}} def __init__(self, constraint_set): self._constraint_set = constraint_set @property def constraint_set(self): return self._constraint_set def __repr__(self): return f"{self.__class__.__name__}<{self.constraint_set}>" def constraint_callback(self, instance): if not isinstance(instance, ConstraintSet): raise InvalidConstraintException( f"{self.__class__.__name__} can only" f" be applied to `ConstraintSet`, it cannot be applied to `{point.__class__.__name__}`" ) self.constraint_set.constrain_with(LinkedValueConstraint(instance)) pass #... {{% /skip %}} def __eq__(self, other): return isinstance( other, self.__class__) and self.constraint_set == other.constraint_set # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_647(managenamespace): class InvalidConstraintException(RuntimeError): """Indicates that a constraint has been applied to an object which doesn't make sense.""" class CoincidentConstraint(Constraint): #... {{% skip %}} def __init__(self, line): self._line = line @property def line(self): return self._line def __repr__(self): return f"{self.__class__.__name__}<{self.line}>" #... {{% /skip %}} def constraint_callback(self, point): if not isinstance(point, Point): raise InvalidConstraintException( f"{self.__class__.__name__} can only" f" be applied to `Point`, it cannot be applied to `{point.__class__.__name__}`" ) point.x.constrain_with(InfluencedConstraint(self)) point.y.constrain_with(InfluencedConstraint(self)) # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_illegal_operation(managenamespace): """Update asserts if operation in not 'update', 'copy', or 'clear'.""" items = {"E": None, "F": None} with pytest.raises(AssertionError) as exc_info: managenamespace(operation="bogus", additions=items) want = 'operation="bogus" is not allowed' assert want in str(exc_info.value)
def test_code_963(managenamespace): from abc import ABC, abstractmethod class Constraint(ABC): """Used to restrict that value of a ```ConstrainedValue```.""" @abstractmethod def validate_object(self, instance): """Validates that `instance` is suitable. Raises `InvalidConstraintException` if not""" raise NotImplementedError( "`validate_object` must be implemented explicitly.") @abstractmethod def apply_reciprocal_constraint(self, instance): """Applies a matching constraint to the provided instance.""" raise NotImplementedError( "`apply_reciprocal_callback` must be implemented explicitly.") @abstractmethod def cascade_constraints(self, instance): """Applies appropriate constraints to the properties of `instance`.""" raise NotImplementedError( "`cascade_constraints` must be implemented explicitly.") # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_402(managenamespace): class ConstraintSet: def __init__(self): self._constraints = [] def constrain_with(self, constraint): """Add a constraint to this objects list of constraints.""" self._constraints.append(constraint) def reset_constraints(self): """Removes the exsting constraints from the constraint set""" self._constraints = [] def resolve(self): """Nieve implementation to aid testing""" for constraint in self._constraints: if isinstance(constraint, FixedValueConstraint): return constraint.value raise UnderconstrainedError("Fixed Value has not been provided.") class ConstrainedValue: """An object which can be passed around to represent a value.""" def __set_name__(self, owner, name): self.public_name = name self.private_name = f"_{name}" def __get__(self, instance, typ=None): # grab the ConstraintSet from the instance constraint_set = getattr(instance, self.private_name, None) # If the instance didn't have an initialised ConstraintSet then # give it one if constraint_set is None: constraint_set = ConstraintSet() setattr(instance, self.private_name, constraint_set) return constraint_set def __set__(self, instance, value): if isinstance(value, ConstraintSet): setattr(instance, self.private_name, value) return constraint_set = self.__get__(instance) constraint_set.reset_constraints() constraint_set.constrain_with(FixedValueConstraint(value)) class Point: x = ConstrainedValue() y = ConstrainedValue() def __init__(self, x=None, y=None): if x is not None: self.x = x if y is not None: self.y = y # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_21_output_32(managenamespace): import sys a = 10 print(sys.version_info) # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_85_output_93(capsys, managenamespace): hex_digits = string.hexdigits print(hex_digits) _phm_expected_str = """\ 0123456789abcdefABCDEF """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def test_code_763(managenamespace): class ConstrainedValue: """An object which can be passed around to represent a value.""" #... {{% skip %}} def __set_name__(self, owner, name): self.public_name = name self.private_name = f"_{name}" def __get__(self, instance, typ=None): constraint_set = getattr(instance, self.private_name, None) if constraint_set is None: constraint_set = ConstraintSet( f"{instance.name}.{self.public_name}") setattr(instance, self.private_name, constraint_set) return constraint_set #... {{% /skip %}} def __set__(self, instance, value): # Grab the ConstraintSet from the instance constraint_set = self.__get__(instance, None) constraint_set.reset_constraints() # if the value we've been asked to assign is a ConstraintSet # then add a LinkedValueConstraint: if isinstance(value, ConstraintSet): constraint_set.constrain_with(LinkedValueConstraint(value)) return # otherwise use a FixedValueConstraint to constrain to the provided # value constraint_set.constrain_with(FixedValueConstraint(value)) #... {{% skip %}} class Point: x = ConstrainedValue() y = ConstrainedValue() def __init__(self, name="", x=None, y=None): self._name = name if x is not None: self.x = x if y is not None: self.y = y @property def name(self): return self._name # {{% /skip %}} # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_update_item_removals(managenamespace): """Try an update with names that get popped before the update.""" # 'sys' below exercises the _ = additions.pop('sys', None) line in the # fixture source code. items = { "sys": None, "managenamespace": None, "doctest_namespace": None, "capsys": None, "_phm_expected_str": None, "example_variable": 1111, } managenamespace(operation="update", additions=items) namespace_copy = managenamespace(operation="copy") assert "example_variable" in namespace_copy assert namespace_copy["example_variable"] == 1111 assert "sys" not in namespace_copy assert "managenamespace" not in namespace_copy assert "doctest_namespace" not in namespace_copy assert "capsys" not in namespace_copy assert "_phm_expected_str" not in namespace_copy # Clear the namespace. managenamespace(operation="clear", additions=None) namespace_copy = managenamespace(operation="copy") assert len(namespace_copy) == 0 # Add more items to the namespace. more_items = {"A": None, "B": None, "C": None} managenamespace(operation="update", additions=more_items) namespace_copy = managenamespace(operation="copy") assert len(namespace_copy) == 3 for name in more_items.keys(): assert name in namespace_copy
def test_code_144_output_149(capsys, managenamespace): p = Point(1, 2) print(type(p.x).__name__) print(type(p.y).__name__) _phm_expected_str = """\ ConstrainedValue ConstrainedValue """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def test_code_268_output_274(capsys, managenamespace): p = Point(1, 2) q = Point() q.x = p.x print(f"q.x is {q.x.resolve()}") _phm_expected_str = """\ q.x is 1 """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def test_code_185_output_191(capsys, managenamespace): p = Point(1, 2) print(f"p.x is {p.x.resolve()}") print(f"p.y is {p.y.resolve()}") _phm_expected_str = """\ p.x is 1 p.y is 2 """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def test_code_403(managenamespace): from abc import ABC, abstractmethod class Constraint(ABC): """Used to restrict that value of a ```ConstrainedValue```.""" @abstractmethod def constraint_callback(self, instance): raise NotImplementedError( "`constraint_callback` must be implemented explicitly.") # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_108_output_114(capsys, managenamespace): print("Names are cleared after the code runs.") print(grades == ["A", "B", "C", "D"]) print(hex_digits) _phm_expected_str = """\ Names are cleared after the code runs. True 0123456789abcdefABCDEF """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="clear")
def test_directive_share_names(managenamespace): import string x, y, z = 77, 88, 99 def incrementer(x): return x + 1 grades = ["A", "B", "C"] # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_750_output_756(capsys, managenamespace): p = Point('p', 1, 2) print(p.x) c = LinkedValueConstraint(p.x) print(c) _phm_expected_str = """\ p.x LinkedValueConstraint<p.x> """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def test_code_633_output_638(capsys, managenamespace): p = Point(1, 2) c = LinkedValueConstraint(p.x) print(c) _phm_expected_str = """\ LinkedValueConstraint<ConstraintSet( FixedValueConstraint<1> )> """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def _phm_setup_teardown(managenamespace): # setup code line 55. import predictit import numpy as np import pandas as pd from predictit import config managenamespace(operation="update", additions=locals()) yield # <teardown code here> managenamespace(operation="clear")
def test_code_825_output_833(capsys, managenamespace): p = Point() l = LinkedValueConstraint(p.x) m = LinkedValueConstraint(p.x) n = LinkedValueConstraint(q.x) print(f"l == m: {l == m}") print(f"l == n: {l == n}") _phm_expected_str = """\ l == m: True l == n: False """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def test_code_617(managenamespace): class LinkedValueConstraint(Constraint): def __init__(self, constraint_set): self._constraint_set = constraint_set @property def constraint_set(self): return self._constraint_set def __repr__(self): return f"{self.__class__.__name__}<{self.constraint_set}>" # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_698_output_706(capsys, managenamespace): p = Point('p') q = Point('q') q.x = p.x print(f"p.x is {repr(p.x)}") print(f"q.x is {repr(q.x)}") _phm_expected_str = """\ p.x is ConstraintSet() q.x is ConstraintSet( LinkedValueConstraint<p.x> ) """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def _phm_setup_teardown(managenamespace): # setup code line 11. mylist = [1, 2, 3] # a, b = 10, 11 # phmdoctest:omit def raiser(): pass # assert False # phmdoctest:pass managenamespace(operation="update", additions=locals()) yield # teardown code line 41. mylist.clear() assert not mylist, "mylist was not emptied" # assert False # phmdoctest:omit managenamespace(operation="clear")
def test_code_557(managenamespace): class InfluencedConstraint(Constraint): def __init__(self, constraint): self._constraint = constraint def constraint_callback(self, instance): pass @property def constraint(self): return self._constraint def __repr__(self): return f"{self.__class__.__name__}<{self.constraint}>" # Caution- no assertions. managenamespace(operation="update", additions=locals())
def test_code_599_output_606(capsys, managenamespace): l = Line(1, 2, 3, 4) p = Point('p') p.constrain_with(CoincidentConstraint(l)) print(f"p is {repr(p)}") print(f"p.x is {repr(p.x)}") _phm_expected_str = """\ p is Point( CoincidentConstraint<Line<(1,2),(3,4)>> ) p.x is ConstraintSet( InfluencedConstraint<CoincidentConstraint<Line<(1,2),(3,4)>>> ) """ _phm_compare_exact(a=_phm_expected_str, b=capsys.readouterr().out) managenamespace(operation="update", additions=locals())
def _phm_setup_teardown(managenamespace): # setup code line 9. import math mylist = [1, 2, 3] a, b = 10, 11 def doubler(x): return x * 2 managenamespace(operation="update", additions=locals()) yield # teardown code line 58. mylist.clear() assert not mylist, "mylist was not emptied" managenamespace(operation="clear")
def test_setup_doctest_teardown_fixture(_phm_setup_doctest_teardown, managenamespace): """Show the fixture runs and the namespace is created.""" # This is the fixture with placeholders for setup and teardown code. # There is no way to get code coverage of the # doctest_namespace assignment in the loop. items = managenamespace(operation="copy") assert items == dict()