File: test_count_parameters.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (57 lines) | stat: -rw-r--r-- 1,377 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from functools import partial

from textual._callback import count_parameters


def test_functions() -> None:
    """Test count parameters of functions."""

    def foo(): ...
    def bar(a): ...
    def baz(a, b): ...

    # repeat to allow for caching
    for _ in range(3):
        assert count_parameters(foo) == 0
        assert count_parameters(bar) == 1
        assert count_parameters(baz) == 2


def test_methods() -> None:
    """Test count parameters of methods."""

    class Foo:
        def foo(self): ...
        def bar(self, a): ...
        def baz(self, a, b): ...

    foo = Foo()

    # repeat to allow for caching
    for _ in range(3):
        assert count_parameters(foo.foo) == 0
        assert count_parameters(foo.bar) == 1
        assert count_parameters(foo.baz) == 2


def test_partials() -> None:
    """Test count parameters of partials."""

    class Foo:
        def method(self, a, b, c, d): ...

    foo = Foo()

    partial0 = partial(foo.method)
    partial1 = partial(foo.method, 10)
    partial2 = partial(foo.method, b=10, c=20)

    for _ in range(3):
        assert count_parameters(partial0) == 4
        assert count_parameters(partial0) == 4

        assert count_parameters(partial1) == 3
        assert count_parameters(partial1) == 3

        assert count_parameters(partial2) == 2
        assert count_parameters(partial2) == 2