use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coerceToEnumShouldWorkInList.
@Test
public void coerceToEnumShouldWorkInList() throws NoSuchFieldException, CoerceFailedException {
Type type = TestFields.class.getField("listOfTestEnums").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
ImmutableList<String> input = ImmutableList.of("PURPLE", "RED", "RED", "PURPLE");
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
ImmutableList<TestEnum> expected = ImmutableList.of(TestEnum.PURPLE, TestEnum.RED, TestEnum.RED, TestEnum.PURPLE);
assertEquals(expected, result);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coerceToEnumsShouldWorkWithUpperAndLowerCaseValues.
@Test
public void coerceToEnumsShouldWorkWithUpperAndLowerCaseValues() throws NoSuchFieldException, CoerceFailedException {
Type type = TestFields.class.getField("listOfTestEnums").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
ImmutableList<String> input = ImmutableList.of("grey", "YELLOW", "red", "PURPLE");
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
ImmutableList<TestEnum> expected = ImmutableList.of(TestEnum.grey, TestEnum.yellow, TestEnum.RED, TestEnum.PURPLE);
assertEquals(expected, result);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method disallowSubclassOfSuperclass.
/**
* Cannot declare a field that is not a superclass (i.e. LinkedList instead of List or
* ImmutableList). Required as TypeCoercer will assign an ImmutableList.
*/
@Test(expected = IllegalArgumentException.class)
public void disallowSubclassOfSuperclass() throws NoSuchFieldException {
Type type = TestFields.class.getField("subclassOfList").getGenericType();
typeCoercerFactory.typeCoercerForType(type);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method hasElementTypesForPair.
@Test
public void hasElementTypesForPair() throws NoSuchFieldException {
Type type = TestFields.class.getField("pairOfPathsAndStrings").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
assertTrue(coercer.hasElementClass(String.class));
assertTrue(coercer.hasElementClass(Path.class));
assertFalse(coercer.hasElementClass(Integer.class));
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coerceToEitherLeftOrRightWithCollections.
@Test
public void coerceToEitherLeftOrRightWithCollections() throws NoSuchFieldException, CoerceFailedException {
Type type = TestFields.class.getField("eitherStringOrStringList").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
String inputString = "a_string";
ImmutableList<String> inputList = ImmutableList.of("a", "b");
assertEquals(Either.ofLeft(inputString), coercer.coerce(cellRoots, filesystem, Paths.get(""), inputString));
assertEquals(Either.ofRight(inputList), coercer.coerce(cellRoots, filesystem, Paths.get(""), inputList));
}
Aggregations