-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom function registration in Java API (#101)
- Loading branch information
Showing
5 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.github.erdos.stencil; | ||
|
||
import io.github.erdos.stencil.exceptions.EvalException; | ||
import io.github.erdos.stencil.functions.Function; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class APITest { | ||
|
||
private static class CustomFunction implements Function { | ||
private int callCount = 0; | ||
private Object lastResult = null; | ||
|
||
@Override | ||
public Object call(final Object... arguments) throws IllegalArgumentException { | ||
callCount++; | ||
lastResult = arguments.length > 0 ? arguments[0] : arguments; | ||
return lastResult; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "customFunction"; | ||
} | ||
} | ||
|
||
@Test | ||
public void testCustomFunction() throws IOException { | ||
final CustomFunction fn = new CustomFunction(); | ||
try (final PreparedTemplate prepared = | ||
API.prepare(new File("test-resources/test-custom-function.docx"))) { | ||
final Map<String, Object> data = new HashMap<>(); | ||
data.put("input", "testInput"); | ||
API.render(prepared, Collections.emptyMap(), TemplateData.fromMap(data), Collections.singletonList(fn)); | ||
} | ||
Assert.assertTrue("Custom function should have been called", fn.callCount > 0); | ||
Assert.assertEquals("Custom function returned unexpected value", "testInput", fn.lastResult); | ||
} | ||
|
||
@Test(expected = EvalException.class) | ||
public void testWithoutCustomFunction() throws IOException { | ||
try (final PreparedTemplate prepared = | ||
API.prepare(new File("test-resources/test-custom-function.docx"))) { | ||
final Map<String, Object> data = new HashMap<>(); | ||
data.put("input", "testInput"); | ||
API.render(prepared, Collections.emptyMap(), TemplateData.fromMap(data)); | ||
Assert.fail("Should have thrown exception"); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters