|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""``Method._missing_`` must find the hyphenated methods by value. |
| 3 | +
|
| 4 | +GitHub issue #2284. The constructor resolves case-insensitively (#896): it |
| 5 | +upper-cases the value and looks it up. It checked member *names* only, so the |
| 6 | +two RFC 3253 methods whose name differs from their value (``BASELINE_CONTROL`` |
| 7 | +/ ``'BASELINE-CONTROL'``, ``VERSION_CONTROL`` / ``'VERSION-CONTROL'``) missed, |
| 8 | +and ``Method('baseline-control')`` minted an unregistered member with |
| 9 | +``idempotent=False`` instead of returning the registered one. #908 fixed the |
| 10 | +same gap in ``Method.get``; that override stays case-sensitive and is repinned |
| 11 | +here. Strings that are no method still mint an unregistered member (#860). |
| 12 | +
|
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import inspect |
| 17 | +import unittest |
| 18 | + |
| 19 | +#: Test tier (#1958): pure-Python lookups over a generated registry, which behave |
| 20 | +#: the same on every interpreter. |
| 21 | +__tier__ = 'LATEST' |
| 22 | + |
| 23 | +#: Spellings of the two hyphenated methods, keyed by member name. |
| 24 | +SPELLINGS = { |
| 25 | + 'BASELINE_CONTROL': ('baseline-control', 'Baseline-Control', 'BASELINE-CONTROL'), |
| 26 | + 'VERSION_CONTROL': ('version-control', 'Version-Control', 'VERSION-CONTROL'), |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +class HyphenatedLookupTests(unittest.TestCase): |
| 31 | + """The constructor returns the registered member in any casing.""" |
| 32 | + |
| 33 | + def test_hyphenated_methods_resolve_in_any_case(self) -> None: |
| 34 | + """Without the fix, the lower- and mixed-case spellings mint a |
| 35 | + member named e.g. ``BASELINE-CONTROL`` with ``idempotent=False``.""" |
| 36 | + from pcapkit.const.http.method import Method |
| 37 | + |
| 38 | + for name, spellings in SPELLINGS.items(): |
| 39 | + member = Method[name] |
| 40 | + for value in spellings: |
| 41 | + with self.subTest(value=value): |
| 42 | + probed = Method(value) |
| 43 | + self.assertIs(probed, member) |
| 44 | + self.assertTrue(probed.idempotent) |
| 45 | + self.assertFalse(probed.safe) |
| 46 | + |
| 47 | + def test_name_lookup_still_folds_case(self) -> None: |
| 48 | + """#896: the constructor's name lookup stays case-insensitive.""" |
| 49 | + from pcapkit.const.http.method import Method |
| 50 | + |
| 51 | + self.assertIs(Method('get'), Method.GET) |
| 52 | + self.assertIs(Method('Get'), Method.GET) |
| 53 | + |
| 54 | + def test_get_stays_case_sensitive(self) -> None: |
| 55 | + """#908/#896: ``get`` does not fold case, so a lower-case hyphenated |
| 56 | + spelling is still a distinct, unregistered method there.""" |
| 57 | + from pcapkit.const.http.method import Method |
| 58 | + |
| 59 | + probed = Method.get('baseline-control') |
| 60 | + self.assertIsNot(probed, Method.BASELINE_CONTROL) |
| 61 | + self.assertEqual(probed.value, 'baseline-control') |
| 62 | + |
| 63 | + |
| 64 | +class UnregisteredMemberTests(unittest.TestCase): |
| 65 | + """A string matching no name or value still mints (#860).""" |
| 66 | + |
| 67 | + def test_non_method_strings_mint_an_unregistered_member(self) -> None: |
| 68 | + """The minted member keeps the caller's casing as its value, carries |
| 69 | + the upper-cased name, and is not added to either lookup table.""" |
| 70 | + from pcapkit.const.http.method import Method |
| 71 | + |
| 72 | + before = (len(Method._member_map_), len(Method._value2member_map_)) |
| 73 | + for value in ('frob', 'Frob-Control', 'baseline control'): |
| 74 | + with self.subTest(value=value): |
| 75 | + probed = Method(value) |
| 76 | + self.assertNotIn(probed, list(Method)) |
| 77 | + self.assertEqual(probed.value, value) |
| 78 | + self.assertEqual(probed.name, value.upper()) |
| 79 | + self.assertFalse(probed.safe) |
| 80 | + self.assertFalse(probed.idempotent) |
| 81 | + self.assertEqual( |
| 82 | + (len(Method._member_map_), len(Method._value2member_map_)), before) |
| 83 | + |
| 84 | + |
| 85 | +class VendorTemplateParityTests(unittest.TestCase): |
| 86 | + """The fix lives in the crawler template, so a regeneration keeps it.""" |
| 87 | + |
| 88 | + def test_the_crawler_template_carries_the_same_missing(self) -> None: |
| 89 | + """The generated ``_missing_`` appears verbatim in the rendered |
| 90 | + template, and both consult ``_value2member_map_``.""" |
| 91 | + from pcapkit.const.http.method import Method |
| 92 | + from pcapkit.vendor.http.method import LINE |
| 93 | + |
| 94 | + rendered = LINE('Method', 'HTTP Method', '<ENUM>', 'pcapkit.vendor.http.method') |
| 95 | + source = inspect.getsource(Method._missing_.__func__) # type: ignore[attr-defined] |
| 96 | + |
| 97 | + self.assertIn('cls._value2member_map_[name]', source) |
| 98 | + self.assertIn(source.rstrip('\n'), rendered) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments