Skip to content

Commit

Permalink
Merge pull request #36 from passsy/feature/check_args_in_asserts
Browse files Browse the repository at this point in the history
Validate input arguments
  • Loading branch information
passsy authored Dec 23, 2018
2 parents 4c0ae8c + df0e94d commit dac4d84
Show file tree
Hide file tree
Showing 25 changed files with 1,095 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ script:
- pub run test test/dart_kollection_test.dart
- pub global activate coverage
- pub global run coverage:collect_coverage --port=8111 -o coverage.json --resume-isolates --wait-paused &
- dart --observe=8111 test/dart_kollection_test.dart
- dart --observe=8111 --enable-asserts test/dart_kollection_test.dart
- pub global run coverage:format_coverage --packages=.packages --report-on lib --in coverage.json --out lcov.info --lcov
after_success:
- bash <(curl -s https://codecov.io/bash)
26 changes: 19 additions & 7 deletions lib/src/collection/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ class DartList<T>

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

@override
T get(int index) {
if (index == null) throw ArgumentError("index can't be null");
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
if (index < 0 || index >= size) {
throw IndexOutOfBoundsException("index: $index, size: $size");
}
Expand All @@ -63,7 +70,10 @@ class DartList<T>

@override
KListIterator<T> listIterator([int index = 0]) {
if (index == null) throw ArgumentError("index can't be null");
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
return InterOpKListIterator(_list, index);
}

Expand All @@ -72,15 +82,17 @@ class DartList<T>

@override
KList<T> subList(int fromIndex, int toIndex) {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
assert(() {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
if (fromIndex > toIndex)
throw ArgumentError("fromIndex: $fromIndex > toIndex: $toIndex");
return true;
}());
if (fromIndex < 0 || toIndex > size) {
throw IndexOutOfBoundsException(
"fromIndex: $fromIndex, toIndex: $toIndex, size: $size");
}
if (fromIndex > toIndex) {
throw ArgumentError("fromIndex: $fromIndex > toIndex: $toIndex");
}
return DartList(_list.sublist(fromIndex, toIndex));
}

Expand Down
30 changes: 24 additions & 6 deletions lib/src/collection/list_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ 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) {
if (index == null) throw ArgumentError("index can't be null");
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
throw IndexOutOfBoundsException(
"Empty list doesn't contain element at index $index.");
}

@override
T operator [](int index) {
if (index == null) throw ArgumentError("index can't be null");
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
throw IndexOutOfBoundsException(
"Empty list doesn't contain element at index $index.");
}
Expand All @@ -47,7 +59,10 @@ class EmptyList<T>

@override
KListIterator<T> listIterator([int index = 0]) {
if (index == null) throw ArgumentError("index can't be null");
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
return _EmptyIterator();
}

Expand All @@ -56,8 +71,11 @@ class EmptyList<T>

@override
KList<T> subList(int fromIndex, int toIndex) {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
assert(() {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
return true;
}());
if (fromIndex == 0 && toIndex == 0) return this;
throw IndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex");
}
Expand Down
55 changes: 48 additions & 7 deletions lib/src/collection/list_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ class DartMutableList<T>

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

@override
T get(int index) {
if (index == null) throw ArgumentError("index can't be null");
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
if (index < 0 || index >= size) {
throw IndexOutOfBoundsException("index: $index, size: $size");
}
Expand Down Expand Up @@ -81,18 +88,31 @@ class DartMutableList<T>

@override
bool addAll(KIterable<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
_list.addAll(elements.iter);
return true;
}

@override
bool addAllAt(int index, KCollection<T> elements) {
assert(() {
if (index == null) throw ArgumentError("index can't be null");
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
_list.insertAll(index, elements.iter);
return true;
}

@override
void addAt(int index, T element) {
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
_list.insert(index, element);
}

Expand All @@ -103,10 +123,20 @@ class DartMutableList<T>
bool remove(T element) => _list.remove(element);

@override
T removeAt(int index) => _list.removeAt(index);
T removeAt(int index) {
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
return _list.removeAt(index);
}

@override
T set(int index, T element) {
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
final old = _list[index];
_list[index] = element;
return old;
Expand All @@ -117,6 +147,10 @@ class DartMutableList<T>

@override
bool removeAll(KIterable<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
var changed = false;
for (var value in elements.iter) {
changed |= _list.remove(value);
Expand All @@ -126,21 +160,28 @@ class DartMutableList<T>

@override
bool retainAll(KIterable<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
_list.removeWhere((it) => !elements.contains(it));
return true;
}

@override
KMutableList<T> subList(int fromIndex, int toIndex) {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
assert(() {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
if (fromIndex > toIndex) {
throw ArgumentError("fromIndex: $fromIndex > toIndex: $toIndex");
}
return true;
}());
if (fromIndex < 0 || toIndex > size) {
throw IndexOutOfBoundsException(
"fromIndex: $fromIndex, toIndex: $toIndex, size: $size");
}
if (fromIndex > toIndex) {
throw ArgumentError("fromIndex: $fromIndex > toIndex: $toIndex");
}
return DartMutableList(_list.sublist(fromIndex, toIndex));
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/collection/map_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class DartMutableMap<K, V>

@override
void putAll(KMap<K, V> from) {
assert(() {
if (from == null) throw ArgumentError("from can't be null");
return true;
}());
for (var entry in from.entries.iter) {
_map[entry.key] = entry.value;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/collection/set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class DartSet<T>

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

Expand Down
8 changes: 7 additions & 1 deletion lib/src/collection/set_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class EmptySet<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
bool isEmpty() => true;
Expand Down
16 changes: 16 additions & 0 deletions lib/src/collection/set_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class DartMutableSet<T>

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

Expand Down Expand Up @@ -67,6 +71,10 @@ class DartMutableSet<T>

@override
bool addAll(KIterable<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
var oldSize = size;
_set.addAll(elements.iter);
return size != oldSize;
Expand All @@ -80,6 +88,10 @@ class DartMutableSet<T>

@override
bool removeAll(KIterable<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
final oldSize = size;
for (var value in elements.iter) {
_set.remove(value);
Expand All @@ -89,6 +101,10 @@ class DartMutableSet<T>

@override
bool retainAll(KIterable<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
final oldSize = size;
_set.removeWhere((it) => !elements.contains(it));
return oldSize != size;
Expand Down
Loading

0 comments on commit dac4d84

Please sign in to comment.