Skip to content

Commit 53cef5b

Browse files
yawpitchcmccandless
authored andcommitted
generate_tests: option to wrap long lines (#2125)
Adds a function to the render environment to allow line wrapping of VERY long strings. Updates the alphametics test as an example.
1 parent 4d949b3 commit 53cef5b

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

bin/generate_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from string import punctuation, whitespace
2929
from subprocess import check_call
3030
from tempfile import NamedTemporaryFile
31+
from textwrap import wrap
3132

3233
from jinja2 import Environment, FileSystemLoader, TemplateNotFound, UndefinedError
3334

@@ -67,6 +68,13 @@ def camel_case(string):
6768
return "".join(w.title() for w in to_snake(string).split("_"))
6869

6970

71+
def wrap_overlong(string, width=70):
72+
"""
73+
Break an overly long string literal into escaped lines.
74+
"""
75+
return ["{0!r} \\".format(w) for w in wrap(string, width)]
76+
77+
7078
def get_tested_properties(spec):
7179
"""
7280
Get set of tested properties from spec. Include nested cases.
@@ -242,6 +250,7 @@ def generate(
242250
env = Environment(loader=loader, keep_trailing_newline=True)
243251
env.filters["to_snake"] = to_snake
244252
env.filters["camel_case"] = camel_case
253+
env.filters["wrap_overlong"] = wrap_overlong
245254
env.filters["regex_replace"] = regex_replace
246255
env.tests["error_case"] = error_case
247256
result = True

exercises/alphametics/.meta/template.j2

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
1111
{% endif %}
1212
def test_{{ description }}(self):
1313
{% set expected = case["expected"] -%}
14+
{% if value|length > 100 -%}
15+
{% for line in value | wrap_overlong(width=62) -%}
16+
{% if loop.index == 1 -%}
17+
puzzle = {{ line }}
18+
{% else -%}
19+
{{ line }}
20+
{% endif -%}
21+
{% endfor %}
22+
self.assertEqual({{ case["property"] }}(puzzle), {{ expected }})
23+
{% else -%}
1424
self.assertEqual({{ case["property"] }}("{{ value }}"), {{ expected }})
15-
25+
{% endif %}
1626
{% endfor %}
1727

1828
{{ macros.footer() }}

exercises/alphametics/alphametics_test.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,34 @@ def test_puzzle_with_ten_letters(self):
6262
# Reason to skip this test at https://github.com/exercism/python/pull/1358
6363
@unittest.skip("extra-credit")
6464
def test_puzzle_with_ten_letters_and_199_addends(self):
65+
puzzle = (
66+
"THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL"
67+
"+ A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE +"
68+
"THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER +"
69+
"THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST +"
70+
"FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE +"
71+
"TROLL + RESTS + AT + THE + HOLE + OF + LOSSES + IT + IS +"
72+
"THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER +"
73+
"SHE + SATISFIES + HER + HATE + OFF + THOSE + FEARS + A + TASTE"
74+
"+ RISES + AS + SHE + HEARS + THE + LEAST + FAR + HORSE + THOSE"
75+
"+ FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE +"
76+
"OFF + TO + THE + FOREST + THE + HORSES + THAT + ALERTS + RAISE"
77+
"+ THE + STARES + OF + THE + OTHERS + AS + THE + TROLL +"
78+
"ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF"
79+
"+ OFF + TORSO + AS + THE + LAST + HORSE + FORFEITS + ITS +"
80+
"LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS +"
81+
"THEIR + FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS +"
82+
"ARREST + AS + THE + FIRST + FATHERS + RESETTLE + THE + LAST +"
83+
"OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE"
84+
"+ FOREST + HEART + FREE + AT + LAST + OF + THE + LAST + TROLL"
85+
"+ ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS +"
86+
"FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS +"
87+
"STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL +"
88+
"SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A +"
89+
"ROOFS + FOR + THEIR + SAFE == FORTRESSES"
90+
)
6591
self.assertEqual(
66-
solve(
67-
"THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + ROOFS + FOR + THEIR + SAFE == FORTRESSES"
68-
),
92+
solve(puzzle),
6993
{
7094
"A": 1,
7195
"E": 0,

0 commit comments

Comments
 (0)