Skip to content

Commit

Permalink
tests: add a few more tests from pyproject-metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Oct 25, 2024
1 parent bc2ab34 commit 3687e1e
Showing 1 changed file with 168 additions and 0 deletions.
168 changes: 168 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from __future__ import annotations

import email.message
import inspect
import pathlib
import textwrap

import pytest

Expand Down Expand Up @@ -916,3 +921,166 @@ def test_modern_license(self):
]

assert core_metadata.get_payload() is None

@pytest.mark.parametrize(
("items", "data"),
[
pytest.param(
[],
"",
id="empty",
),
pytest.param(
[
("Foo", "Bar"),
],
"Foo: Bar\n",
id="simple",
),
pytest.param(
[
("Foo", "Bar"),
("Foo2", "Bar2"),
],
"""\
Foo: Bar
Foo2: Bar2
""",
id="multiple",
),
pytest.param(
[
("Foo", "Unicøde"),
],
"Foo: Unicøde\n",
id="unicode",
),
pytest.param(
[
("Foo", "🕵️"),
],
"Foo: 🕵️\n",
id="emoji",
),
pytest.param(
[
("Item", None),
],
"",
id="none",
),
pytest.param(
[
("ItemA", "ValueA"),
("ItemB", "ValueB"),
("ItemC", "ValueC"),
],
"""\
ItemA: ValueA
ItemB: ValueB
ItemC: ValueC
""",
id="order 1",
),
pytest.param(
[
("ItemB", "ValueB"),
("ItemC", "ValueC"),
("ItemA", "ValueA"),
],
"""\
ItemB: ValueB
ItemC: ValueC
ItemA: ValueA
""",
id="order 2",
),
pytest.param(
[
("ItemA", "ValueA1"),
("ItemB", "ValueB"),
("ItemC", "ValueC"),
("ItemA", "ValueA2"),
],
"""\
ItemA: ValueA1
ItemB: ValueB
ItemC: ValueC
ItemA: ValueA2
""",
id="multiple keys",
),
pytest.param(
[
("ItemA", "ValueA"),
("ItemB", "ValueB1\nValueB2\nValueB3"),
("ItemC", "ValueC"),
],
"""\
ItemA: ValueA
ItemB: ValueB1
ValueB2
ValueB3
ItemC: ValueC
""",
id="multiline",
),
],
)
def test_headers(self, items: list[tuple[str, None | str]], data: str) -> None:
message = metadata.RFC822Message()

for name, value in items:
if value:
message[name] = value

data = textwrap.dedent(data) + "\n"
assert str(message) == data
assert bytes(message) == data.encode()

assert email.message_from_string(str(message)).items() == [
(a, "\n ".join(b.splitlines())) for a, b in items if b is not None
]

def test_body(self) -> None:
message = metadata.RFC822Message()

message["ItemA"] = "ValueA"
message["ItemB"] = "ValueB"
message["ItemC"] = "ValueC"
body = inspect.cleandoc(
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
congue semper fermentum. Nunc vitae tempor ante. Aenean aliquet
posuere lacus non faucibus. In porttitor congue luctus. Vivamus eu
dignissim orci. Donec egestas mi ac ipsum volutpat, vel elementum
sapien consectetur. Praesent dictum finibus fringilla. Sed vel
feugiat leo. Nulla a pharetra augue, at tristique metus.
Aliquam fermentum elit at risus sagittis, vel pretium augue congue.
Donec leo risus, faucibus vel posuere efficitur, feugiat ut leo.
Aliquam vestibulum vel dolor id elementum. Ut bibendum nunc interdum
neque interdum, vel tincidunt lacus blandit. Ut volutpat
sollicitudin dapibus. Integer vitae lacinia ex, eget finibus nulla.
Donec sit amet ante in neque pulvinar faucibus sed nec justo. Fusce
hendrerit massa libero, sit amet pulvinar magna tempor quis. ø
"""
)
headers = inspect.cleandoc(
"""
ItemA: ValueA
ItemB: ValueB
ItemC: ValueC
"""
)
full = f"{headers}\n\n{body}"

message.set_payload(textwrap.dedent(body))

assert str(message) == full

new_message = email.message_from_string(str(message))
assert new_message.items() == message.items()
assert new_message.get_payload() == message.get_payload()

assert bytes(message) == full.encode("utf-8")

0 comments on commit 3687e1e

Please sign in to comment.