Skip to content

Commit

Permalink
Added trivial getters and functions for all FooEmpty types
Browse files Browse the repository at this point in the history
  • Loading branch information
LowLevelSubmarine committed Mar 27, 2024
1 parent 582fd64 commit b5f63d3
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 3 deletions.
28 changes: 28 additions & 0 deletions lib/src/ilist/ilist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ class IListEmpty<T> // ignore: must_be_immutable
@override
bool get isNotEmpty => false;

/// An empty list does not contain anything, by definition
@override
bool contains(covariant T? element) => false;

/// An empty list is always of length `0`, by definition
@override
int get length => 0;

/// An empty list has no first element, by definition
@override
Never get first => throw StateError("No element");

/// An empty list has no last element, by definition
@override
Never get last => throw StateError("No element");

/// An empty list has no single element, by definition
@override
Never get single => throw StateError("No element");

/// An empty list is always the reversed version of itself, by definition
@override
IListEmpty<T> get reversed => this;

/// An empty list is always the cleared version of itself, by definition
@override
IListEmpty<T> clear() => this;

@override
int get _counter => 0;

Expand Down
24 changes: 24 additions & 0 deletions lib/src/imap/imap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ class IMapEmpty<K, V> // ignore: must_be_immutable
@override
bool get isNotEmpty => false;

/// An empty map does not contain anything, by definition
@override
bool contains(K key, V value) => false;

/// An empty map does not contain anything, by definition
@override
bool containsKey(K? key) => false;

/// An empty map does not contain anything, by definition
@override
bool containsValue(V? value) => false;

/// An empty map does not contain anything, by definition
@override
bool containsEntry(MapEntry<K, V> entry) => false;

/// An empty map is always of length `0`, by definition
@override
int get length => 0;

/// An empty map is always the cleared version of itself, by definition
@override
IMapEmpty<K, V> clear() => this;

@override
int get _counter => 0;

Expand Down
24 changes: 24 additions & 0 deletions lib/src/iset/iset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ class ISetEmpty<T> // ignore: must_be_immutable
@override
bool get isNotEmpty => false;

/// An empty set does not contain anything, by definition
@override
bool contains(covariant T? element) => false;

/// An empty set is always of length `0`, by definition
@override
int get length => 0;

/// An empty set has no first element, by definition
@override
Never get first => throw StateError("No element");

/// An empty set has no last element, by definition
@override
Never get last => throw StateError("No element");

/// An empty set has no single element, by definition
@override
Never get single => throw StateError("No element");

/// An empty set is always the cleared version of itself, by definition
@override
ISetEmpty<T> clear() => this;

@override
int get _counter => 0;

Expand Down
27 changes: 26 additions & 1 deletion test/ilist/ilist_empty_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,33 @@ void main() {
expect(const IList.empty().equalItemsAndConfig(const IListEmpty()), isTrue);
});

test(".isEmpty() | .isNotEmpty()", () {
test("isEmpty | isNotEmpty", () {
expect(const IList.empty().isEmpty, isTrue);
expect(const IList.empty().isNotEmpty, isFalse);
});

test("contains", () {
expect(const IList.empty().contains(Object()), isFalse);
expect(const IList.empty().contains(null), isFalse);
});

test("length", () {
expect(const IList.empty().length, 0);
});

test("fist | last | single", () {
expect(() => const IList.empty().first, throwsStateError);
expect(() => const IList.empty().last, throwsStateError);
expect(() => const IList.empty().single, throwsStateError);
});

test("reversed", () {
const list = IList.empty();
expect(identical(list, list.reversed), isTrue);
});

test("clear()", () {
const list = IList.empty();
expect(identical(list, list.clear()), isTrue);
});
}
25 changes: 24 additions & 1 deletion test/imap/imap_empty_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,31 @@ void main() {
expect(const IMap.empty().equalItemsAndConfig(const IMapEmpty()), isTrue);
});

test(".isEmpty() | .isNotEmpty()", () {
test("isEmpty | isNotEmpty", () {
expect(const IMap.empty().isEmpty, isTrue);
expect(const IMap.empty().isNotEmpty, isFalse);
});

test("contains | -Key | -Value | -Entry", () {
expect(const IMap.empty().contains(Object(), Object()), isFalse);
expect(const IMap.empty().contains(null, null), isFalse);

expect(const IMap.empty().containsKey(Object()), isFalse);
expect(const IMap.empty().containsKey(null), isFalse);

expect(const IMap.empty().containsValue(Object()), isFalse);
expect(const IMap.empty().containsValue(null), isFalse);

expect(const IMap.empty().containsEntry(const MapEntry(Object(), Object())), isFalse);
expect(const IMap.empty().containsEntry(const MapEntry(null, null)), isFalse);
});

test("length", () {
expect(const IMap.empty().length, 0);
});

test("clear()", () {
const list = IMap.empty();
expect(identical(list, list.clear()), isTrue);
});
}
22 changes: 21 additions & 1 deletion test/iset/iset_empty_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,28 @@ void main() {
expect(const ISet.empty().equalItemsAndConfig(const ISetEmpty()), isTrue);
});

test(".isEmpty() | .isNotEmpty()", () {
test("isEmpty | isNotEmpty", () {
expect(const ISet.empty().isEmpty, isTrue);
expect(const ISet.empty().isNotEmpty, isFalse);
});

test("contains", () {
expect(const ISet.empty().contains(Object()), isFalse);
expect(const ISet.empty().contains(null), isFalse);
});

test("length", () {
expect(const ISet.empty().length, 0);
});

test("fist | last | single", () {
expect(() => const ISet.empty().first, throwsStateError);
expect(() => const ISet.empty().last, throwsStateError);
expect(() => const ISet.empty().single, throwsStateError);
});

test("clear()", () {
const list = ISet.empty();
expect(identical(list, list.clear()), isTrue);
});
}

0 comments on commit b5f63d3

Please sign in to comment.