use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method traverseWithEitherAndContainer.
@Test
public void traverseWithEitherAndContainer() throws NoSuchFieldException {
Type type = TestFields.class.getField("eitherStringOrStringList").getGenericType();
@SuppressWarnings("unchecked") TypeCoercer<Either<String, List<String>>> coercer = (TypeCoercer<Either<String, List<String>>>) typeCoercerFactory.typeCoercerForType(type);
TestTraversal traversal = new TestTraversal();
Either<String, List<String>> input = Either.ofRight((List<String>) ImmutableList.of("foo"));
coercer.traverse(input, traversal);
assertThat(traversal.getObjects(), Matchers.contains(ImmutableList.of(sameInstance((Object) input.getRight()), sameInstance((Object) input.getRight().get(0)))));
traversal = new TestTraversal();
Either<String, List<String>> input2 = Either.ofLeft("foo");
coercer.traverse(input2, traversal);
assertThat(traversal.getObjects(), hasSize(1));
assertThat(traversal.getObjects().get(0), sameInstance((Object) "foo"));
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method traverseShouldVisitEveryObject.
/**
* Traverse visits every element of an input value without coercing to the output type.
*/
@Test
public void traverseShouldVisitEveryObject() throws NoSuchFieldException {
Type type = TestFields.class.getField("stringMapOfLists").getGenericType();
@SuppressWarnings("unchecked") TypeCoercer<ImmutableMap<String, ImmutableList<String>>> coercer = (TypeCoercer<ImmutableMap<String, ImmutableList<String>>>) typeCoercerFactory.typeCoercerForType(type);
final ImmutableMap<String, ImmutableList<String>> input = ImmutableMap.of("foo", ImmutableList.of("//foo:bar", "//foo:baz"), "bar", ImmutableList.of(":bar", "//foo:foo"));
TestTraversal traversal = new TestTraversal();
coercer.traverse(input, traversal);
Matcher<Iterable<?>> matcher = Matchers.contains(ImmutableList.of(sameInstance((Object) input), is((Object) "foo"), sameInstance((Object) input.get("foo")), is((Object) "//foo:bar"), is((Object) "//foo:baz"), is((Object) "bar"), sameInstance((Object) input.get("bar")), is((Object) ":bar"), is((Object) "//foo:foo")));
assertThat(traversal.getObjects(), matcher);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coercingHeterogeneousAppleSourceGroups.
@Test
public void coercingHeterogeneousAppleSourceGroups() throws NoSuchFieldException, CoerceFailedException {
Type type = TestFields.class.getField("listOfSourcesWithFlags").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
ImmutableList<?> input = ImmutableList.of("Group1/foo.m", ImmutableList.of("Group1/bar.m", ImmutableList.of("-Wall", "-Werror")), "Group2/baz.m", ImmutableList.of("Group2/blech.m", ImmutableList.of("-fobjc-arc")));
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
ImmutableList<SourceWithFlags> expectedResult = ImmutableList.of(SourceWithFlags.of(new FakeSourcePath("Group1/foo.m")), SourceWithFlags.of(new FakeSourcePath("Group1/bar.m"), ImmutableList.of("-Wall", "-Werror")), SourceWithFlags.of(new FakeSourcePath("Group2/baz.m")), SourceWithFlags.of(new FakeSourcePath("Group2/blech.m"), ImmutableList.of("-fobjc-arc")));
assertEquals(expectedResult, result);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method shouldAllowListTypeToBeSuperclassOfResult.
@Test
public void shouldAllowListTypeToBeSuperclassOfResult() throws CoerceFailedException, NoSuchFieldException {
Type type = TestFields.class.getField("superclassOfImmutableList").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
ImmutableList<String> input = ImmutableList.of("a", "b", "c");
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
assertEquals(ImmutableList.of("a", "b", "c"), result);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coerceToNeededCoverageSpec.
@Test
public void coerceToNeededCoverageSpec() throws NoSuchFieldException, CoerceFailedException {
Type type = TestFields.class.getField("listOfNeededCoverageSpecs").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
ImmutableList<?> input = ImmutableList.of(ImmutableList.of(0.0, "//some:build-target"), ImmutableList.of(0.9, "//other/build:target"), ImmutableList.of(1.0, "//:target", "some/path.py"));
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
ImmutableList<NeededCoverageSpec> expectedResult = ImmutableList.of(NeededCoverageSpec.of(0.0f, BuildTargetFactory.newInstance("//some:build-target"), Optional.empty()), NeededCoverageSpec.of(0.9f, BuildTargetFactory.newInstance("//other/build:target"), Optional.empty()), NeededCoverageSpec.of(1.0f, BuildTargetFactory.newInstance("//:target"), Optional.of("some/path.py")));
assertEquals(expectedResult, result);
}
Aggregations