Port tests/decode.js to Python - #24
Open
cjchanh wants to merge 1 commit into
Open
Conversation
Add the full contents of tests/decode.js to tests/test_decode.py: %xx decoding, invalid-sequence passthrough, the reserved-set variants, and the complete 36-case binary UTF-8 sample matrix (validity judged by a strict UTF-8 oracle that mirrors decodeURIComponent). Keep the pre-existing decode tests unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Port
tests/decode.jsto PythonCloses #2 — ports the last remaining JS test fixture in the repo into
tests/test_decode.pyas native pytest tests. No library code changes.What was ported
%xxdecoding, passthrough of invalidsequences (
%2g%z1%%), and the three reserved-set variants.SAMPLESdict isported verbatim from the JS fixture (grouping comments included), and
parametrized as
test_decode_utf8_sample_matrix, with each binary sampleas the test id, matching the JS
it(k)names. A_percent_encodehelpermirrors the JS
encodeBinaryhelper.test_decode_multi_byte,test_decode_invalid_utf8) are kept unchanged — the diff is purelyadditive. The latter also preserves the known
3 × �vsurllib.parse.unquote(2 × �) replacement-count difference.tests/decode.jsis left in place as a reference (can be dropped in afollow-up if desired).
reservedSet semantics
mdurl.decode(string, exclude=...)—excludeis the JSreservedSet(default
;/?:@&=+$,#). Percent-escapes of characters inexcludeare leftuntouched, while everything else is decoded. The ported tests exercise this
with
exclude="%"," ", and" %".The binary UTF-8 matrix
The 36 samples cover invalid leading/continuation bytes, truncated
sequences, overlong ("impossible") encodings, surrogate-range encodings, and
the U+10FFFF boundary. As in the JS test, the
True/Falsemarks aredocumentation only — upstream iterates
Object.keys(samples)and neverreads them, and the port follows suit. Each sample's validity is decided
empirically by an oracle:
decodeURIComponentthrowsURIErroron malformedUTF-8, so the port uses
urllib.parse.unquote_to_bytes(...).decode("utf-8", "strict")(raisingUnicodeDecodeError) as its equivalent. Samples the oracle rejects mustcontain
�in thedecoderesult; samples it accepts must matchdecodeexactly (and contain no�).Test commands
This mirrors the CI setup (
pip install . -r tests/requirements.txt,pytest --cov --cov-fail-under=75);tests/requirements.txtincludespytest-randomly, so the suite is order-independent. The pre-commit hooks(isort, black, flake8, mypy, docformatter, …) all pass.