|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2021-present Pycord Development |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +from collections.abc import Callable, Iterable, Iterator |
| 28 | +from typing import Literal, TypeVar |
| 29 | + |
| 30 | +import pytest |
| 31 | +from typing_extensions import TypeIs |
| 32 | + |
| 33 | +from discord.utils import find |
| 34 | + |
| 35 | +T = TypeVar("T") |
| 36 | + |
| 37 | + |
| 38 | +def is_even(x: int) -> bool: |
| 39 | + return x % 2 == 0 |
| 40 | + |
| 41 | + |
| 42 | +def always_true(_: object) -> bool: |
| 43 | + return True |
| 44 | + |
| 45 | + |
| 46 | +def greater_than_3(x: int) -> bool: |
| 47 | + return x > 3 |
| 48 | + |
| 49 | + |
| 50 | +def equals_1(x: int) -> TypeIs[Literal[1]]: |
| 51 | + return x == 1 |
| 52 | + |
| 53 | + |
| 54 | +def equals_2(x: int) -> TypeIs[Literal[2]]: |
| 55 | + return x == 2 |
| 56 | + |
| 57 | + |
| 58 | +def equals_b(c: str) -> TypeIs[Literal["b"]]: |
| 59 | + return c == "b" |
| 60 | + |
| 61 | + |
| 62 | +def equals_30(x: int) -> TypeIs[Literal[30]]: |
| 63 | + return x == 30 |
| 64 | + |
| 65 | + |
| 66 | +def is_none_pred(x: object) -> TypeIs[None]: |
| 67 | + return x is None |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + ("seq", "predicate", "expected"), |
| 72 | + [ |
| 73 | + ([], always_true, None), |
| 74 | + ([1, 2, 3], greater_than_3, None), |
| 75 | + ([1, 2, 3], equals_1, 1), |
| 76 | + ([1, 2, 3], equals_2, 2), |
| 77 | + ("abc", equals_b, "b"), |
| 78 | + ((10, 20, 30), equals_30, 30), |
| 79 | + ([None, False, 0], is_none_pred, None), |
| 80 | + ([1, 2, 3, 4], is_even, 2), |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_find_basic_parametrized( |
| 84 | + seq: Iterable[T], |
| 85 | + predicate: Callable[[T], object], |
| 86 | + expected: T | None, |
| 87 | +) -> None: |
| 88 | + result = find(predicate, seq) |
| 89 | + if expected is None: |
| 90 | + assert result is None |
| 91 | + else: |
| 92 | + assert result == expected |
| 93 | + |
| 94 | + |
| 95 | +def test_find_with_truthy_non_boolean_predicate() -> None: |
| 96 | + seq: list[int] = [2, 4, 5, 6] |
| 97 | + result = find(lambda x: x % 2, seq) |
| 98 | + assert result == 5 |
| 99 | + |
| 100 | + |
| 101 | +def test_find_on_generator_and_stop_early() -> None: |
| 102 | + def bad_gen() -> Iterator[str]: |
| 103 | + yield "first" |
| 104 | + raise RuntimeError("should not be reached") |
| 105 | + |
| 106 | + assert find(lambda x: x == "first", bad_gen()) == "first" |
| 107 | + |
| 108 | + |
| 109 | +def test_find_does_not_evaluate_rest() -> None: |
| 110 | + calls: list[str] = [] |
| 111 | + |
| 112 | + def predicate(x: str) -> bool: |
| 113 | + calls.append(x) |
| 114 | + return x == "stop" |
| 115 | + |
| 116 | + seq: list[str] = ["go", "stop", "later"] |
| 117 | + result = find(predicate, seq) |
| 118 | + assert result == "stop" |
| 119 | + assert calls == ["go", "stop"] |
| 120 | + |
| 121 | + |
| 122 | +def test_find_with_set_returns_first_iterated_element() -> None: |
| 123 | + data: set[str] = {"a", "b", "c"} |
| 124 | + result = find(lambda x: x in data, data) |
| 125 | + assert result in data |
| 126 | + |
| 127 | + |
| 128 | +def test_find_none_predicate() -> None: |
| 129 | + seq: list[int] = [42, 43, 44] |
| 130 | + result = find(lambda x: True, seq) |
| 131 | + assert result == 42 |
0 commit comments