-
Notifications
You must be signed in to change notification settings - Fork 105
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
Use first repeat instance(s) for jr:choice-name choices #758
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 |
---|---|---|
|
@@ -16,6 +16,25 @@ | |
|
||
package org.javarosa.core.model; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.javarosa.core.log.WrappedException; | ||
import org.javarosa.core.model.TriggerableDag.EventNotifierAccessor; | ||
import org.javarosa.core.model.actions.ActionController; | ||
|
@@ -67,26 +86,6 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
/** | ||
* Definition of a form. This has some meta data about the form definition and a | ||
* collection of groups together with question branching or skipping rules. | ||
|
@@ -866,10 +865,18 @@ public Object eval(Object[] args, EvaluationContext ec) { | |
// the situations where that context would be used don't make sense for trying to reverse a | ||
// select value back to a label in an unrelated expression | ||
if (ref.isAmbiguous()) { | ||
// SurveyCTO: We need a absolute "ref" to populate the dynamic choices, | ||
// like we do when we populate those at FormEntryPrompt (line 251). | ||
// The "ref" here is ambiguous, so we need to make it concrete first. | ||
// ref, the reference used to specify where choices are defined, could be an absolute | ||
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. This |
||
// reference to a repeat nodeset. In that case, we need to convert that nodeset ref | ||
// into a node ref. First try to contextualize based on the current repeat in case the | ||
// choice-name call is from inside a repeat. Then use position 1 for any repeats that | ||
// weren't contextualized (this is what a standards-compliant XPath engine would always do). | ||
ref = ref.contextualize(ec.getContextRef()); | ||
|
||
for (int i = 0; i < ref.size(); i++) { | ||
if (ref.getMultiplicity(i) == TreeReference.INDEX_UNBOUND) { | ||
ref.setMultiplicity(i, TreeReference.DEFAULT_MULTIPLICITY); | ||
} | ||
} | ||
} | ||
choices = itemset.getChoices(f, ref); | ||
} else { // static choices | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ | |
import static org.javarosa.core.util.XFormsElement.item; | ||
import static org.javarosa.core.util.XFormsElement.mainInstance; | ||
import static org.javarosa.core.util.XFormsElement.model; | ||
import static org.javarosa.core.util.XFormsElement.repeat; | ||
import static org.javarosa.core.util.XFormsElement.select1; | ||
import static org.javarosa.core.util.XFormsElement.select1Dynamic; | ||
import static org.javarosa.core.util.XFormsElement.t; | ||
import static org.javarosa.core.util.XFormsElement.title; | ||
import static org.javarosa.test.utils.ResourcePathHelper.r; | ||
|
@@ -117,4 +119,75 @@ public class ChoiceNameTest { | |
scenario.answer("/data/city", "grenoble"); | ||
assertThat(scenario.answerOf("/data/city_name"), is(stringAnswer("Grenoble"))); | ||
} | ||
|
||
@Test public void choiceNameCallWithIndexedRepeatAndStaticChoices_worksWithMultipleRepeats() throws IOException, XFormParser.ParseException { | ||
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. This is not affected by the code change. It's there to document behavior that has always worked. |
||
Scenario scenario = Scenario.init("Static choices in repeat", html( | ||
head( | ||
title("Static choices in repeat"), | ||
model( | ||
mainInstance(t("data id=\"static-choices-repeat\"", | ||
t("thing", | ||
t("choice")), | ||
t("choice1_label") | ||
)), | ||
|
||
bind("/data/thing/choice").type("string"), | ||
bind("/data/choice1_label").type("string").calculate("jr:choice-name(indexed-repeat(/data/thing/choice, /data/thing, 1),'/data/thing/choice')") | ||
) | ||
), | ||
body( | ||
repeat("/data/thing", | ||
select1("/data/thing/choice", | ||
item("choice1", "Choice1"), | ||
item("choice2", "Choice2")) | ||
)))); | ||
|
||
scenario.next(); | ||
scenario.next(); | ||
scenario.next(); | ||
scenario.createNewRepeat(); | ||
|
||
scenario.answer("/data/thing[1]/choice", "choice1"); | ||
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1"))); | ||
|
||
scenario.answer("/data/thing[2]/choice", "choice2"); | ||
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1"))); | ||
} | ||
|
||
@Test public void choiceNameCallWithIndexedRepeatAndDynamicChoices_worksWithMultipleRepeats() throws IOException, XFormParser.ParseException { | ||
Scenario scenario = Scenario.init("Dynamic choices in repeat", html( | ||
head( | ||
title("Dynamic choices in repeat"), | ||
model( | ||
mainInstance(t("data id=\"dynamic-choices-repeat\"", | ||
t("thing", | ||
t("choice")), | ||
t("choice1_label") | ||
)), | ||
|
||
t("instance id=\"choices\"", | ||
t("root", | ||
item("choice1", "Choice1"), | ||
item("choice2", "Choice2")) | ||
), | ||
bind("/data/thing/choice").type("string"), | ||
bind("/data/choice1_label").type("string").calculate("jr:choice-name(indexed-repeat(/data/thing/choice, /data/thing, 1),'/data/thing/choice')") | ||
) | ||
), | ||
body( | ||
repeat("/data/thing", | ||
select1Dynamic("/data/thing/choice", "instance('choices')/root/item")) | ||
))); | ||
|
||
scenario.next(); | ||
scenario.next(); | ||
scenario.next(); | ||
scenario.createNewRepeat(); | ||
|
||
scenario.answer("/data/thing[1]/choice","choice1"); | ||
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1"))); | ||
|
||
scenario.answer("/data/thing[2]/choice", "choice2"); | ||
assertThat(scenario.answerOf("/data/choice1_label"), is(stringAnswer("Choice1"))); | ||
} | ||
} |
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 think I'm likely the one who messed up this order previously. 😬 My IDE should now enforce the order that's mostly consistent across existing files.