mockValues<T> function

T Function() mockValues<T>(
  1. List<T> values
)

Returns a function that cycles through values on each call.

Mirrors mockValues from the JS AI SDK v6 ai/test sub-path.

final next = mockValues(['a', 'b', 'c']);
expect(next(), 'a');
expect(next(), 'b');
expect(next(), 'c');
// After the last value, keeps returning the last element:
expect(next(), 'c');

Implementation

T Function() mockValues<T>(List<T> values) {
  if (values.isEmpty) {
    throw ArgumentError('values must not be empty');
  }
  var index = 0;
  return () {
    final value = values[index];
    if (index < values.length - 1) index++;
    return value;
  };
}