|
| 1 | +"""Gherkin step implementations for Modernization Phase 1 (issue #29).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from behave import given, then, when |
| 8 | +from environment import scratch_dir # noqa: E402 |
| 9 | + |
| 10 | +from pptx import Presentation |
| 11 | +from pptx.enum.dml import MSO_PATTERN_TYPE |
| 12 | + |
| 13 | + |
| 14 | +# given =================================================== |
| 15 | + |
| 16 | + |
| 17 | +@given("a freshly-saved presentation at a Path") |
| 18 | +def given_freshly_saved_presentation(context): |
| 19 | + target = Path(scratch_dir) / "modernization_seed.pptx" |
| 20 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 21 | + Presentation().save(target) |
| 22 | + context.path = target |
| 23 | + |
| 24 | + |
| 25 | +@given("a fresh presentation") |
| 26 | +def given_fresh_presentation(context): |
| 27 | + context.prs = Presentation() |
| 28 | + |
| 29 | + |
| 30 | +@given("a fresh slide on a fresh presentation") |
| 31 | +def given_fresh_slide(context): |
| 32 | + prs = Presentation() |
| 33 | + context.slide = prs.slides.add_slide(prs.slide_layouts[6]) |
| 34 | + |
| 35 | + |
| 36 | +# when ==================================================== |
| 37 | + |
| 38 | + |
| 39 | +@when("I call Presentation(path) with the Path") |
| 40 | +def when_call_presentation_with_path(context): |
| 41 | + context.prs = Presentation(context.path) |
| 42 | + |
| 43 | + |
| 44 | +@when("I save it to a Path") |
| 45 | +def when_save_to_path(context): |
| 46 | + target = Path(scratch_dir) / "modernization_out.pptx" |
| 47 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 48 | + context.prs.save(target) |
| 49 | + context.saved_path = target |
| 50 | + |
| 51 | + |
| 52 | +# then ==================================================== |
| 53 | + |
| 54 | + |
| 55 | +@then("I get a presentation back") |
| 56 | +def then_presentation_back(context): |
| 57 | + assert context.prs is not None |
| 58 | + |
| 59 | + |
| 60 | +@then("a non-empty .pptx file exists at that Path") |
| 61 | +def then_non_empty_file_exists(context): |
| 62 | + assert context.saved_path.exists() |
| 63 | + assert context.saved_path.stat().st_size > 0 |
| 64 | + |
| 65 | + |
| 66 | +@then("MSO_PATTERN_TYPE.PERCENT_40 exists with xml_value pct40") |
| 67 | +def then_percent_40_correct(context): |
| 68 | + assert MSO_PATTERN_TYPE.PERCENT_40.xml_value == "pct40" |
| 69 | + |
| 70 | + |
| 71 | +@then("the broken name ERCENT_40 does not exist") |
| 72 | +def then_ercent_40_absent(context): |
| 73 | + assert hasattr(MSO_PATTERN_TYPE, "ERCENT_40") is False |
| 74 | + |
| 75 | + |
| 76 | +@then("slide.background.element local-name is bg") |
| 77 | +def then_background_element_is_bg(context): |
| 78 | + from lxml import etree |
| 79 | + |
| 80 | + bg_elm = context.slide.background.element |
| 81 | + local = etree.QName(bg_elm.tag).localname |
| 82 | + assert local == "bg", f"expected 'bg', got '{local}'" |
0 commit comments