def test_change_signature_remove_parameter_nodefault(self): self.first["foo"] = (Function( "foo", _generic, [Signature([Parameter("a", _generic), Parameter("b", _generic)])])) self.second["foo"] = (Function( "foo", _generic, [Signature([Parameter("a", _generic)])])) self.expect(Bump.major)
def test_multiple_signatures_one_compat_one_removed(self): self.first["foo"] = (Function("foo", _generic, [ Signature([Parameter("a", _a)]), Signature([Parameter("a", _a), Parameter("b", _generic)]) ])) self.second["foo"] = (Function( "foo", _generic, [Signature([Parameter("a", _compatWithA)])])) self.expect(Bump.major)
def test_multiple_signatures_new_one(self): self.first["foo"] = (Function("foo", _generic, [Signature([Parameter("a", _a)])])) self.second["foo"] = (Function("foo", _generic, [ Signature([Parameter("a", _a)]), Signature([Parameter("a", _a), Parameter("b", _generic)]) ])) self.expect(Bump.minor)
def test_change_signature_rename_parameters(self): self.first["foo"] = (Function( "foo", _generic, [Signature([Parameter("a", _generic), Parameter("b", _generic)])])) self.second["foo"] = (Function( "foo", _generic, [Signature([Parameter("b", _generic), Parameter("a", _generic)])])) self.expect(Bump.patch)
def test_change_signature_add_parameter_default(self): self.first["foo"] = (Function( "foo", _generic, [Signature([Parameter("a", _generic), Parameter("b", _generic)])])) self.second["foo"] = (Function("foo", _generic, [ Signature([ Parameter("a", _generic), Parameter("b", _generic), Parameter("c", _generic, default_value=True) ]) ])) self.expect(Bump.minor)
def _class_or_interface_to_unit(node, compilation, type_system): """Convert a class or interface declaration ('node') into a Unit. Requires the 'compilation' where the node so it can be used to look up type information.""" assert isinstance(node, javalang.tree.ClassDeclaration) or \ isinstance(node, javalang.tree.InterfaceDeclaration) or \ isinstance(node, javalang.tree.AnnotationDeclaration) or \ isinstance(node, javalang.tree.EnumDeclaration) # TODO: Handle annotation declarations if isinstance(node, javalang.tree.AnnotationDeclaration): return Unit("annotation", {}, {}, {}) # TODO: Handle enum declarations if isinstance(node, javalang.tree.EnumDeclaration): return Unit("enum", {}, {}, {}) fields = dict() functions = dict() units = dict() for n in [n for n in node.body if _is_public(n)]: # Convert fields. if isinstance(n, javalang.tree.FieldDeclaration): for declarator_name in _get_declarator_names(n): type_object = type_system.qualify_lookup(n.type, compilation) fields[declarator_name] = Field(declarator_name, type_object) # Convert methods. elif isinstance(n, javalang.tree.MethodDeclaration): parameters = _get_parameters(n, type_system, compilation) return_type = _get_return_type(n, type_system, compilation) # Java supports method overloading, where methods with the # same name can be differentiated by their parameters and return type. # To fit this into autobump's common representation, we set # the first parameter of the method to be the return type. # Then, we set a dummy type that is compatible with anything as the # actual return type of the function. parameters = [Parameter("$AUTOBUMP_RETURN$", return_type) ] + parameters if n.name in functions: functions[n.name].signatures.append(Signature(parameters)) else: functions[n.name] = Function(n.name, _dummyType, [Signature(parameters)]) # Convert inner classes and interfaces. elif isinstance(n, javalang.tree.ClassDeclaration) or \ isinstance(n, javalang.tree.InterfaceDeclaration): units[n.name] = _class_or_interface_to_unit( n, compilation, type_system) fqn = _qualify_type(node, compilation) return Unit(fqn, fields, functions, units)
def _xml_element_to_unit(elt): """Convert an XML <class> element into a Unit.""" functions = dict() fields = dict() units = dict() for child in elt: if child.tag == "field": field = _xml_element_to_field(child) fields[field.name] = field elif child.tag == "method": signature = _xml_get_signature_of_method(child) if child.attrib["name"] in functions: functions[child.attrib["name"]].signatures.append(signature) else: function = Function(child.attrib["name"], _dummyType, [signature]) functions[function.name] = function elif child.tag == "class": unit = _xml_element_to_unit(child) units[unit.name] = unit return Unit(elt.attrib["name"], fields, functions, units)
def _container_to_unit(name, container): """Convert a Python AST module or class to a Unit.""" fields = dict() functions = dict() units = dict() for node in container.body: if hasattr(node, "name") and not _is_public(node.name): # Completely ignore any private things - # they are irrelevant to the API. continue if isinstance(node, ast.ClassDef): units[node.name] = _container_to_unit(node.name, node) elif isinstance(node, ast.FunctionDef): functions[node.name] = Function(node.name, _dynamic, _get_signature(node)) elif isinstance(node, ast.Assign): # TODO: Handle other forms of assignment. for target in [ t for t in node.targets if isinstance(t, ast.Name) and _is_public(t.id) ]: fields[target.id] = Field(target.id, _dynamic) return Unit(name, fields, functions, units)
def test_change_type_of_visible_function_incompatible(self): self.first["foo"] = (Function("foo", _a)) self.second["foo"] = (Function("foo", _incompatWithA)) self.expect(Bump.major)
def test_remove_function(self): self.first["foo"] = (Function("foo", _generic)) self.expect(Bump.major)
def test_add_function(self): self.second["foo"] = (Function("foo", _generic)) self.expect(Bump.minor)
def test_change_signature_parameter_type_incompatible(self): self.first["foo"] = (Function("foo", _a, [Signature([Parameter("a", _generic)])])) self.second["foo"] = (Function( "foo", _incompatWithA, [Signature([Parameter("a", _generic)])])) self.expect(Bump.major)
def read_function(lst): verify_tag("function", lst) tag, name, signatures = lst return Function(name, _clojure_nil, [read_signature(s) for s in signatures])