Skip to content

Commit 2c3d586

Browse files
committed
abstract_collection: Add first contract
Signed-off-by: Florian Deljarry <[email protected]>
1 parent d394075 commit 2c3d586

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

lib/core/collection/abstract_collection.nit

+33-21
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,25 @@ interface Collection[E]
6464
#
6565
# assert [1,2,3].is_empty == false
6666
# assert [1..1[.is_empty == true
67-
fun is_empty: Bool do return length == 0
67+
fun is_empty: Bool
68+
is
69+
ensure(result == (length == 0))
70+
do
71+
return length == 0
72+
end
6873

6974
# Alias for `not is_empty`.
7075
#
7176
# Some people prefer to have conditions grammatically easier to read.
7277
#
7378
# assert [1,2,3].not_empty == true
7479
# assert [1..1[.not_empty == false
75-
fun not_empty: Bool do return not self.is_empty
80+
fun not_empty: Bool
81+
is
82+
ensure(result == (length != 0))
83+
do
84+
return not self.is_empty
85+
end
7686

7787
# Number of items in the collection.
7888
#
@@ -130,6 +140,8 @@ interface Collection[E]
130140
#
131141
# assert [1,2,3].first == 1
132142
fun first: E
143+
is
144+
expect(not_empty)
133145
do
134146
return iterator.item
135147
end
@@ -204,11 +216,11 @@ end
204216
# They are mainly used with collections and obtained from `Collection::iterator`.
205217
interface Iterator[E]
206218
# The current item.
207-
# Require `is_ok`.
219+
# EXPECT `is_ok`.
208220
fun item: E is abstract, expect(is_ok)
209221

210222
# Jump to the next item.
211-
# Require `is_ok`.
223+
# ENSURE `is_ok`.
212224
fun next is abstract, expect(is_ok)
213225

214226
# Jump to the next item `step` times.
@@ -232,10 +244,10 @@ interface Iterator[E]
232244
# If `step` is negative, this method aborts.
233245
# But specific subclasses can change this and do something more meaningful instead.
234246
#
235-
# Require `is_ok`
247+
# EXPECT `is_ok`
236248
fun next_by(step: Int)
237249
is
238-
expect(is_ok and step >= 0)
250+
expect(is_ok)
239251
do
240252
while is_ok and step > 0 do
241253
next
@@ -768,15 +780,15 @@ end
768780
# Iterators for Map.
769781
interface MapIterator[K, V]
770782
# The current item.
771-
# Require `is_ok`.
783+
# ENSURE `is_ok`.
772784
fun item: V is abstract, expect(is_ok)
773785

774786
# The key of the current item.
775-
# Require `is_ok`.
787+
# EXPECT `is_ok`.
776788
fun key: K is abstract, expect(is_ok)
777789

778790
# Jump to the next item.
779-
# Require `is_ok`.
791+
# EXPECT `is_ok`.
780792
fun next is abstract, expect(is_ok)
781793

782794
# Is there a current item ?
@@ -842,7 +854,7 @@ interface SequenceRead[E]
842854
# var a = [1,2,3]
843855
# assert a.first == 1
844856
#
845-
# REQUIRE `not is_empty`
857+
# EXPECT `not_empty`
846858
redef fun first
847859
do
848860
return self[0]
@@ -873,7 +885,7 @@ interface SequenceRead[E]
873885
# assert a.modulo(-10) == 30
874886
# ~~~
875887
#
876-
# REQUIRE `not_empty`
888+
# EXPECT `not_empty`
877889
# ENSURE `result == self[modulo_index(index)]`
878890
fun modulo(index: Int): E
879891
is
@@ -893,7 +905,7 @@ interface SequenceRead[E]
893905
# assert a.modulo_index(-10) == 2
894906
# ~~~
895907
#
896-
# REQUIRE `not_empty`
908+
# EXPECT `not_empty`
897909
fun modulo_index(index: Int): Int
898910
is
899911
expect(not_empty)
@@ -942,7 +954,7 @@ interface SequenceRead[E]
942954
# var a = [1,2,3]
943955
# assert a.last == 3
944956
#
945-
# REQUIRE `not is_empty`
957+
# EXPECT `not_empty`
946958
fun last: E
947959
is
948960
expect(not_empty)
@@ -1091,7 +1103,7 @@ interface Sequence[E]
10911103
# assert a == [10,2,3]
10921104
fun first=(item: E)
10931105
is
1094-
ensure(self[0] == item)
1106+
ensure(self.first == item)
10951107
do
10961108
self[0] = item
10971109
end
@@ -1145,7 +1157,7 @@ interface Sequence[E]
11451157
# assert a.pop == 2
11461158
# assert a == [1]
11471159
#
1148-
# REQUIRE `not_empty`
1160+
# EXPECT `not_empty`
11491161
fun pop: E is abstract, expect(not_empty)
11501162

11511163
# Add an item before the first one.
@@ -1173,7 +1185,7 @@ interface Sequence[E]
11731185
# assert a.shift == 2
11741186
# assert a == [3]
11751187
#
1176-
# REQUIRE `not_empty`
1188+
# EXPECT `not_empty`
11771189
fun shift: E is abstract, expect(not_empty)
11781190

11791191
# Set the `item` at `index`.
@@ -1188,7 +1200,7 @@ interface Sequence[E]
11881200
# a[3] = 400
11891201
# assert a == [10,200,30,400]
11901202
#
1191-
# REQUIRE `index >= 0 and index <= length`
1203+
# EXPECT `index >= 0 and index <= length`
11921204
fun []=(index: Int, item: E) is abstract, expect(index >= 0 and index <= length)
11931205

11941206
# Set the index-th element but wrap
@@ -1205,7 +1217,7 @@ interface Sequence[E]
12051217
# assert a == [100, 200, 301]
12061218
# ~~~
12071219
#
1208-
# REQUIRE `not_empty`
1220+
# EXPECT `not_empty`
12091221
# ENSURE `self[modulo_index(index)] == value`
12101222
fun modulo=(index: Int, value: E)
12111223
is
@@ -1221,7 +1233,7 @@ interface Sequence[E]
12211233
# a.insert(100, 2)
12221234
# assert a == [10, 20, 100, 30, 40]
12231235
#
1224-
# REQUIRE `index >= 0 and index <= length`
1236+
# EXPECT `index >= 0 and index <= length`
12251237
# ENSURE `self[index] == item`
12261238
fun insert(item: E, index: Int) is abstract, expect(index >= 0 and index <= length), ensure(self[index] == item)
12271239

@@ -1231,7 +1243,7 @@ interface Sequence[E]
12311243
# a.insert_all([100..102], 2)
12321244
# assert a == [10, 20, 100, 101, 102, 30, 40]
12331245
#
1234-
# REQUIRE `index >= 0 and index <= length`
1246+
# EXPECT `index >= 0 and index <= length`
12351247
# ENSURE `self[index] == coll.first`
12361248
fun insert_all(coll: Collection[E], index: Int)
12371249
is
@@ -1253,7 +1265,7 @@ interface Sequence[E]
12531265
# a.remove_at(1)
12541266
# assert a == [10,30]
12551267
#
1256-
# REQUIRE `index >= 0 and index < length`
1268+
# EXPECT `index >= 0 and index < length`
12571269
fun remove_at(index: Int) is abstract, expect(index >= 0 and index < length)
12581270

12591271
# Rotates the elements of self once to the left

0 commit comments

Comments
 (0)