-
Notifications
You must be signed in to change notification settings - Fork 28
Add intersects handler #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -493,6 +493,30 @@ public Object eval(Object[] args, EvaluationContext ec) { | |
| return args[0]; | ||
| }}); | ||
|
|
||
| fd.getEvaluationContext().addFunctionHandler(new IFunctionHandler() { | ||
|
|
||
| public String getName() { | ||
| return "intersects"; | ||
| } | ||
|
|
||
| public List<Class[]> getPrototypes() { | ||
| return new ArrayList<Class[]>(); | ||
| } | ||
|
|
||
| public boolean rawArgs() { | ||
| return true; | ||
| } | ||
|
|
||
| public boolean realTime() { | ||
| return false; | ||
| } | ||
|
|
||
| public Object eval(Object[] args, EvaluationContext ec) { | ||
| // stub for validation | ||
| return args[0]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this not return a Boolean?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think you meant the actual return value, not the return type! I don't think it makes any practical difference because values would get coerced so I think it's safe to change. |
||
| } | ||
| }); | ||
|
|
||
| // check for runtime errors | ||
| fd.initialize(true, new InstanceInitializationFactory()); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.opendatakit.validate; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.net.URISyntaxException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.isEmptyString; | ||
|
|
||
| public class CollectFunctionHandlerTest { | ||
|
|
||
| @Test | ||
| public void acceptsIntersects() throws URISyntaxException { | ||
| final Path path = TestUtils.getPathOf("intersects_function.xml"); | ||
| final FormValidator validator = new FormValidator(); | ||
|
|
||
| Output output = Output.runAndGet(() -> validator.validate(path.toString())); | ||
| assertThat(output.getErr(), isEmptyString()); | ||
| assertThat(output.getStd(), containsString("Xform is valid")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package org.opendatakit.validate; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
|
|
||
| public class Output { | ||
| private final String std; | ||
| private final String err; | ||
|
|
||
| Output(String std, String err) { | ||
| this.std = std; | ||
| this.err = err; | ||
| } | ||
|
|
||
| static Output runAndGet(Runnable runnable) { | ||
| PrintStream outBackup = System.out; | ||
| ByteArrayOutputStream stdBaos = new ByteArrayOutputStream(); | ||
| PrintStream stdPs = new PrintStream(stdBaos); | ||
| System.setOut(stdPs); | ||
|
|
||
| PrintStream errBackup = System.err; | ||
| ByteArrayOutputStream errBaos = new ByteArrayOutputStream(); | ||
| PrintStream errPs = new PrintStream(errBaos); | ||
| System.setErr(errPs); | ||
|
|
||
| runnable.run(); | ||
|
|
||
| stdPs.flush(); | ||
| String std = stdBaos.toString(); | ||
| System.setOut(outBackup); | ||
| System.out.print(std); | ||
|
|
||
| errPs.flush(); | ||
| String err = errBaos.toString(); | ||
| System.setErr(errBackup); | ||
| System.err.print(err); | ||
|
|
||
| return new Output(std, err); | ||
| } | ||
|
|
||
| public String getStd() { | ||
| return std; | ||
| } | ||
|
|
||
| public String getErr() { | ||
| return err; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package org.opendatakit.validate; | ||
|
|
||
| import java.net.URISyntaxException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| public class TestUtils { | ||
| public static Path getPathOf(String filename) throws URISyntaxException { | ||
| return Paths.get(ValidateExternalSecondaryInstancesTest.class.getResource(filename.startsWith("/") ? filename : "/" + filename).toURI()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0"?> | ||
| <h:html xmlns="http://www.w3.org/2002/xforms" xmlns:odk="http://www.opendatakit.org/xforms" | ||
| xmlns:jr="http://openrosa.org/javarosa" xmlns:h="http://www.w3.org/1999/xhtml" | ||
| xmlns:orx="http://openrosa.org/xforms"> | ||
| <h:head> | ||
| <h:title>Form with intersects</h:title> | ||
| <model> | ||
| <instance> | ||
| <data id="intersects"> | ||
| <q1/> | ||
| <calc/> | ||
| </data> | ||
| </instance> | ||
| <bind nodeset="/data/calc" calculate="intersects(/data/q1)" type="string"/> | ||
| </model> | ||
| </h:head> | ||
| <h:body> | ||
| <input ref="/data/q1"/> | ||
| </h:body> | ||
| </h:html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really see any extra risk in returning the real prototype here and not overriding
rawArgs:In #100, you say:
That's true that Collect does extra verification as well, but it also just uses the JavaRosa prototype type check before it does any of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because all types can be coerced to
String, the only difference with doing that is that it would enforce a single argument. I'm on the fence about that. On one hand, it's nice to reject extra arguments now. On the other, it means that we have to remember to change the prototype as we add tointersects. We've talked about detecting intersections across shapes. I still lean towards leaving this lenient but if you still feel strongly after reading this I'll change it.