def test_default_values_on_stereotype_inheritance2(self): mcl = CMetaclass("MCL", attributes={ "aStr2": "def", "aList2": ["d1", "d2"], "b": bool }) mcl2 = CMetaclass("MCL2", superclasses=mcl, attributes={ "aStr1": str, "aList1": list }) try: CStereotype("S1", extended=mcl, default_values={ "aStr1": "a", "aList2": [] }) exception_expected_() except CException as e: eq_( e.value, "attribute 'aStr1' unknown for metaclasses extended by stereotype 'S1'" ) s2 = CStereotype("S2", extended=mcl2, default_values={ "aList1": ["a"], "aStr2": "def2" }) eq_(s2.default_values, {"aList1": ["a"], "aStr2": "def2"})
def test_apply_stereotype_instances_wrong_metaclass_inheritance(self): mcl1 = CMetaclass("MCL1", superclasses=self.mcl) mcl2 = CMetaclass("MCL2", superclasses=self.mcl) s1 = CStereotype("S1", extended=mcl1) s2 = CStereotype("S2", extended=mcl2) super_stereotype = CStereotype("SuperST", extended=self.mcl) c = CClass(self.mcl, "CL") try: c.stereotype_instances = s1 exception_expected_() except CException as e: eq_( e.value, "stereotype 'S1' cannot be added to 'CL': no extension by this stereotype found" ) c.stereotype_instances = super_stereotype mcl1_class = CClass(mcl1, "Mcl1Class", stereotype_instances=s1) try: mcl1_class.stereotype_instances = [s1, s2] exception_expected_() except CException as e: eq_( e.value, "stereotype 'S2' cannot be added to 'Mcl1Class': no extension by this stereotype found" ) mcl1_class.stereotype_instances = [s1, super_stereotype] eq_(set(mcl1_class.stereotype_instances), {s1, super_stereotype}) eq_(c.stereotype_instances, [super_stereotype])
def test_default_values_on_stereotype_inheritance1(self): mcl = CMetaclass("MCL", attributes={ "aStr2": "def", "aList2": ["d1", "d2"], "b": bool }) mcl2 = CMetaclass("MCL2", superclasses=mcl, attributes={ "aStr1": str, "aList1": list }) s1 = CStereotype("S1", extended=mcl2, default_values={ "aStr1": "a", "aList2": [] }) s2 = CStereotype("S2", superclasses=s1, extended=mcl2, default_values={ "aList1": ["a"], "aStr2": "def2" }) eq_(s1.default_values, {"aStr1": "a", "aList2": []}) eq_(s2.default_values, {"aList1": ["a"], "aStr2": "def2"}) eq_(s1.get_default_value("aStr1"), "a") eq_(s2.get_default_value("aStr2"), "def2") eq_(s2.get_default_value("aStr1"), None) eq_(s1.get_default_value("aStr2"), None)
def test_delete_attribute_values_with_superclass(self): mcl_super = CMetaclass("SCL_M", attributes={ "intVal": 20, "intVal2": 30 }) mcl = CMetaclass("M", superclasses=mcl_super, attributes={ "isBoolean": True, "intVal": 1 }) cl = CClass(mcl, "C", values={"isBoolean": False}) cl.delete_value("isBoolean") cl.delete_value("intVal2") eq_(cl.values, {"intVal": 1}) cl.set_value("intVal", 2, mcl_super) cl.set_value("intVal", 3, mcl) eq_(cl.values, {"intVal": 3}) cl.delete_value("intVal") eq_(cl.values, {"intVal": 2}) cl.set_value("intVal", 2, mcl_super) cl.set_value("intVal", 3, mcl) cl.delete_value("intVal", mcl) eq_(cl.values, {"intVal": 2}) cl.set_value("intVal", 2, mcl_super) cl.set_value("intVal", 3, mcl) cl.delete_value("intVal", mcl_super) eq_(cl.values, {"intVal": 3})
def test_metaclass_change(self): m1 = CMetaclass("M1") m2 = CMetaclass("M2") c1 = CClass(m1, "C1") c1.metaclass = m2 eq_(c1.metaclass, m2) eq_(m1.classes, []) eq_(m2.classes, [c1])
def test_metaclass_is_deleted_in_constructor(self): m1 = CMetaclass("M1") m1.delete() try: CClass(m1, "C1") exception_expected_() except CException as e: ok_(e.value.endswith("cannot access named element that has been deleted"))
def test_super_metaclasses_that_are_deleted(self): m1 = CMetaclass("M1") m1.delete() try: CMetaclass(superclasses=[m1]) exception_expected_() except CException as e: eq_(e.value, "cannot access named element that has been deleted")
def test_add_stereotype_instance_metaclass_correct_by_inheritance_of_metaclass( self): mcl1 = CMetaclass("MCL1") mcl2 = CMetaclass("MCL2", superclasses=mcl1) s = CStereotype("S1", extended=mcl1) c = CClass(mcl2, "CL", stereotype_instances=s) eq_(s.extended_instances, [c]) eq_(c.stereotype_instances, [s])
def test_default_init_after_instance_creation(self): enum_type = CEnum("EnumT", values=["A", "B", "C"]) mcl = CMetaclass(attributes={"e1": enum_type, "e2": enum_type}) cl = CClass(mcl, "C") e2 = mcl.get_attribute("e2") e2.default = "A" eq_(cl.get_value("e1"), None) eq_(cl.get_value("e2"), "A")
def test_extended_metaclass_that_is_deleted(self): m1 = CMetaclass("M1") m1.delete() try: CStereotype(extended=[m1]) exception_expected_() except CException as e: eq_(e.value, "cannot access named element that has been deleted")
def test_attribute_get_name_and_classifier(self): m = CMetaclass("M", attributes={"isBoolean": True}) a = m.get_attribute("isBoolean") eq_(a.name, "isBoolean") eq_(a.classifier, m) m.delete() eq_(a.name, None) eq_(a.classifier, None)
def test_stereotype_extended_append(self): m1 = CMetaclass() m2 = CMetaclass() s = CStereotype(extended=[m1]) # should have no effect, as setter must be used s.extended.append(m2) eq_(m1.stereotypes, [s]) eq_(m2.stereotypes, []) eq_(s.extended, [m1])
def test_extended_classes_of_inheriting_stereotypes__superclass_is_set_to_the_same( self): m1 = CMetaclass("M1") s1 = CStereotype("S1") s2 = CStereotype("S2", extended=[m1], superclasses=[s1]) m1.stereotypes = [s2, s1] eq_(set(s1.extended), {m1}) eq_(set(s2.extended), {m1}) eq_(set(m1.stereotypes), {s2, s1})
def test_add_stereotype_of_inherited_metaclass(self): sub1 = CMetaclass("Sub1", superclasses=self.m1) sub2 = CMetaclass("Sub2", superclasses=self.m2) s = CStereotype("S1", extended=self.a) c1 = CClass(sub1, "C1") c2 = CClass(sub2, "C2") links = add_links({c1: c2}, stereotype_instances=s)[0] eq_(s.extended_instances, [links]) eq_(links.stereotype_instances, [s])
def test_extended_classes_of_inheriting_stereotypes__remove_superclass_stereotype( self): m1 = CMetaclass() s1 = CStereotype(extended=[m1]) s2 = CStereotype(superclasses=[s1], extended=[m1]) m1.stereotypes = s2 eq_(set(s1.extended), set()) eq_(set(s2.extended), {m1}) eq_(set(m1.stereotypes), {s2})
def test_check_derived_association_is_metaclass_association(self): m1 = CMetaclass("MC1") m2 = CMetaclass("MC2") try: m1.association(m2, "* -> 1", derived_from=self.a) exception_expected_() except CException as e: eq_(e.value, "association 'derived_from' is called on is not a class-level association")
def test_metaclass_change_none_input(self): m1 = CMetaclass("M1") CMetaclass("M2") c1 = CClass(m1, "C1") try: c1.metaclass = None exception_expected_() except CException as e: eq_("'None' is not a metaclass", e.value)
def test_metaclass_change_wrong_input_type(self): m1 = CMetaclass("M1") CMetaclass("M2") c1 = CClass(m1, "C1") try: c1.metaclass = CClass(m1) exception_expected_() except CException as e: ok_(e.value.endswith("' is not a metaclass"))
def setup(self): self.m1 = CMetaclass("M1") self.m2 = CMetaclass("M2") self.a = self.m1.association(self.m2, name="a", multiplicity="*", role_name="m2", source_multiplicity="1", source_role_name="m1")
def test_extended_classes_of_inheriting_stereotypes__superclass_has_none( self): m1 = CMetaclass() m2 = CMetaclass(superclasses=[m1]) s1 = CStereotype() s2 = CStereotype(superclasses=[s1]) m2.stereotypes = s2 eq_(len(s1.extended), 0) eq_(set(m2.stereotypes), {s2})
def test_get_connected_elements__wrong_keyword_arg(self): m1 = CMetaclass("m1") try: m1.get_connected_elements(a="m1") exception_expected_() except CException as e: eq_( e.value, "unknown keyword argument 'a', should be one of: " + "['add_associations', 'add_stereotypes', 'process_stereotypes', 'add_bundles', 'process_bundles', " + "'stop_elements_inclusive', 'stop_elements_exclusive']")
def test_extended_classes_of_inheriting_stereotypes__superclass_has_metaclasses_superclass( self): m1 = CMetaclass() m2 = CMetaclass(superclasses=[m1]) s1 = CStereotype(extended=[m1]) s2 = CStereotype(superclasses=[s1], extended=[m2]) eq_(set(s1.extended), {m1}) eq_(set(s2.extended), {m2}) eq_(set(m1.stereotypes), {s1}) eq_(set(m2.stereotypes), {s2})
def test_metaclass_unknown_non_positional_argument(self): t = CMetaclass("T") try: CMetaclass("ST", superclass=t) exception_expected_() except CException as e: eq_( "unknown keyword argument 'superclass', should be one of: " + "['stereotypes', 'attributes', 'superclasses', 'bundles']", e.value)
def test_delete_metaclass_having_two_bundles(self): b1 = CBundle("B1") b2 = CBundle("B2") m1 = CMetaclass("m1", bundles=[b1, b2]) m2 = CMetaclass("m2", bundles=[b2]) m1.delete() eq_(set(b1.get_elements(type=CMetaclass)), set()) eq_(set(b2.get_elements(type=CMetaclass)), {m2}) eq_(set(m1.bundles), set()) eq_(set(m2.bundles), {b2})
def test_metaclass_is_deleted_in_metaclass_method(self): m1 = CMetaclass("M1") m2 = CMetaclass("M2") c1 = CClass(m2, "C1") m1.delete() try: c1.metaclass = m1 exception_expected_() except CException as e: ok_(e.value.endswith("cannot access named element that has been deleted"))
def test_creation_of_unnamed_metaclass_in_bundle(self): m1 = CMetaclass() m2 = CMetaclass() m3 = CMetaclass("x") cl = CClass(m1) self.b1.elements = [m1, m2, m3, cl] eq_(set(self.b1.get_elements(type=CMetaclass)), {m1, m2, m3}) eq_(self.b1.get_element(name=None), m1) eq_(set(self.b1.get_elements(type=CMetaclass, name=None)), {m1, m2}) eq_(set(self.b1.get_elements(name=None)), {m1, m2, cl})
def test_link_association_has_a_compatible_superclass(self): code = CMetaclass("Code") source = CMetaclass("Source") code.association(source, "[contained_code] * -> [source] *") code_a = CMetaclass("Code A", superclasses=code) code_b = CMetaclass("Code B", superclasses=code) a_b_association = code_a.association(code_b, "a_b: [code_a] * -> [code_b] *") source_1 = CClass(source, "source_1") code_a1 = CClass(code_a, "code_a1") code_b1 = CClass(code_b, "code_b1") code_b2 = CClass(code_b, "code_b2") links = add_links({code_a1: [code_b1, code_b2]}, association=a_b_association) try: add_links({source_1: [code_a1, code_b2, code_b1, links[0], links[1]]}, role_name="contained_code") exception_expected_() except CException as e: eq_("the metaclass link's association is missing a compatible classifier", e.value) a_b_association.superclasses = self.mcl try: add_links({source_1: [code_a1, code_b2, code_b1, links[0], links[1]]}, role_name="contained_code") exception_expected_() except CException as e: eq_("no common metaclass for classes or links found", e.value) a_b_association.superclasses = code_b add_links({source_1: [code_a1, code_b2, code_b1, links[0], links[1]]}, role_name="contained_code")
def setup(self): self.mcl = CMetaclass("MCL") self.stereotype = CStereotype("S", extended=self.mcl) self.m1 = CMetaclass("M1") self.m2 = CMetaclass("M2") self.a = self.m1.association(self.m2, name="a", multiplicity="*", role_name="m1", source_multiplicity="1", source_role_name="m2")
def test_attributes_overwrite_no_defaults(self): mcl = CMetaclass(attributes={"isBoolean": bool, "intVal": int}) cl = CClass(mcl, "C") eq_(cl.get_value("isBoolean"), None) cl.set_value("isBoolean", False) mcl.attributes = {"isBoolean": bool, "intVal": int, "floatVal": float} eq_(cl.get_value("isBoolean"), False) eq_(cl.get_value("floatVal"), None) eq_(cl.get_value("intVal"), None) cl.set_value("floatVal", 1.2) eq_(cl.get_value("floatVal"), 1.2)
def test_default_values_from_stereotype__metaclass_delete(self): mcl = CMetaclass("MCL", attributes={ "aStr2": "def", "aList2": ["d1", "d2"], "b": bool }) mcl2 = CMetaclass("MCL2", superclasses=mcl, attributes={ "aStr1": str, "aList1": list }) s1 = CStereotype("S1", extended=mcl2, default_values={ "aStr1": "a", "aList2": [] }) s2 = CStereotype("S2", superclasses=s1, extended=mcl2, default_values={ "aList1": ["a"], "aStr2": "def2" }) c1 = CClass(mcl2, "C1", stereotype_instances=s2) mcl.delete() c2 = CClass(mcl2, "C2", stereotype_instances=s1) c3 = CClass(mcl2, "C3", stereotype_instances=s2) eq_(c1.get_value("aStr1"), "a") eq_(c1.get_value("aList1"), ["a"]) try: eq_(c1.get_value("aStr2"), "def2") exception_expected_() except CException as e: eq_(e.value, "attribute 'aStr2' unknown for 'C1'") eq_(c2.get_value("aStr1"), "a") eq_(c2.get_value("aList1"), None) try: eq_(c2.get_value("aStr2"), "def2") exception_expected_() except CException as e: eq_(e.value, "attribute 'aStr2' unknown for 'C2'") eq_(c3.get_value("aStr1"), "a") eq_(c3.get_value("aList1"), ["a"]) try: eq_(c3.get_value("aStr2"), "def2") exception_expected_() except CException as e: eq_(e.value, "attribute 'aStr2' unknown for 'C3'")