Files
python/exercises/complex-numbers/.meta/template.j2
Michael Morehouse 7a42fb71e2 various: comma fixes (#2150)
* forth: reinstate seperate classes

The template removed seperate classes per major case, resulting in
several tests with duplicate names and therefore an incomplete test
suite.

This reinstates the distinct classes.

Fixes #2148

* forth: minor black issue

Was accidentally running on a later version of Black than the one
specified in our requirements-generator.txt.

* various: fixes trailing comma issues

An upcoming change in Black revealed that we were adding unnecessary
trailing commas. These will _not_ be trimmed by Black in future builds.

Co-authored-by: Corey McCandless <cmccandless@users.noreply.github.com>
2019-12-24 21:28:38 -05:00

63 lines
2.1 KiB
Django/Jinja

from __future__ import division
import math
{% extends "master_template.j2" -%}
{%- set imports = ["ComplexNumber"] -%}
{%- macro translate_math(item) -%}
{{ item | replace("pi", "math.pi") | replace("e", "math.e") | replace("ln", "math.log") }}
{%- endmacro %}
{%- macro formatValue(val) -%}
{% if val is iterable and val is not string -%}
ComplexNumber({% for part in val %}{{ translate_math(part) }}{{ "," if not loop.last }}{% endfor %})
{%- else -%}
{{ translate_math(val) }}
{%- endif -%}
{%- endmacro -%}
{%- macro smartAssert(case) -%}
self.assert
{%- if case["property"] in ["exp", "div"] -%}Almost
{%- elif case["property"] == "eq" and not case["expected"] -%}Not
{%- endif -%}Equal
{%- endmacro -%}
{%- macro operator(prop) -%}
{% if prop == "add" -%}+
{%- elif prop == "sub" -%}-
{%- elif prop == "mul" -%}*
{%- elif prop == "div" -%}/
{%- endif -%}
{%- endmacro -%}
{% macro test_case(case) -%}
def test_{{ case["description"] | to_snake }}(self):
{% set input = case["input"] -%}
{%- set prop = case["property"] -%}
{%- set is_callable = prop not in ["real", "imaginary"] -%}
{%- set is_builtin = prop in ["abs"] -%}
{%- set expected = formatValue(case["expected"]) -%}
{%- if input|length == 1 -%}
{%- set z = formatValue(input["z"]) -%}
{% if is_builtin -%}
{%- set actual = prop ~ "(" ~ z ~ ")" -%}
{%- else -%}
{%- set actual = z ~ "." ~ prop -%}
{%- if is_callable -%}
{%- set actual = actual ~ "()" -%}
{%- endif -%}
{%- endif -%}
{%- else -%}
{%- set z1 = formatValue(input["z1"]) -%}
{%- set z2 = formatValue(input["z2"]) -%}
{%- if prop == "eq" -%}
{%- set actual = z1 -%}
{%- set expected = z2 -%}
{%- else -%}
{%- set actual = z1 ~ operator(prop) ~ z2 -%}
{% endif -%}
{%- endif -%}
{{ smartAssert(case) }}({{ actual }}, {{expected}})
{%- endmacro %}