-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ValueArgumentsProvider.java
49 lines (41 loc) · 1.63 KB
/
ValueArgumentsProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.jupiter.params.provider;
import static java.util.stream.Collectors.toList;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;
import org.junit.platform.commons.util.Preconditions;
/**
* @since 5.0
*/
class ValueArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<ValueSource> {
private Object[] arguments;
@Override
public void accept(ValueSource source) {
List<Object> arrays = Stream.of(source.strings(), source.ints(), source.longs(), source.doubles()) //
.filter(array -> Array.getLength(array) > 0) //
.collect(toList());
Preconditions.condition(arrays.size() == 1, () -> "Exactly one type of input must be provided in the @"
+ ValueSource.class.getSimpleName() + " annotation but there were " + arrays.size());
Object originalArray = arrays.get(0);
arguments = IntStream.range(0, Array.getLength(originalArray)) //
.mapToObj(index -> Array.get(originalArray, index)) //
.toArray();
}
@Override
public Stream<? extends Arguments> arguments(ContainerExtensionContext context) {
return Arrays.stream(arguments).map(ObjectArrayArguments::arguments);
}
}