Skip to content

Commit 67d5fe9

Browse files
committed
Finish doc and tests for sq_ass_item.
1 parent ffdf228 commit 67d5fe9

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed

doc/sphinx/source/new_types.rst

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,13 +1029,132 @@ In ``src/cpy/Object/cSeqObject.c``:
10291029
Tests
10301030
--------------
10311031

1032-
Tests are in ``tests/unit/test_c_seqobject.py`` which includes failure modes:
1032+
Tests are in ``tests/unit/test_c_seqobject.py`` which includes failure modes.
1033+
First setting a value:
10331034

10341035
.. code-block:: python
10351036
10361037
from cPyExtPatt import cSeqObject
10371038
1038-
pass
1039+
@pytest.mark.parametrize(
1040+
'initial_sequence, index, value, expected',
1041+
(
1042+
(
1043+
[7, 4, 1, ], 0, 14, [14, 4, 1, ],
1044+
),
1045+
(
1046+
[7, 4, 1, ], -1, 14, [7, 4, 14, ],
1047+
),
1048+
(
1049+
[7, 4, 1, ], -2, 14, [7, 14, 1, ],
1050+
),
1051+
(
1052+
[7, 4, 1, ], -3, 14, [14, 4, 1, ],
1053+
),
1054+
)
1055+
)
1056+
def test_SequenceLongObject_setitem(initial_sequence, index, value, expected):
1057+
obj = cSeqObject.SequenceLongObject(initial_sequence)
1058+
obj[index] = value
1059+
assert list(obj) == expected
1060+
1061+
1062+
Setting a value with an out of range index:
1063+
1064+
.. code-block:: python
1065+
1066+
from cPyExtPatt import cSeqObject
1067+
1068+
@pytest.mark.parametrize(
1069+
'initial_sequence, index, expected',
1070+
(
1071+
(
1072+
[7, 4, 1, ], 3, 'Index 3 is out of range for length 3',
1073+
),
1074+
(
1075+
[7, 4, 1, ], -4, 'Index -4 is out of range for length 3',
1076+
),
1077+
)
1078+
)
1079+
def test_SequenceLongObject_setitem_raises(initial_sequence, index, expected):
1080+
print()
1081+
print(initial_sequence, index, expected)
1082+
obj = cSeqObject.SequenceLongObject(initial_sequence)
1083+
with pytest.raises(IndexError) as err:
1084+
obj[index] = 100
1085+
print(list(obj))
1086+
assert err.value.args[0] == expected
1087+
1088+
1089+
Deleting a value:
1090+
1091+
.. code-block:: python
1092+
1093+
from cPyExtPatt import cSeqObject
1094+
1095+
@pytest.mark.parametrize(
1096+
'initial_sequence, index, expected',
1097+
(
1098+
(
1099+
[7, ], 0, [],
1100+
),
1101+
(
1102+
[7, ], -1, [],
1103+
),
1104+
(
1105+
[7, 4, 1, ], 1, [7, 1, ],
1106+
),
1107+
(
1108+
[7, 4, ], 0, [4, ],
1109+
),
1110+
(
1111+
[7, 4, 1, ], -1, [7, 4, ],
1112+
),
1113+
(
1114+
[7, 4, 1, ], -2, [7, 1, ],
1115+
),
1116+
(
1117+
[7, 4, 1, ], -3, [4, 1, ],
1118+
),
1119+
)
1120+
)
1121+
def test_SequenceLongObject_delitem(initial_sequence, index, expected):
1122+
obj = cSeqObject.SequenceLongObject(initial_sequence)
1123+
del obj[index]
1124+
assert list(obj) == expected
1125+
1126+
1127+
Deleting a value with an out of range index:
1128+
1129+
.. code-block:: python
1130+
1131+
from cPyExtPatt import cSeqObject
1132+
1133+
@pytest.mark.parametrize(
1134+
'initial_sequence, index, expected',
1135+
(
1136+
(
1137+
[], 0, 'Index 0 is out of range for length 0',
1138+
),
1139+
(
1140+
[], -1, 'Index -1 is out of range for length 0',
1141+
),
1142+
(
1143+
[7, ], 1, 'Index 1 is out of range for length 1',
1144+
),
1145+
(
1146+
[7, ], -3, 'Index -3 is out of range for length 1',
1147+
),
1148+
)
1149+
)
1150+
def test_SequenceLongObject_delitem_raises(initial_sequence, index, expected):
1151+
print()
1152+
print(initial_sequence, index, expected)
1153+
obj = cSeqObject.SequenceLongObject(initial_sequence)
1154+
print(list(obj))
1155+
with pytest.raises(IndexError) as err:
1156+
del obj[index]
1157+
assert err.value.args[0] == expected
10391158
10401159
10411160

tests/unit/test_c_seqobject.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ def test_SequenceLongObject_item_raises(initial_sequence, index, expected):
194194
(
195195
[7, 4, 1, ], -1, 14, [7, 4, 14, ],
196196
),
197+
(
198+
[7, 4, 1, ], -2, 14, [7, 14, 1, ],
199+
),
200+
(
201+
[7, 4, 1, ], -3, 14, [14, 4, 1, ],
202+
),
197203
)
198204
)
199205
def test_SequenceLongObject_setitem(initial_sequence, index, value, expected):
@@ -238,6 +244,15 @@ def test_SequenceLongObject_setitem_raises(initial_sequence, index, expected):
238244
(
239245
[7, 4, ], 0, [4, ],
240246
),
247+
(
248+
[7, 4, 1, ], -1, [7, 4, ],
249+
),
250+
(
251+
[7, 4, 1, ], -2, [7, 1, ],
252+
),
253+
(
254+
[7, 4, 1, ], -3, [4, 1, ],
255+
),
241256
)
242257
)
243258
def test_SequenceLongObject_delitem(initial_sequence, index, expected):

0 commit comments

Comments
 (0)