|
| 1 | + |
| 2 | +package org.scijava.ops.hints; |
| 3 | + |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | +import org.scijava.ops.AbstractTestEnvironment; |
| 10 | +import org.scijava.ops.OpField; |
| 11 | +import org.scijava.ops.core.OpCollection; |
| 12 | +import org.scijava.ops.hints.BaseOpHints.Adaptation; |
| 13 | +import org.scijava.ops.hints.BaseOpHints.Simplification; |
| 14 | +import org.scijava.ops.hints.impl.DefaultHints; |
| 15 | +import org.scijava.ops.matcher.OpMatchingException; |
| 16 | +import org.scijava.plugin.Plugin; |
| 17 | + |
| 18 | +@Plugin(type = OpCollection.class) |
| 19 | +public class SimplificationHintTest extends AbstractTestEnvironment { |
| 20 | + |
| 21 | + @OpField(names = "test.simplification.hints") |
| 22 | + public final Function<Double[], Double[]> op = (in) -> new Double[in.length]; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testSimplification() { |
| 26 | + // make sure we can find the Op when adaptation is allowed |
| 27 | + Hints hints = new DefaultHints(); |
| 28 | + hints.setHint(Simplification.ALLOWED); |
| 29 | + ops.env().setHints(hints); |
| 30 | + @SuppressWarnings("unused") |
| 31 | + Function<Integer[], Integer[]> adaptable = ops.op( |
| 32 | + "test.simplification.hints").inType(Integer[].class).outType( |
| 33 | + Integer[].class).function(); |
| 34 | + // make sure we cannot find the Op when adaptation is not allowed |
| 35 | + hints.setHint(Simplification.FORBIDDEN); |
| 36 | + ops.env().setHints(hints); |
| 37 | + try { |
| 38 | + ops.op("test.simplification.hints").inType(Integer[].class).outType( |
| 39 | + Integer[].class).function(); |
| 40 | + throw new IllegalStateException( |
| 41 | + "Simplification is forbidden - this op call should not match!"); |
| 42 | + } |
| 43 | + catch (IllegalArgumentException e) { |
| 44 | + assertTrue(e.getCause() instanceof OpMatchingException); |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @OpHints(hints = { Simplification.FORBIDDEN }) |
| 50 | + @OpField(names = "test.simplification.unsimplifiable") |
| 51 | + public final Function<Double[], Double[]> nonAdaptableOp = ( |
| 52 | + in) -> new Double[in.length]; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testUnsimplifiableOp() { |
| 56 | + // make sure we can find the Op when adaptation is allowed |
| 57 | + Hints hints = new DefaultHints(); |
| 58 | + hints.setHint(Simplification.ALLOWED); |
| 59 | + ops.env().setHints(hints); |
| 60 | + @SuppressWarnings("unused") |
| 61 | + Function<Double[], Double[]> adaptable = ops.op( |
| 62 | + "test.simplification.unsimplifiable").inType(Double[].class).outType( |
| 63 | + Double[].class).function(); |
| 64 | + // make sure that we cannot match the Op via adaptation even when |
| 65 | + // simplification |
| 66 | + // is allowed (since it declares itself to be unsimplifiable) |
| 67 | + try { |
| 68 | + ops.op("test.simplification.unsimplifiable").inType(Integer[].class) |
| 69 | + .outType(Integer[].class).function(); |
| 70 | + throw new IllegalStateException( |
| 71 | + "The only relevant Op is not simplifiable - this op call should not match!"); |
| 72 | + } |
| 73 | + catch (IllegalArgumentException e) { |
| 74 | + assertTrue(e.getCause() instanceof OpMatchingException); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments