Skip to content

Commit 7d344e7

Browse files
committed
tests(test_sparse_array): Add tests for SparseArray utility class
why: Improve code coverage for sparse_array module. what: - Add tests for is_sparse_array_list TypeGuard function - Add tests for SparseArray.append method - Add tests for SparseArray.iter_values method - Add tests for SparseArray.as_list method
1 parent 1f34b31 commit 7d344e7

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

tests/test/test_sparse_array.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
"""Tests for libtmux sparse array utilities."""
2+
3+
from __future__ import annotations
4+
5+
import typing as t
6+
7+
import pytest
8+
9+
from libtmux._internal.sparse_array import SparseArray, is_sparse_array_list
10+
11+
12+
class IsSparseArrayListTestCase(t.NamedTuple):
13+
"""Test case for is_sparse_array_list TypeGuard function."""
14+
15+
test_id: str
16+
input_dict: dict[str, t.Any]
17+
expected: bool
18+
19+
20+
class SparseArrayAppendTestCase(t.NamedTuple):
21+
"""Test case for SparseArray.append method."""
22+
23+
test_id: str
24+
initial_adds: list[tuple[int, str]] # [(index, value), ...] to setup
25+
append_values: list[str] # values to append
26+
expected_keys: list[int] # expected keys in sorted order
27+
expected_mapping: dict[int, str] # specific index -> value checks
28+
29+
30+
class SparseArrayValuesTestCase(t.NamedTuple):
31+
"""Test case for iter_values and as_list methods."""
32+
33+
test_id: str
34+
adds: list[tuple[int, str]] # [(index, value), ...]
35+
expected_values: list[str] # values in sorted index order
36+
37+
38+
IS_SPARSE_ARRAY_LIST_TEST_CASES: list[IsSparseArrayListTestCase] = [
39+
IsSparseArrayListTestCase("empty_dict", {}, True),
40+
IsSparseArrayListTestCase(
41+
"sparse_arrays_only",
42+
{"hook1": SparseArray(), "hook2": SparseArray()},
43+
True,
44+
),
45+
IsSparseArrayListTestCase(
46+
"mixed_values",
47+
{"hook1": SparseArray(), "opt": "string"},
48+
False,
49+
),
50+
IsSparseArrayListTestCase(
51+
"strings_only",
52+
{"key1": "val1", "key2": "val2"},
53+
False,
54+
),
55+
IsSparseArrayListTestCase("none_value", {"key1": None}, False),
56+
]
57+
58+
SPARSE_ARRAY_APPEND_TEST_CASES: list[SparseArrayAppendTestCase] = [
59+
SparseArrayAppendTestCase(
60+
"append_to_empty",
61+
initial_adds=[],
62+
append_values=["first"],
63+
expected_keys=[0],
64+
expected_mapping={0: "first"},
65+
),
66+
SparseArrayAppendTestCase(
67+
"append_after_add",
68+
initial_adds=[(5, "at five")],
69+
append_values=["appended"],
70+
expected_keys=[5, 6],
71+
expected_mapping={5: "at five", 6: "appended"},
72+
),
73+
SparseArrayAppendTestCase(
74+
"multiple_appends",
75+
initial_adds=[],
76+
append_values=["100", "200", "300"],
77+
expected_keys=[0, 1, 2],
78+
expected_mapping={0: "100", 1: "200", 2: "300"},
79+
),
80+
]
81+
82+
SPARSE_ARRAY_VALUES_TEST_CASES: list[SparseArrayValuesTestCase] = [
83+
SparseArrayValuesTestCase(
84+
"sorted_order",
85+
adds=[(10, "ten"), (1, "one"), (5, "five")],
86+
expected_values=["one", "five", "ten"],
87+
),
88+
SparseArrayValuesTestCase(
89+
"empty",
90+
adds=[],
91+
expected_values=[],
92+
),
93+
SparseArrayValuesTestCase(
94+
"consecutive",
95+
adds=[(3, "three"), (1, "one"), (2, "two")],
96+
expected_values=["one", "two", "three"],
97+
),
98+
]
99+
100+
101+
@pytest.mark.parametrize(
102+
"test_case",
103+
[pytest.param(tc, id=tc.test_id) for tc in IS_SPARSE_ARRAY_LIST_TEST_CASES],
104+
)
105+
def test_is_sparse_array_list(test_case: IsSparseArrayListTestCase) -> None:
106+
"""Test is_sparse_array_list TypeGuard function."""
107+
result = is_sparse_array_list(test_case.input_dict)
108+
assert result is test_case.expected
109+
110+
111+
@pytest.mark.parametrize(
112+
"test_case",
113+
[pytest.param(tc, id=tc.test_id) for tc in SPARSE_ARRAY_APPEND_TEST_CASES],
114+
)
115+
def test_sparse_array_append(test_case: SparseArrayAppendTestCase) -> None:
116+
"""Test SparseArray.append method."""
117+
arr: SparseArray[str] = SparseArray()
118+
for index, value in test_case.initial_adds:
119+
arr.add(index, value)
120+
for value in test_case.append_values:
121+
arr.append(value)
122+
assert sorted(arr.keys()) == test_case.expected_keys
123+
for index, expected_value in test_case.expected_mapping.items():
124+
assert arr[index] == expected_value
125+
126+
127+
@pytest.mark.parametrize(
128+
"test_case",
129+
[pytest.param(tc, id=tc.test_id) for tc in SPARSE_ARRAY_VALUES_TEST_CASES],
130+
)
131+
def test_sparse_array_iter_values(test_case: SparseArrayValuesTestCase) -> None:
132+
"""Test SparseArray.iter_values method."""
133+
arr: SparseArray[str] = SparseArray()
134+
for index, value in test_case.adds:
135+
arr.add(index, value)
136+
assert list(arr.iter_values()) == test_case.expected_values
137+
138+
139+
@pytest.mark.parametrize(
140+
"test_case",
141+
[pytest.param(tc, id=tc.test_id) for tc in SPARSE_ARRAY_VALUES_TEST_CASES],
142+
)
143+
def test_sparse_array_as_list(test_case: SparseArrayValuesTestCase) -> None:
144+
"""Test SparseArray.as_list method."""
145+
arr: SparseArray[str] = SparseArray()
146+
for index, value in test_case.adds:
147+
arr.add(index, value)
148+
assert arr.as_list() == test_case.expected_values

0 commit comments

Comments
 (0)