Dart API ReferenceunittestLogEntryList

LogEntryList Class

We do verification on a list of LogEntrys. To allow chaining of calls to verify, we encapsulate such a list in the LogEntryList class.

Constructors

Code new LogEntryList([String filter]) #

LogEntryList([this.filter]) {
  logs = new List<LogEntry>();
}

Methods

Code add(LogEntry entry) #

Add a LogEntry to the log.

add(LogEntry entry) => logs.add(entry);

Code LogEntryList after(Date when, [bool inPlace = false]) #

Returns log events that happened after when. If inPlace is true, then it returns this LogEntryList after removing the entries that happened up to when; otherwise a new list is created.

LogEntryList after(Date when, [bool inPlace = false]) =>
    _tail((e) => e.time > when, inPlace, 'after $when', logs.length);

Code LogEntryList afterEntry(LogEntry logEntry, [bool inPlace = false]) #

Returns log events that happened after logEntry's time. If inPlace is true, then it returns this LogEntryList after removing the entries that happened up to when; otherwise a new list is created. If logEntry is null the current time is used.

LogEntryList afterEntry(LogEntry logEntry, [bool inPlace = false]) =>
    after(logEntry == null ? new Date.now() : logEntry.time);

Code LogEntryList afterFirst(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened after the first event in segment. If inPlace is true, then it returns this LogEntryList after removing the entries that happened earlier; otherwise a new list is created.

LogEntryList afterFirst(LogEntryList segment, [bool inPlace = false]) =>
    afterEntry(segment.first, inPlace);

Code LogEntryList afterLast(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened after the last event in segment. If inPlace is true, then it returns this LogEntryList after removing the entries that happened earlier; otherwise a new list is created.

LogEntryList afterLast(LogEntryList segment, [bool inPlace = false]) =>
    afterEntry(segment.last, inPlace);

Code LogEntryList before(Date when, [bool inPlace = false]) #

Returns log events that happened before when. If inPlace is true, then it returns this LogEntryList after removing the entries that happened from when onwards; otherwise a new list is created.

LogEntryList before(Date when, [bool inPlace = false]) =>
    _head((e) => e.time >= when, inPlace, 'before $when', logs.length);

Code LogEntryList beforeEntry(LogEntry logEntry, [bool inPlace = false]) #

Returns log events that happened before logEntry's time. If inPlace is true, then it returns this LogEntryList after removing the entries that happened from when onwards; otherwise a new list is created. If logEntry is null the epoch time is used.

LogEntryList beforeEntry(LogEntry logEntry, [bool inPlace = false]) =>
    before(logEntry == null ?
        new Date.fromMillisecondsSinceEpoch(0) : logEntry.time);

Code LogEntryList beforeFirst(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened before the first event in segment. If inPlace is true, then it returns this LogEntryList after removing the entries that happened later; otherwise a new list is created.

LogEntryList beforeFirst(LogEntryList segment, [bool inPlace = false]) =>
    beforeEntry(segment.first, inPlace);

Code LogEntryList beforeLast(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened before the last event in segment. If inPlace is true, then it returns this LogEntryList after removing the entries that happened later; otherwise a new list is created.

LogEntryList beforeLast(LogEntryList segment, [bool inPlace = false]) =>
    beforeEntry(segment.last, inPlace);

Code int findLogEntry(logFilter, [int start = 0, int failureReturnValue = -1]) #

Find the first log entry that satisfies logFilter and return its position. A search start position can be provided to allow for repeated searches. logFilter can be a CallMatcher, or a predicate function that takes a LogEntry argument and returns a bool. If logFilter is null, it will match any LogEntry. If no entry is found, then failureReturnValue is returned.

int findLogEntry(logFilter, [int start = 0, int failureReturnValue = -1]) {
  logFilter = _makePredicate(logFilter);
  int pos = start;
  while (pos < logs.length) {
    if (logFilter(logs[pos])) {
      return pos;
    }
    ++pos;
  }
  return failureReturnValue;
}

Code get first() #

Get the first entry, or null if no entries.

get first() => (logs == null || logs.length == 0) ? null : logs[0];

Code LogEntryList following(LogEntryList keys, [mockNameFilter = null, logFilter = null, int distance = 1, bool includeKeys = false]) #

Iterate through the LogEntryList looking for matches to the entries in keys; for each match found the closest distance subsequent log entries that match mocknameFilter and logFilter will be included in the result. If includeKeys is true then the entries in keys that resulted in entries in the output list are themselves included in the output list. If distance is zero then all matches are included. See preceding for a usage example.

LogEntryList following(LogEntryList keys,
                       [mockNameFilter = null,
                       logFilter = null,
                       int distance = 1,
                       bool includeKeys = false]) =>
    _neighboring(false, keys, mockNameFilter, logFilter,
        distance, includeKeys);

Code LogEntryList from(Date when, [bool inPlace = false]) #

Returns log events that happened from when onwards. If inPlace is true, then it returns this LogEntryList after removing the entries that happened before when; otherwise a new list is created.

LogEntryList from(Date when, [bool inPlace = false]) =>
    _tail((e) => e.time >= when, inPlace, 'from $when', logs.length);

Code LogEntryList fromEntry(LogEntry logEntry, [bool inPlace = false]) #

Returns log events that happened from logEntry's time onwards. If inPlace is true, then it returns this LogEntryList after removing the entries that happened before when; otherwise a new list is created. If logEntry is null the current time is used.

LogEntryList fromEntry(LogEntry logEntry, [bool inPlace = false]) =>
    from(logEntry == null ? new Date.now() : logEntry.time);

Code LogEntryList fromFirst(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened from the time of the first event in segment onwards. If inPlace is true, then it returns this LogEntryList after removing the earlier entries; otherwise a new list is created.

LogEntryList fromFirst(LogEntryList segment, [bool inPlace = false]) =>
    fromEntry(segment.first, inPlace);

Code LogEntryList fromLast(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened from the time of the last event in segment onwards. If inPlace is true, then it returns this LogEntryList after removing the earlier entries; otherwise a new list is created.

LogEntryList fromLast(LogEntryList segment, [bool inPlace = false]) =>
    fromEntry(segment.last, inPlace);

Code LogEntryList getMatches([mockNameFilter, logFilter, Matcher actionMatcher, bool destructive = false]) #

Create a new LogEntryList consisting of LogEntrys from this list that match the specified mockNameFilter and logFilter. mockNameFilter can be null, a String, a predicate Function, or a Matcher. If mockNameFilter is null, this is the same as anything. If logFilter is null, all entries in the log will be returned. Otherwise logFilter should be a CallMatcher or predicate function that takes a LogEntry and returns a bool. If destructive is true, the log entries are removed from the original list.

LogEntryList getMatches([mockNameFilter,
                        logFilter,
                        Matcher actionMatcher,
                        bool destructive = false]) {
  if (mockNameFilter == null) {
    mockNameFilter = anything;
  } else {
    mockNameFilter = wrapMatcher(mockNameFilter);
  }
  Function entryFilter = _makePredicate(logFilter);
  String filterName = _qualifiedName(mockNameFilter, logFilter.toString());
  LogEntryList rtn = new LogEntryList(filterName);
  MatchState matchState = new MatchState();
  for (var i = 0; i < logs.length; i++) {
    LogEntry entry = logs[i];
    if (mockNameFilter.matches(entry.mockName, matchState) &&
        entryFilter(entry)) {
      if (actionMatcher == null ||
          actionMatcher.matches(entry, matchState)) {
        rtn.add(entry);
        if (destructive) {
          logs.removeRange(i--, 1);
        }
      }
    }
  }
  return rtn;
}

Code get last() #

Get the last entry, or null if no entries.

get last() => (logs == null || logs.length == 0) ? null : logs.last();

Code LogEntryList preceding(LogEntryList keys, [mockNameFilter = null, logFilter = null, int distance = 1, bool includeKeys = false]) #

Iterate through the LogEntryList looking for matches to the entries in keys; for each match found the closest distance prior log entries that match mocknameFilter and logFilter will be included in the result. If includeKeys is true then the entries in keys that resulted in entries in the output list are themselves included in the output list. If distance is zero then all matches are included.

The idea here is that you could find log entries that are related to other logs entries in some temporal sense. For example, say we have a method commit() that returns -1 on failure. Before commit() gets called the value being committed is created by process(). We may want to find the calls to process() that preceded calls to commit() that failed. We could do this with:

 print(log.preceding(log.getLogs(callsTo('commit'), returning(-1)),
     logFilter: callsTo('process')).toString());

We might want to include the details of the failing calls to commit() to see what parameters were passed in, in which case we would set includeKeys.

As another simple example, say we wanted to know the three method calls that immediately preceded each failing call to commit():

print(log.preceding(log.getLogs(callsTo('commit'), returning(-1)),
    distance: 3).toString());
LogEntryList preceding(LogEntryList keys,
                       [mockNameFilter = null,
                       logFilter = null,
                       int distance = 1,
                       bool includeKeys = false]) =>
    _neighboring(true, keys, mockNameFilter, logFilter,
        distance, includeKeys);

Code String toString([Date baseTime]) #

Turn the logs into human-readable text. If baseTime is specified then each entry is prefixed with the offset from that time in milliseconds; otherwise the time of day is used.

String toString([Date baseTime]) {
  String s = '';
  for (var e in logs) {
    s = '$s${e.toString(baseTime)}\n';
  }
  return s;
}

Code LogEntryList until(Date when, [bool inPlace = false]) #

Returns log events that happened until when. If inPlace is true, then it returns this LogEntryList after removing the entries that happened after when; otherwise a new list is created.

LogEntryList until(Date when, [bool inPlace = false]) =>
    _head((e) => e.time > when, inPlace, 'until $when', logs.length);

Code LogEntryList untilEntry(LogEntry logEntry, [bool inPlace = false]) #

Returns log events that happened until logEntry's time. If inPlace is true, then it returns this LogEntryList after removing the entries that happened after when; otherwise a new list is created. If logEntry is null the epoch time is used.

LogEntryList untilEntry(LogEntry logEntry, [bool inPlace = false]) =>
    until(logEntry == null ?
        new Date.fromMillisecondsSinceEpoch(0) : logEntry.time);

Code LogEntryList untilFirst(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened until the first event in segment. If inPlace is true, then it returns this LogEntryList after removing the entries that happened later; otherwise a new list is created.

LogEntryList untilFirst(LogEntryList segment, [bool inPlace = false]) =>
    untilEntry(segment.first, inPlace);

Code LogEntryList untilLast(LogEntryList segment, [bool inPlace = false]) #

Returns log events that happened until the last event in segment. If inPlace is true, then it returns this LogEntryList after removing the entries that happened later; otherwise a new list is created.

LogEntryList untilLast(LogEntryList segment, [bool inPlace = false]) =>
    untilEntry(segment.last, inPlace);

Code LogEntryList verify(Matcher matcher) #

Apply a unit test Matcher to the LogEntryList.

LogEntryList verify(Matcher matcher) {
  if (_mockFailureHandler == null) {
    _mockFailureHandler =
        new _MockFailureHandler(getOrCreateExpectFailureHandler());
  }
  expect(logs, matcher, filter, _mockFailureHandler);
  return this;
}

Fields

Code String filter #

String filter;

Code List<LogEntry> logs #

List<LogEntry> logs;