Search in sources :

Example 1 with NeededCoverageSpec

use of com.facebook.buck.python.NeededCoverageSpec in project buck by facebook.

the class NeededCoverageSpecTypeCoercer method coerce.

@Override
public NeededCoverageSpec coerce(CellPathResolver cellRoots, ProjectFilesystem filesystem, Path pathRelativeToProjectRoot, Object object) throws CoerceFailedException {
    if (object instanceof NeededCoverageSpec) {
        return (NeededCoverageSpec) object;
    }
    if (object instanceof Collection<?>) {
        Collection<?> collection = (Collection<?>) object;
        if (collection.size() == 2 || collection.size() == 3) {
            Iterator<?> iter = collection.iterator();
            Float neededRatio = floatTypeCoercer.coerce(cellRoots, filesystem, pathRelativeToProjectRoot, iter.next());
            if (neededRatio < 0 || neededRatio > 1) {
                throw CoerceFailedException.simple(object, getOutputClass(), "the needed coverage ratio should be in range [0; 1]");
            }
            BuildTarget buildTarget = buildTargetTypeCoercer.coerce(cellRoots, filesystem, pathRelativeToProjectRoot, iter.next());
            Optional<String> pathName = Optional.empty();
            if (iter.hasNext()) {
                pathName = Optional.of(pathNameTypeCoercer.coerce(cellRoots, filesystem, pathRelativeToProjectRoot, iter.next()));
            }
            return NeededCoverageSpec.of(neededRatio, buildTarget, pathName);
        }
    }
    throw CoerceFailedException.simple(object, getOutputClass(), "input should be a tuple of needed coverage ratio, a build target, and optionally a path");
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) Collection(java.util.Collection) NeededCoverageSpec(com.facebook.buck.python.NeededCoverageSpec)

Example 2 with NeededCoverageSpec

use of com.facebook.buck.python.NeededCoverageSpec 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);
}
Also used : Type(java.lang.reflect.Type) NeededCoverageSpec(com.facebook.buck.python.NeededCoverageSpec) Test(org.junit.Test)

Aggregations

NeededCoverageSpec (com.facebook.buck.python.NeededCoverageSpec)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 Test (org.junit.Test)1