Skip to content

Add pytest coverage for the parse API, Post protocol, handlers and util (79 tests) - #152

Closed
wxs-lang wants to merge 1 commit into
eyeseast:mainfrom
wxs-lang:main
Closed

wxs-lang wants to merge 1 commit into
eyeseast:mainfrom
wxs-lang:main

Conversation

@wxs-lang

Copy link
Copy Markdown

The existing suite (tests/unit_test.py, fixture files and doctests) covers loading the bundled examples and load → dump → load round trips. This PR adds pytest coverage for the parts of the public API that had none, with all assertions taken from the library's current behaviour:

  • frontmatter/util.py (previously untested) — u() normalises bytes/str to unicode and CRLF → LF, and stringifies non-string input; is_readable / is_writable / can_open classification of file-like objects, paths and other values
  • parse() semantics — the (metadata, content) tuple, keyword defaults being used and overridden by front matter, non-dict YAML front matter (lists/scalars) being silently ignored, unclosed or non-leading front matter treated as plain text, and a handler whose split raises ValueError bailing out to defaults + untouched text
  • check / checks / detect_format — string inputs, file-like objects, detection order (YAML → JSON → TOML), and unopenable argument types
  • Error paths — load/dump with an unopenable argument raise ValueError; invalid YAML/JSON/TOML front matter propagates the underlying parser errors instead of being swallowed
  • YAML specifics — scalar type coercion (int/float/bool/null/list/nested map), unquoted dates becoming datetime.date, quoted numbers staying strings, anchors/aliases, delimiter variants (----, trailing spaces), later --- lines staying in the content, export key sorting, and the sort_keys=False passthrough in dumps
  • JSON & TOML handlers — detection and round trips with nested structures, TOML datetimes and tables, JSON export indentation/key order, and handler override in dumps changing the output format
  • Post — the full metadata mapping protocol ([], in, set/delete, get, keys, values), to_dict() returning a copy with content added, str/bytes, and the handler fallback to YAML when post.handler is None
  • Custom handlers — a real BaseHandler subclass (a ;;;-delimited key = value format) driven end to end, registration in frontmatter.handlers, detection precedence, and BaseHandler raising NotImplementedError without a boundary

Current-behaviour quirks pinned by tests (not changed):

  • A one-line JSON document ({"a": 1}) is not detected as front matter, because the JSON handler's boundary only matches a line that is exactly { or }
  • frontmatter.check() on a missing file raises FileNotFoundError instead of returning False
  • Dumping an empty-metadata post produces ---\n{}\n--- (YAML), {} plus an extra blank line from the shared template (JSON), and +++\n\n+++ (TOML)
  • Post(None).content is the string "None" (content is coerced with str())

All tests live in new files under the existing tests/ directory; no production code is changed. The TOML tests skip cleanly if the optional toml package is not installed. No new conftest.py is needed: the package is installed by the project's test setup (uv sync / the test extra, which also provides toml).

Verification (Python 3.12, PyYAML + toml installed):

$ python -m pytest tests/test_util.py tests/test_parse_api.py tests/test_yaml_format.py tests/test_json_toml_formats.py tests/test_post_behavior.py tests/test_custom_handlers.py -q
79 passed
$ python -m pytest . --doctest-modules --doctest-glob "README.md" -q   # full suite as CI runs it
146 passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants