Skip to content

Commit

Permalink
More tests for argument checks
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Dec 23, 2018
1 parent 6136da2 commit df0e94d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/src/collection/list_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ class EmptyList<T>
bool contains(T element) => false;

@override
bool containsAll(KCollection<T> elements) => elements.isEmpty();
bool containsAll(KCollection<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
return elements.isEmpty();
}

@override
T get(int index) {
Expand Down
6 changes: 6 additions & 0 deletions test/collection/collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,11 @@ void testCollection(KCollection<T> Function<T>() emptyCollection,
catchException<ArgumentError>(() => collection.containsAll(null));
expect(e.message, allOf(contains("null"), contains("elements")));
});

test("containsAll (empty collection) doesn't allow null as argument", () {
final e = catchException<ArgumentError>(
() => emptyCollection().containsAll(null));
expect(e.message, allOf(contains("null"), contains("elements")));
});
});
}
20 changes: 20 additions & 0 deletions test/collection/list_empty_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:test/test.dart';

import '../test/assert_dart.dart';

void main() {
group('empty list', () {
test("has no elements", () {
Expand Down Expand Up @@ -74,6 +76,18 @@ void main() {
expect(empty0, equals(empty1));
});

test("sublist doesn't allow null as fromIndex", () {
final e =
catchException<ArgumentError>(() => emptyList().subList(null, 10));
expect(e.message, allOf(contains("null"), contains("fromIndex")));
});

test("sublist doesn't allow null as toIndex", () {
final e =
catchException<ArgumentError>(() => emptyList().subList(0, null));
expect(e.message, allOf(contains("null"), contains("toIndex")));
});

test("sublist works for index 0 to 0", () {
final empty = emptyList<Object>();
final subList = empty.subList(0, 0);
Expand All @@ -98,5 +112,11 @@ void main() {
List<String> list = emptyList<String>().list;
expect(list.length, 0);
});

test("listIterator requires index", () {
ArgumentError e = catchException(() => emptyList().listIterator(null));
expect(e.message, contains("index"));
expect(e.message, contains("null"));
});
});
}
22 changes: 22 additions & 0 deletions test/collection/list_mutable_extensions_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:test/test.dart';

import '../test/assert_dart.dart';

void main() {
group('fill', () {
test("replace all elements", () {
Expand Down Expand Up @@ -28,10 +30,30 @@ void main() {
expect(result, listOf(["lisa", "paul", "john", "max"]));
});

test("sortBy doesn't allow null as argument", () {
num Function(dynamic) selector = null;
final e = catchException<ArgumentError>(
() => mutableListOf<String>()..sortBy(selector));
expect(e.message, allOf(contains("null"), contains("selector")));
});

test("sortByDescending", () {
final result = mutableListOf(["paul", "john", "max", "lisa"])
..sortByDescending(lastChar);
expect(result, listOf(["max", "john", "paul", "lisa"]));
});

test("sortByDescending doesn't allow null as argument", () {
num Function(dynamic) selector = null;
final e = catchException<ArgumentError>(
() => mutableListOf<String>()..sortByDescending(selector));
expect(e.message, allOf(contains("null"), contains("selector")));
});

test("sortWith doesn't allow null as argument", () {
final e = catchException<ArgumentError>(
() => mutableListOf<String>()..sortWith(null));
expect(e.message, allOf(contains("null"), contains("comparator")));
});
});
}

0 comments on commit df0e94d

Please sign in to comment.