def test_instantiate_with(self): instance = ClassHeader(self.fake_template, self.fake_license_header).instantiate_with( self.class_name) for token, value in ClassHeader.create_replacement_rules( self.class_name).items(): self.assert_string_does_not_contain(instance, token) self.assert_string_contains(instance, value) self.assert_string_contains(instance, "license")
class ImplementationHeader(): def __init__(self, template, license_header, interface): self.bare_header = ClassHeader(template, license_header) self.interface = interface def instantiate_with(self, class_name, interface_name): return self.add_content_to( self.bare_header.instantiate_with(class_name), class_name, interface_name) def add_content_to(self, header, class_name, interface_name): header = self.add_interface_header(header, self.interface) header = self.add_inheritance(header, class_name, interface_name) header = self.add_methods(header, self.interface) return header @staticmethod def add_inheritance(header, class_name, interface_name): inheritance = ": public " + interface_name return header.replace(class_name + " {", class_name + inheritance + " {") @staticmethod def add_interface_header(header, interface): project_name = InterfaceHeader.extract_project_name(interface) interface_name = InterfaceHeader.extract_interface_name(interface) include = "#include <" + project_name + "/" + interface_name + ".h>\n" return header.replace("namespace", include + "\nnamespace") @staticmethod def add_methods(header, interface): class_end = "};" header = header.replace(class_end, "\n" + class_end) for method in InterfaceHeader.extract_methods(interface): header = header.replace( class_end, ImplementationHeader.create_signature(method) + class_end) return header @staticmethod def create_signature(method): return " " + method + " override;\n"
class InterfaceHeader(): def __init__(self, template, license_header): self.bare_header = ClassHeader(template, license_header) def instantiate_with(self, class_name): return self.add_content_to( self.bare_header.instantiate_with(class_name), class_name) @staticmethod def add_content_to(bare_header, class_name): virtual_destructor = " virtual ~" + class_name + "() {}\n" method_template = " //virtual return_type_ method_name_(input_type_) = 0;\n" token = "};" return bare_header.replace( token, virtual_destructor + "\n" + method_template + token) # \todo : This class should be renamed InterfaceHeaderTemplate and the following method should be in another class # named InterfaceHeader @staticmethod def extract_interface_name(header): for line in header.splitlines(): if line.startswith("class"): return line.split()[1] return "" @staticmethod def extract_methods(header): method_token = " = 0;" methods = [] for line in header.splitlines(): if not line.find(method_token) == -1: methods.append(line.lstrip().replace("virtual ", "").replace( method_token, "")) return methods @staticmethod def extract_project_name(header): for line in header.splitlines(): if line.startswith("namespace"): return line.split()[1] return ""
def create_header_file(self): path = self.path.to_include_directory/(self.class_name+".h") template = ClassHeader(File.read(self.path.to_class_header_template), self.create_license_header()) File.write(self.path.to_include_directory/(self.class_name+".h"), template.instantiate_with(self.class_name))
def test_create_replacement_rules(self): self.assert_equals( { "class_name_": str(self.class_name), "CLASS_NAME_": str(self.class_name).upper() + "_" }, ClassHeader.create_replacement_rules(self.class_name))
def __init__(self, template, license_header, interface): self.bare_header = ClassHeader(template, license_header) self.interface = interface
def __init__(self, template, license_header): self.bare_header = ClassHeader(template, license_header)