Coverage for tests / test_derivepassphrase_types / test_heavy_duty.py: 100.000%
19 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-07-06 21:34 +0200
« prev ^ index » next coverage.py v7.13.1, created at 2026-07-06 21:34 +0200
1# SPDX-FileCopyrightText: 2026 Marco Ricci <software@the13thletter.info>
2#
3# SPDX-License-Identifier: Zlib
5"""Tests for `derivepassphrase._types`: heavy-duty tests."""
7from __future__ import annotations
9import math
11import hypothesis
12from hypothesis import strategies
13from typing_extensions import Any
15from derivepassphrase import _types
16from tests.machinery import pytest as pytest_machinery
18# All tests in this module are heavy-duty tests.
19pytestmark = [pytest_machinery.heavy_duty]
22@strategies.composite
23def js_atoms_strategy(
24 draw: strategies.DrawFn,
25) -> int | float | str | bytes | bool | None:
26 """Yield a JS atom."""
27 return draw( 1b
28 strategies.one_of(
29 strategies.integers(),
30 strategies.floats(allow_nan=False, allow_infinity=False),
31 strategies.text(max_size=100),
32 strategies.binary(max_size=100),
33 strategies.booleans(),
34 strategies.none(),
35 ),
36 )
39@strategies.composite
40def js_nested_strategy(draw: strategies.DrawFn) -> Any:
41 """Yield an arbitrary and perhaps nested JS value."""
42 return draw( 1b
43 strategies.one_of(
44 js_atoms_strategy(),
45 strategies.builds(tuple),
46 strategies.builds(list),
47 strategies.builds(dict),
48 strategies.builds(set),
49 strategies.builds(frozenset),
50 strategies.recursive(
51 js_atoms_strategy(),
52 lambda s: strategies.one_of(
53 strategies.frozensets(s, max_size=100),
54 strategies.builds(
55 tuple, strategies.frozensets(s, max_size=100)
56 ),
57 ),
58 max_leaves=8,
59 ),
60 strategies.recursive(
61 js_atoms_strategy(),
62 lambda s: strategies.one_of(
63 strategies.lists(s, max_size=100),
64 strategies.dictionaries(strategies.text(max_size=100), s),
65 ),
66 max_leaves=25,
67 ),
68 ),
69 )
72@hypothesis.given(value=js_nested_strategy())
73@hypothesis.example(value=float("nan")).via("branch coverage (implementation)") 1ab
74def test_js_truthiness(value: Any) -> None:
75 """Determine the truthiness of a value according to JavaScript.
77 Use hypothesis to generate test values.
79 """
80 expected = ( 1bc
81 value is not None # noqa: PLR1714
82 and value != False # noqa: E712
83 and value != 0
84 and value != 0.0 # noqa: RUF069
85 and value != ""
86 and not (isinstance(value, float) and math.isnan(value))
87 )
88 assert _types.js_truthiness(value) == expected 1bc