|
| 1 | + |
| 2 | +package org.scijava.ops.engine.describe; |
| 3 | + |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.BeforeAll; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.scijava.function.Producer; |
| 8 | +import org.scijava.ops.engine.AbstractTestEnvironment; |
| 9 | +import org.scijava.ops.spi.OpCollection; |
| 10 | +import org.scijava.ops.spi.OpField; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests the {@code engine.describe} Ops in {@link BaseDescriptors}. |
| 17 | + * |
| 18 | + * @author Gabriel Selzer |
| 19 | + */ |
| 20 | +public class BaseDescriptorsTest extends AbstractTestEnvironment implements |
| 21 | + OpCollection |
| 22 | +{ |
| 23 | + |
| 24 | + @BeforeAll |
| 25 | + public static void addNeededOps() { |
| 26 | + ops.register(new BaseDescriptors<>()); |
| 27 | + ops.register(new BaseDescriptorsTest()); |
| 28 | + } |
| 29 | + |
| 30 | + @OpField(names = "test.byteDescriptor") |
| 31 | + public final Producer<Byte> byteProducer = // |
| 32 | + () -> (byte) 0; |
| 33 | + |
| 34 | + @OpField(names = "test.shortDescriptor") |
| 35 | + public final Producer<Short> shortProducer = // |
| 36 | + () -> (short) 0; |
| 37 | + |
| 38 | + @OpField(names = "test.intDescriptor") |
| 39 | + public final Producer<Integer> intProducer = // |
| 40 | + () -> 0; |
| 41 | + |
| 42 | + @OpField(names = "test.longDescriptor") |
| 43 | + public final Producer<Long> longProducer = // |
| 44 | + () -> 0L; |
| 45 | + |
| 46 | + @OpField(names = "test.floatDescriptor") |
| 47 | + public final Producer<Float> floatProducer = // |
| 48 | + () -> 0.0f; |
| 49 | + |
| 50 | + @OpField(names = "test.doubleDescriptor") |
| 51 | + public final Producer<Double> doubleProducer = // |
| 52 | + () -> 0.0; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testBoxedPrimitiveDescriptor() { |
| 56 | + String[] tests = { "byte", "short", "int", "long", "float", "double" }; |
| 57 | + for (String t : tests) { |
| 58 | + var expected = "test." + t + "Descriptor:\n\t- () -> number"; |
| 59 | + var actual = ops.help("test." + t + "Descriptor"); |
| 60 | + Assertions.assertEquals(expected, actual); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @OpField(names = "test.listDescriptor") |
| 65 | + public final Producer<List<Double>> listDoubleProducer = ArrayList::new; |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testListDescriptor() { |
| 69 | + var expected = "test.listDescriptor:\n\t- () -> list<number>"; |
| 70 | + var actual = ops.help("test.listDescriptor"); |
| 71 | + Assertions.assertEquals(expected, actual); |
| 72 | + } |
| 73 | + |
| 74 | + @OpField(names = "test.arrayDescriptor") |
| 75 | + public final Producer<double[]> doubleArrayProducer = // |
| 76 | + () -> new double[0]; |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testArrayDescriptor() { |
| 80 | + var expected = "test.arrayDescriptor:\n\t- () -> number[]"; |
| 81 | + var actual = ops.help("test.arrayDescriptor"); |
| 82 | + Assertions.assertEquals(expected, actual); |
| 83 | + } |
| 84 | + |
| 85 | + @OpField(names = "test.arrayArrayDescriptor") |
| 86 | + public final Producer<double[][]> doubleArrayArrayProducer = // |
| 87 | + () -> new double[0][0]; |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testArrayArrayDescriptor() { |
| 91 | + var expected = "test.arrayArrayDescriptor:\n\t- () -> number[][]"; |
| 92 | + var actual = ops.help("test.arrayArrayDescriptor"); |
| 93 | + Assertions.assertEquals(expected, actual); |
| 94 | + } |
| 95 | + |
| 96 | + @OpField(names = "test.listArrayDescriptor") |
| 97 | + public final Producer<List<Double[]>> listDoubleArrayProducer = // |
| 98 | + ArrayList::new; |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testListArrayDescriptor() { |
| 102 | + var expected = "test.listArrayDescriptor:\n\t- () -> list<number[]>"; |
| 103 | + var actual = ops.help("test.listArrayDescriptor"); |
| 104 | + Assertions.assertEquals(expected, actual); |
| 105 | + } |
| 106 | + |
| 107 | + public static class Foo { |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @OpField(names = "test.fooDescriptor") |
| 112 | + public final Producer<Foo> fooProducer = Foo::new; |
| 113 | + |
| 114 | + /** |
| 115 | + * Test behavior when no {@code engine.describe} Op exists for a type. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testFallbackDescriptions() { |
| 119 | + var expected = |
| 120 | + "test.fooDescriptor:\n\t- () -> Foo"; |
| 121 | + var actual = ops.help("test.fooDescriptor"); |
| 122 | + Assertions.assertEquals(expected, actual); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments