Skip to content

Commit cbb3260

Browse files
wiedenmctrueden
authored andcommitted
Add simple Op method test
1 parent ac6786b commit cbb3260

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops;
31+
32+
import static org.junit.Assert.assertEquals;
33+
34+
import java.util.function.BiFunction;
35+
import java.util.function.Function;
36+
37+
import org.junit.Test;
38+
import org.scijava.ops.core.OpCollection;
39+
import org.scijava.ops.types.Nil;
40+
import org.scijava.ops.util.Functions;
41+
import org.scijava.param.Parameter;
42+
import org.scijava.plugin.Plugin;
43+
import org.scijava.struct.ItemIO;
44+
45+
/**
46+
* @author Marcel Wiedenmann
47+
*/
48+
@Plugin(type = OpCollection.class)
49+
public class OpMethodTest extends AbstractTestEnvironment {
50+
51+
@Test
52+
public void testParseIntegerOp() {
53+
// Will match a lambda created and returned by createParseIntegerOp() below.
54+
final Function<String, Integer> parseIntegerOp = Functions.unary(ops, "test.parseInteger", new Nil<String>() {},
55+
new Nil<Integer>()
56+
{});
57+
58+
final String numericString = "42";
59+
final Integer parsedInteger = parseIntegerOp.apply(numericString);
60+
assertEquals(Integer.parseInt(numericString), (int) parsedInteger);
61+
}
62+
63+
@Test
64+
public void testMultiplyNumericStringsOpMethod() {
65+
// Will match a lambda created and returned by
66+
// createMultiplyNumericStringsOp(..), which itself captured a lambda
67+
// created and returned by createParseIntegerOp().
68+
final BiFunction<String, String, Integer> multiplyNumericStringsOp = Functions.binary(ops,
69+
"test.multiplyNumericStrings", new Nil<String>()
70+
{}, new Nil<String>() {}, new Nil<Integer>() {});
71+
72+
final String numericString1 = "3";
73+
final String numericString2 = "18";
74+
final Integer multipliedNumericStrings = multiplyNumericStringsOp.apply(numericString1, numericString2);
75+
assertEquals(Integer.parseInt(numericString1) * Integer.parseInt(numericString2), (int) multipliedNumericStrings);
76+
}
77+
78+
@OpMethod(names = "test.parseInteger")
79+
// Refers to the input parameter of the function that's returned by this
80+
// factory method.
81+
@Parameter(key = "numericString")
82+
// Refers to the output parameter of the function.
83+
@Parameter(key = "parsedInteger", itemIO = ItemIO.OUTPUT)
84+
public Function<String, Integer> createParseIntegerOp() {
85+
return Integer::parseInt;
86+
}
87+
88+
@OpMethod(names = "test.multiplyNumericStrings")
89+
@Parameter(key = "numericString1")
90+
@Parameter(key = "numericString2")
91+
@Parameter(key = "multipliedNumericStrings", itemIO = ItemIO.OUTPUT)
92+
// Refers to the first parameter of this factory method ("parseIntegerOp").
93+
// Will be automatically provided by the matching system. Can be used by the
94+
// returned lambda/Op as if it was an @OpDependency-annotated field in a
95+
// regular Op class.
96+
@OpDependency(name = "test.parseInteger")
97+
public BiFunction<String, String, Integer> createMultiplyNumericStringsOp(
98+
final Function<String, Integer> parseIntegerOp)
99+
{
100+
return (i1, i2) -> parseIntegerOp.apply(i1) * parseIntegerOp.apply(i2);
101+
}
102+
}

0 commit comments

Comments
 (0)