Dart Documentationangular.mockLogger

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

List

Properties

final E first #

inherited from Iterable

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 #

inherited from Iterable

Returns true if there is no element in this collection.

bool get isEmpty;

final bool isNotEmpty #

inherited from Iterable

Returns true if there is at least one element in this collection.

bool get isNotEmpty;

final Iterator<E> iterator #

inherited from Iterable

Returns an Iterator that iterates over this Iterable object.

Iterator<E> get iterator;

final E last #

inherited from Iterable

Returns the last element.

If this is empty throws a StateError.

E get last;

abstract int get length #

inherited from List

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) #

inherited from List

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 #

inherited from List

Returns an Iterable of the objects in this list in reverse order.

Iterable<E> get reversed;

final E single #

inherited from Iterable

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) #

inherited from List

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) #

inherited from List

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) #

inherited from List

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) #

inherited from List

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)) #

inherited from Iterable

Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.

abstract Map<int, E> asMap() #

inherited from List

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() #

inherited from List

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) #

inherited from Iterable

Returns true if the collection contains an element equal to element.

abstract E elementAt(int index) #

inherited from Iterable

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)) #

inherited from Iterable

Returns true if every elements of this collection satisify the predicate test. Returns false otherwise.

abstract Iterable expand(Iterable f(E element)) #

inherited from Iterable

Expands each element of this Iterable into zero or more elements.

The resulting Iterable runs through the elements returned by f for each element of this, in order.

The returned Iterable is lazy, and calls f for each element of this every time it's iterated.

abstract void fillRange(int start, int end, [E fillValue]) #

inherited from List

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()}) #

inherited from Iterable

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)) #

inherited from Iterable

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)) #

inherited from Iterable

Applies the function f to each element of this collection.

abstract Iterable<E> getRange(int start, int end) #

inherited from List

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]) #

inherited from List

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) #

inherited from List

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) #

inherited from List

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 = ""]) #

inherited from Iterable

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]) #

inherited from List

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()}) #

inherited from Iterable

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)) #

inherited from Iterable

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.

docs inherited from Object
noSuchMethod(Invocation invocation) => mirror.reflect(tokens).delegate(invocation);

abstract E reduce(E combine(E value, E element)) #

inherited from Iterable

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) #

inherited from List

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) #

inherited from List

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.

abstract E removeLast() #

inherited from List

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) #

inherited from List

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)) #

inherited from List

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) #

inherited from List

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)) #

inherited from List

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) #

inherited from List

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]) #

inherited from List

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 void shuffle() #

inherited from List

Shuffles the elements of this list randomly.

abstract E singleWhere(bool test(E element)) #

inherited from Iterable

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) #

inherited from Iterable

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)) #

inherited from Iterable

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)]) #

inherited from List

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]) #

inherited from List

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> take(int n) #

inherited from Iterable

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

It is an error if n is negative.

abstract Iterable<E> takeWhile(bool test(E value)) #

inherited from Iterable

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 List<E> toList({bool growable: true}) #

inherited from Iterable

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

abstract Set<E> toSet() #

inherited from Iterable

Creates a Set containing the elements of this Iterable.

abstract Iterable<E> where(bool test(E element)) #

inherited from Iterable

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.