|
19 | 19 | from libtmux.common import has_gte_version |
20 | 20 | from libtmux.constants import OptionScope |
21 | 21 | from libtmux.exc import OptionError |
| 22 | +from libtmux.options import convert_values |
22 | 23 | from libtmux.pane import Pane |
23 | 24 |
|
24 | 25 | if t.TYPE_CHECKING: |
@@ -1198,3 +1199,80 @@ def test_show_options_returns_expected_keys( |
1198 | 1199 | f"Expected '{key}' in {test_case.scope.name} options, " |
1199 | 1200 | f"got keys: {list(options.keys())[:10]}..." |
1200 | 1201 | ) |
| 1202 | + |
| 1203 | + |
| 1204 | +# ============================================================================= |
| 1205 | +# convert_values Tests |
| 1206 | +# ============================================================================= |
| 1207 | + |
| 1208 | + |
| 1209 | +class ConvertValuesSparseTestCase(t.NamedTuple): |
| 1210 | + """Test case for convert_values with SparseArray (via dict branch).""" |
| 1211 | + |
| 1212 | + test_id: str |
| 1213 | + initial_values: dict[int, str] # index -> value |
| 1214 | + expected_converted: dict[int, t.Any] # index -> converted value |
| 1215 | + |
| 1216 | + |
| 1217 | +CONVERT_SPARSE_TEST_CASES: list[ConvertValuesSparseTestCase] = [ |
| 1218 | + ConvertValuesSparseTestCase( |
| 1219 | + "boolean_on_off", |
| 1220 | + {0: "on", 1: "off"}, |
| 1221 | + {0: True, 1: False}, |
| 1222 | + ), |
| 1223 | + ConvertValuesSparseTestCase( |
| 1224 | + "numeric_conversion", |
| 1225 | + {0: "50", 5: "100"}, |
| 1226 | + {0: 50, 5: 100}, |
| 1227 | + ), |
| 1228 | + ConvertValuesSparseTestCase( |
| 1229 | + "mixed_values", |
| 1230 | + {0: "on", 1: "50", 2: "text"}, |
| 1231 | + {0: True, 1: 50, 2: "text"}, |
| 1232 | + ), |
| 1233 | + ConvertValuesSparseTestCase( |
| 1234 | + "sparse_indices", |
| 1235 | + {0: "on", 10: "off", 100: "42"}, |
| 1236 | + {0: True, 10: False, 100: 42}, |
| 1237 | + ), |
| 1238 | +] |
| 1239 | + |
| 1240 | + |
| 1241 | +@pytest.mark.parametrize( |
| 1242 | + "test_case", |
| 1243 | + [pytest.param(tc, id=tc.test_id) for tc in CONVERT_SPARSE_TEST_CASES], |
| 1244 | +) |
| 1245 | +def test_convert_values_sparse_array( |
| 1246 | + test_case: ConvertValuesSparseTestCase, |
| 1247 | +) -> None: |
| 1248 | + """Test convert_values handles SparseArray via dict branch. |
| 1249 | +
|
| 1250 | + Note: SparseArray inherits from dict, so the dict branch in convert_values |
| 1251 | + handles it correctly. This test verifies that behavior. |
| 1252 | + """ |
| 1253 | + sparse: SparseArray[str] = SparseArray() |
| 1254 | + for idx, val in test_case.initial_values.items(): |
| 1255 | + sparse.add(idx, val) |
| 1256 | + |
| 1257 | + result = convert_values(sparse) |
| 1258 | + |
| 1259 | + assert isinstance(result, SparseArray) |
| 1260 | + for idx, expected in test_case.expected_converted.items(): |
| 1261 | + assert result[idx] == expected, f"Index {idx}: {result[idx]} != {expected}" |
| 1262 | + |
| 1263 | + |
| 1264 | +def test_convert_values_preserves_sparse_keys() -> None: |
| 1265 | + """Test convert_values preserves sparse array indices.""" |
| 1266 | + sparse: SparseArray[str] = SparseArray() |
| 1267 | + sparse.add(0, "on") |
| 1268 | + sparse.add(5, "off") |
| 1269 | + sparse.add(99, "100") |
| 1270 | + |
| 1271 | + result = convert_values(sparse) |
| 1272 | + |
| 1273 | + # Keys should be preserved |
| 1274 | + assert isinstance(result, SparseArray) |
| 1275 | + assert sorted(result.keys()) == [0, 5, 99] |
| 1276 | + assert result[0] is True |
| 1277 | + assert result[5] is False |
| 1278 | + assert result[99] == 100 |
0 commit comments