def test_repeat_list(self): sf = SnippetFormatter() self.assertEqual( sf.format('''Table of contents: {chapters:repeat:Chapter {{item}} }''', chapters=["I", "II", "III", "IV"]), '''Table of contents: Chapter I Chapter II Chapter III Chapter IV ''')
class SnippetMacro(SegmentHandler): def __init__(self): self.sf = SnippetFormatter() self.regex = None def set(self, key, value): macros[key] = value def format(self, text, local): return [s + '\n' for s in self.sf.format(''.join(text), **{**macros, **local}).split('\n')][:-1] def handle(self, pass_nr, segment): if segment.name == "0": # at the start of a file there is no matching defined self.regex = None for line in segment.text[1:-1]: if re.search(r"NO\s+MATCH\W", line): self.regex = None continue startline = re.search(r"MATCH\s+(.*)", line) if startline: self.regex = startline.group(1) continue if self.regex: match = re_search(self.regex, line) if match and match.lastindex >= 2: self.set(match.group(1), match.group(2))
def generate_simple_equals(self): sf = SnippetFormatter() text = self.template("""\ @Override public boolean equals(Object o) {{ if (this == o) return true; \ {equals_allow_subclass:if:if (!(o instanceof {class})) return false;}\ {equals_allow_subclass:ifnot:if (o == null || getClass() != o.getClass()) return false;}\ {class} other = ({class}) o; {equals_with_getters:ifnot: {fields:repeat:{{value.need_equals:if:\ {{value.type_simple:if:if ( {{value.name}} != other.{{value.name}} ) return false;}}\ {{value.type:if=float:if (Float.compare(other.{{value.name}}, {{value.name}}) != 0) return false;}}\ {{value.type:if=double:if (Double.compare(other.{{value.name}}, {{value.name}}) != 0) return false;}}\ {{value.type_object:if:if ( {{value.name}} != null ? !{{value.name}}.equals(other.{{value.name}}) : other.{{value.name}} != null ) return false;}}\ }} }}{equals_with_getters:if: {fields:repeat:{{value.need_equals:if:\ {{value.type_simple:if:if ( {{value.getter_name}}() != other.{{value.getter_name}}() ) return false;}}\ {{value.type:if=float:if (Float.compare(other.{{value.getter_name}}(), {{value.getter_name}}() ) != 0) return false;}}\ {{value.type:if=double:if (Double.compare(other.{{value.getter_name}}(), {{value.getter_name}}() ) != 0) return false;}}\ {{value.type_object:if:if ( {{value.getter_name}}() != null ? !{{value.getter_name}}().equals(other.{{value.getter_name}}()) : other.{{value.getter_name}}() ) ) return false;}}\ }} }} return true; }} """) return text
def test_repeat_dict(self): sf = SnippetFormatter() self.assertEqual( sf.format('''Books: {books:repeat:"{{value}}" by {{key}} ---- }''', books={ 'Victor Hugo': 'Notre Dame de Paris', 'Joseph Conrad': 'Lord Jim', }), '''Books: "Notre Dame de Paris" by Victor Hugo ---- "Lord Jim" by Joseph Conrad ---- ''')
def handle_setters(self, segment): line = segment.text[0] for_all = re.search(r"for\s+all", line) for var in self.fields: var.need_setter_calculate(for_all) var.setter_name_calculate() sf = SnippetFormatter() text = self.template("""\ {fields:repeat:{{value.need_setter:if:\ {{value.setter_modifier}}{{value.setter_modifier:if: }}void {{value.setter_name}}(final {{value.type}} {{value.name}}){{{{ this.{{value.name}} = {{value.name}}; }}}} }}}""") segment.text = [segment.text[0]] + text + [segment.text[-1]] segment.modified = True return
def handle_tostring(self, segment): line = segment.text[0] self.template_parameters["tostring_with_getters"] = re.search(r"with\s+getters", line) for var in self.fields: var.getter_name_calculate() sf = SnippetFormatter() text = self.template("""\ @Override public String toString() {{ final StringBuilder sb = new StringBuilder("{class}{{"); {tostring_with_getters:ifnot:{fields:repeat\ sb.append(",")\n:\ sb.append("{{value.name}}=").append({{value.name}});\n}}\ {tostring_with_getters:if:{fields:repeat\ sb.append(",")\n:\ sb.append("{{value.name}}=").append({{value.getter_name}}());\n}}\ sb.append("}}"); return sb.toString(); }} """) segment.text = [segment.text[0]] + text + [segment.text[-1]] segment.modified = True
def handle_builder(self, segment): line = segment.text[1] match = re.search(r"(protected|private|public).*class\s+(\w[\w\d_]*)", line) if not match: match = re.search(r"().*class\s+(\w[\w\d_]*)", line) if match: self.template_parameters["builder_class_modifier"] = match.group(1) self.template_parameters["builder_classname"] = match.group(2) else: self.template_parameters["builder_class_modifier"] = "public" self.template_parameters["builder_classname"] = "Builder" for var in self.fields: var.need_builder_calculate() var.builder_name_calculate() sf = SnippetFormatter() text = self.template("""\ {builder_class_modifier} static class {builder_classname} {{ private {builder_classname}(){{}} private {class} built = new {class}(); public {class} build(){{ final {class} r = built; built = null; return r; }} {fields:repeat:{{value.need_builder:if:\ public final {builder_classname} {{value.builder_name}}(final {{value.type}} {{value.name}}){{{{ built.{{value.name}} = {{value.name}}; return this; }}}}}} }public static {builder_classname} builder(){{ return new {builder_classname}(); }} """) segment.text = [segment.text[0]] + text + [segment.text[-1]] segment.modified = True
def __init__(self): self.sf = SnippetFormatter() self.regex = None
def template(self, template): sf = SnippetFormatter() return [s + "\n" for s in sf.format(template, **self.template_parameters).split("\n")][0:-1]
def test_ifnot_with_fields_false(self): sf = SnippetFormatter() self.assertEqual( sf.format('Action: Back / Logout {manager:ifnot:/ Delete {id}}', manager=False, id=34), 'Action: Back / Logout / Delete 34')
def test_ifnot_static_false(self): sf = SnippetFormatter() self.assertEqual( sf.format('Action: Back / Logout {manager:ifnot:/ Delete}', manager=False), 'Action: Back / Logout / Delete')
def test_basic(self): sf = SnippetFormatter() self.assertEqual(sf.format('a is {a}', a="A"), "a is A")
def test_if_static(self): sf = SnippetFormatter() self.assertEqual( sf.format('Action: Back / Logout {manager:if:/ Delete}', manager=True), 'Action: Back / Logout / Delete')
def test_call(self): sf = SnippetFormatter() self.assertEqual( sf.format('My name is {name.upper:call}', name="eric"), 'My name is ERIC')