diff --git a/bin/generate_tests b/bin/generate_tests index 0de2e0c2..0fc15f21 100755 --- a/bin/generate_tests +++ b/bin/generate_tests @@ -85,6 +85,11 @@ def quote(obj) -> str: return TypeError(f"quote for type {type(obj)} not supported") +def binary(num: int, padding: int = 0) -> str: + """Filter `binary` that converts an int to a binary string.""" + return f"{num:0{padding}b}" + + def camel_to_screaming(name: str) -> str: """Filter `camel_to_screaming` that converts a camel case string to screaming case.""" return name[0].upper() + name[1:] @@ -94,6 +99,7 @@ def jinja_env(exercise: pathlib.Path) -> jinja2.Environment: """Return a configured Jinja env with filters added.""" env = jinja2.Environment(loader=jinja2.FileSystemLoader(exercise / ".meta")) # C quoting of arbitrary data objects. + env.filters["binary"] = binary env.filters["quote"] = quote env.filters["camel_to_screaming"] = camel_to_screaming return env diff --git a/exercises/practice/micro-blog/.meta/template.j2 b/exercises/practice/micro-blog/.meta/template.j2 new file mode 100644 index 00000000..446d9694 --- /dev/null +++ b/exercises/practice/micro-blog/.meta/template.j2 @@ -0,0 +1,40 @@ +{%- macro let_inputs(case) %} + {%- for k, v in case.input.items() -%} + let g:{{ k }} = {{ v | quote }} + {%- if not loop.last %} + {% endif %} + {%- endfor -%} +{%- endmacro %} + +{%- macro let_expected(case) -%} + {%- if case.expect_error -%} + let g:expected = {{ case.expect_error_msg | quote }} + {%- else -%} + let g:expected = {{ case.expected | quote }} + {%- endif -%} +{%- endmacro %} + +{%- macro check(case) -%} + {%- if case.expect_error -%} + AssertThrows call + {%- else -%} + AssertEqual g:expected, + {%- endif %} {{ case.property_function }}( + {%- for k in case.input -%} + g:{{ k }} + {%- if not loop.last %}, {% endif -%} + {%- endfor -%} + ) + {%- if case.expect_error %} + AssertEqual g:expected, g:vader_exception + {%- endif %} +{%- endmacro %} + +{%- for case in cases %} +Execute ({{ case.descriptions | join(" - ") }}): + {%- if case.input %} + {{ let_inputs(case) }} + {%- endif %} + {{ let_expected(case) }} + {{ check(case) }} +{% endfor %} diff --git a/exercises/practice/micro-blog/micro_blog.vader b/exercises/practice/micro-blog/micro_blog.vader index 4c406b39..9200e606 100644 --- a/exercises/practice/micro-blog/micro_blog.vader +++ b/exercises/practice/micro-blog/micro_blog.vader @@ -1,4 +1,3 @@ - Execute (English language short): let g:phrase = "Hi" let g:expected = "Hi"