ApprovalTests.Python — every option, chained

tests/test_options.py · test_demonstrate_available_approvaltests_options

hover a highlighted call in the code to see what it does

for_file.with_extension with_scrubber add_scrubber with_namer with_comparator with_reporter
172def test_demonstrate_available_approvaltests_options() -> None:
173    # begin-snippet: options_with_all_options
174    options = (
175        Options()
176
177        # write the .approved/.received files as Markdown instead of .txt
178        .for_file.with_extension(".md")
179
180        # uppercase the text before it is written/compared, so the effect
181        # is visible in the received/approved files themselves
182        .with_scrubber(lambda text: text.upper())
183
184        # add_scrubber stacks an additional scrubber on top of any scrubbers already
185        # added, instead of replacing them like with_scrubber would; here there's only
186        # one scrubber (from with_scrubber above). The newly added scrubber turns "!"
187        # into "!!!", which sets up the comparator below to show off what it ignores
188        .add_scrubber(lambda text: text.replace("!", "!!!"))
189
190        # use a custom namer, so the user controls the approved/received file names
191        # directly, instead of having them derived from the test's own name
192        .with_namer(UserSpecifiedFileNamer())
193
194        # use a custom comparator that ignores trailing "!"s, so the mismatch
195        # created by add_scrubber above still counts as approved
196        .with_comparator(IgnoreTrailingExclamationsComparator())
197
198        # launch Beyond Compare to review any mismatch
199        .with_reporter(ReportWithBeyondCompare())
200    )
201
202    verify("Hello, Approvals!", options=options)
203    # end-snippet
tests/approved_files/ · final file name
test_demonstrate_available_approvaltests_options_
renamed_by_user.approved.md
class UserSpecifiedFileNamer(StackFrameNamer):
    @override
    def get_file_name(self) -> str:
        return "..._renamed_by_user"
effect of .with_scrubber
in  → Hello, Approvals!
out → HELLO, APPROVALS!
effect of .add_scrubber (stacks on top)
in  → HELLO, APPROVALS!
out → HELLO, APPROVALS!!!
class IgnoreTrailingExclamationsComparator(Comparator):
    @override
    def compare(self, received_path, approved_path) -> bool:
        received = ….rstrip("!\n")
        approved = ….rstrip("!\n")
        return received == approved
demo.received.md <> demo.approved.md — Beyond Compare
HELLO, APPROVALS!!!
(scrubbed, received)
HELLO, APPROVALS!
(approved)
received  HELLO, APPROVALS!!!
approved  HELLO, APPROVALS!
IgnoreTrailingExclamationsComparator strips trailing "!"s from both before comparing — so despite add_scrubber's extra "!!!", the test PASSES