Logger class
A convenient way to verify that a set of operations executed in a specific order. Simply inject the Logger into each operation and call:
operation1(Logger logger) => logger('foo');
operation2(Logger logger) => logger('bar');
Then in the test:
expect(logger).toEqual(['foo', 'bar']);
class Logger implements List { final List tokens = []; /** * Add string token to the list. */ call(dynamic text) => tokens.add(text); /** * Return a `;` separated list of recorded tokens. */ String result() => tokens.join('; '); noSuchMethod(Invocation invocation) => mirror.reflect(tokens).delegate(invocation); }
Implements
Properties
final E first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
E get first;
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty;
final bool isNotEmpty #
Returns true if there is at least one element in this collection.
bool get isNotEmpty;
final Iterator<E> iterator #
Returns an Iterator that iterates over this Iterable object.
Iterator<E> get iterator;
abstract int get length #
Returns the number of objects in this list.
The valid indices for a list are 0
through length - 1
.
abstract void set length(int newLength) #
Changes the length of this list.
If
newLength is greater than
the current length, entries are initialized to null
.
Throws an UnsupportedError if the list is fixed-length.
final Iterable<E> reversed #
Returns an Iterable of the objects in this list in reverse order.
Iterable<E> get reversed;
final E single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError.
E get single;
final List tokens #
final List tokens = []
Operators
abstract E operator [](int index) #
Returns the object at the given index in the list or throws a RangeError if index is out of bounds.
abstract void operator []=(int index, E value) #
Sets the value at the given index in the list to value or throws a RangeError if index is out of bounds.
Methods
abstract void add(E value) #
Adds value to the end of this list, extending the length by one.
Throws an UnsupportedError if the list is fixed-length.
abstract void addAll(Iterable<E> iterable) #
Appends all objects of iterable to the end of this list.
Extends the length of the list by the number of objects in iterable. Throws an UnsupportedError if this list is fixed-length.
abstract bool any(bool test(E element)) #
Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.
abstract Map<int, E> asMap() #
Returns an unmodifiable Map view of this
.
The map uses the indices of this list as keys and the corresponding objects
as values. The Map.keys
Iterable iterates the indices of this list
in numerical order.
List<String> words = ['fee', 'fi', 'fo', 'fum'];
Map<int, String> map = words.asMap();
map[0] + map[1]; // 'feefi';
map.keys.toList(); // [0, 1, 2, 3]
dynamic call(text) #
Add string token to the list.
call(dynamic text) => tokens.add(text);
abstract void clear() #
Removes all objects from this list; the length of the list becomes zero.
Throws an UnsupportedError, and retains all objects, if this is a fixed-length list.
abstract bool contains(Object element) #
Returns true if the collection contains an element equal to element.
abstract E elementAt(int index) #
Returns the indexth element.
If this
has fewer than
index elements throws a RangeError.
Note: if this
does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this
.
abstract bool every(bool test(E element)) #
Returns true if every elements of this collection satisify the
predicate
test. Returns false
otherwise.
abstract void fillRange(int start, int end, [E fillValue]) #
Sets the objects in the range start inclusive to end exclusive to the given fillValue.
An error occurs if
start..
end is not a valid range for this
.
abstract E firstWhere(bool test(E element), {E orElse()}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
abstract dynamic fold(initialValue, combine(previousValue, E element)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
abstract void forEach(void f(E element)) #
Applies the function f to each element of this collection.
abstract Iterable<E> getRange(int start, int end) #
Returns an Iterable that iterates over the objects in the range start inclusive to end exclusive.
An error occurs if end is before start.
An error occurs if the
start and
end are not valid ranges at the time
of the call to this method. The returned Iterable behaves like
skip(start).take(end - start)
. That is, it does not throw exceptions
if this
changes size.
List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
Iterable<String> range = colors.getRange(1, 4);
range.join(', '); // 'green, blue, orange'
colors.length = 3;
range.join(', '); // 'green, blue'
abstract int indexOf(E element, [int start = 0]) #
Returns the first index of element in this list.
Searches the list from index
start to the end of the list.
The first time an object o
is encountered so that o == element
,
the index of o
is returned.
List<String> notes = ['do', 're', 'mi', 're'];
notes.indexOf('re'); // 1
notes.indexOf('re', 2); // 3
Returns -1 if element is not found.
notes.indexOf('fa'); // -1
abstract void insert(int index, E element) #
Inserts the object at position index in this list.
This increases the length of the list by one and shifts all objects at or after the index towards the end of the list.
An error occurs if the index is less than 0 or greater than length. An UnsupportedError occurs if the list is fixed-length.
abstract void insertAll(int index, Iterable<E> iterable) #
Inserts all objects of iterable at position index in this list.
This increases the length of the list by the length of iterable and shifts all later objects towards the end of the list.
An error occurs if the index is less than 0 or greater than length. An UnsupportedError occurs if the list is fixed-length.
String join([String separator = ""]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.
String join([String separator = ""]) { StringBuffer buffer = new StringBuffer(); buffer.writeAll(this, separator); return buffer.toString(); }
abstract int lastIndexOf(E element, [int start]) #
Returns the last index of element in this list.
Searches the list backwards from index start to 0.
The first time an object o
is encountered so that o == element
,
the index of o
is returned.
List<String> notes = ['do', 're', 'mi', 're'];
notes.lastIndexOf('re', 2); // 1
If start is not provided, this method searches from the end of the list./Returns
notes.lastIndexOf('re'); // 3
Returns -1 if element is not found.
notes.lastIndexOf('fa'); // -1
abstract E lastWhere(bool test(E element), {E orElse()}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
abstract Iterable map(f(E element)) #
Returns a lazy Iterable where each element e
of this
is replaced
by the result of f(e)
.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
dynamic noSuchMethod(Invocation invocation) #
noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod in an Invocation. If noSuchMethod returns a value, that value becomes the result of the original invocation.
The default behavior of noSuchMethod is to throw a
noSuchMethodError
.
noSuchMethod(Invocation invocation) => mirror.reflect(tokens).delegate(invocation);
abstract E reduce(E combine(E value, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
abstract bool remove(Object value) #
Removes the first occurence of value from this list.
Returns true if value was in the list, false otherwise.
List<String> parts = ['head', 'shoulders', 'knees', 'toes'];
parts.remove('head'); // true
parts.join(', '); // 'shoulders, knees, toes'
The method has no effect if value was not in the list.
// Note: 'head' has already been removed.
parts.remove('head'); // false
parts.join(', '); // 'shoulders, knees, toes'
An UnsupportedError occurs if the list is fixed-length.
abstract E removeAt(int index) #
Removes the object at position index from this list.
This method reduces the length of this
by one and moves all later objects
down by one position.
Returns the removed object.
- Throws an ArgumentError if index is not an int.
- Throws a RangeError if the index is out of range for this list.
-
Throws an UnsupportedError, and doesn't remove the object, if this is a fixed-length list.
abstract E removeLast() #
Pops and returns the last object in this list.
Throws an UnsupportedError if this is a fixed-length list.
abstract void removeRange(int start, int end) #
Removes the objects in the range start inclusive to end exclusive.
An error occurs if
start..
end is not a valid range for this
.
Throws an UnsupportedError if this is a fixed-length list.
abstract void removeWhere(bool test(E element)) #
Removes all objects from this list that satisfy test.
An object o
satisfies
test if test(o)
is true.
List<String> numbers = ['one', 'two', 'three', 'four'];
numbers.removeWhere((item) => item.length == 3);
numbers.join(', '); // 'three, four'
Throws an UnsupportedError if this is a fixed-length list.
abstract void replaceRange(int start, int end, Iterable<E> iterable) #
Removes the objects in the range start inclusive to end exclusive and replaces them with the contents of the iterable.
List<int> list = [1, 2, 3, 4];
list.replaceRange(1, 3, [6, 7]);
list.join(', '); // '1, 6, 7, 4'
An error occurs if
start..
end is not a valid range for this
.
String result() #
Return a ;
separated list of recorded tokens.
String result() => tokens.join('; ');
abstract void retainWhere(bool test(E element)) #
Removes all objects from this list that fail to satisfy test.
An object o
satisfies
test if test(o)
is true.
List<String> numbers = ['one', 'two', 'three', 'four'];
numbers.retainWhere((item) => item.length == 3);
numbers.join(', '); // 'one, two'
Throws an UnsupportedError if this is a fixed-length list.
abstract void setAll(int index, Iterable<E> iterable) #
Overwrites objects of this
with the objects of
iterable, starting
at position
index in this list.
List<String> list = ['a', 'b', 'c'];
list.setAll(1, ['bee', 'sea']);
list.join(', '); // 'a, bee, sea'
This operation does not increase the length of this
.
An error occurs if the index is less than 0 or greater than length. An error occurs if the iterable is longer than length - index.
abstract void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) #
Copies the objects of
iterable, skipping
skipCount objects first,
into the range
start inclusive to
end exclusive of this
.
List<int> list1 = [1, 2, 3, 4];
List<int> list2 = [5, 6, 7, 8, 9];
// Copies the 4th and 5th items in list2 as the 2nd and 3rd items
// of list1.
list1.setRange(1, 3, list2, 3);
list1.join(', '); // '1, 8, 9, 4'
If start equals end and start.. end represents a legal range, this method has no effect.
An error occurs if
start..
end is not a valid range for this
.
An error occurs if the
iterable does not have enough objects after
skipping
skipCount objects.
abstract E singleWhere(bool test(E element)) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
abstract Iterable<E> skip(int n) #
Returns an Iterable that skips the first n elements.
If this
has fewer than
n elements, then the resulting Iterable is
empty.
It is an error if n is negative.
abstract Iterable<E> skipWhile(bool test(E value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable iterates over all elements of this
.
As long as the iterator's elements satisfy
test they are
discarded. Once an element does not satisfy the
test the iterator stops
testing and uses every later element unconditionally. That is, the elements
of the returned Iterable are the elements of this
starting from the
first element that does not satisfy
test.
abstract void sort([int compare(E a, E b)]) #
Sorts this list according to the order specified by the compare function.
The compare function must act as a Comparator.
List<String> numbers = ['one', 'two', 'three', 'four'];
// Sort from shortest to longest.
numbers.sort((x, y) => x.length.compareTo(y.length));
numbers.join(', '); // 'one, two, four, three'
The default List implementations use Comparable.compare if compare is omitted.
List<int> nums = [13, 2, -11];
nums.sort();
nums.join(', '); // '-11, 2, 13'
abstract List<E> sublist(int start, [int end]) #
Returns a new list containing the objects from start inclusive to end exclusive.
List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
colors.sublist(1, 3); // ['green', 'blue']
If
end is omitted, the length of this
is used.
colors.sublist(1); // ['green', 'blue', 'orange', 'pink']
An error occurs if
start is outside the range 0
.. length
or if
end is outside the range start
.. length
.
abstract Iterable<E> takeWhile(bool test(E value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable starts iterating over the elements of this
.
When the iterator encounters an element e
that does not satisfy
test,
it discards e
and moves into the finished state. That is, it does not
get or provide any more elements.
abstract Iterable<E> where(bool test(E element)) #
Returns a lazy Iterable with all elements that satisfy the predicate test.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function test will not be invoked. Iterating will not cache results, and thus iterating multiple times over the returned Iterable will invoke the supplied function test multiple times on the same element.