Skip to content
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

Fixed using raw strings as requiredMsg #731

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/org/javarosa/form/api/FormEntryPrompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import org.javarosa.core.util.NoLocalizedTextException;
import org.javarosa.core.util.UnregisteredLocaleException;
import org.javarosa.formmanager.view.IQuestionWidget;
import org.javarosa.xform.parse.XFormParser;
import org.javarosa.xpath.XPathParseTool;
import org.javarosa.xpath.expr.XPathExpression;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -185,6 +188,41 @@ public String getAnswerText() {
}
}

public String getRequiredText() {
// look for the text under the requiredMsg bind attribute
String requiredMsgText = form.getMainInstance().resolveReference(index.getReference()).getBindAttributeValue(XFormParser.NAMESPACE_JAVAROSA,
"requiredMsg");
if (requiredMsgText != null) {
XPathExpression xpathRequiredMsg;
try {
xpathRequiredMsg = XPathParseTool.parseXPath("string(" + requiredMsgText + ")");
} catch (Exception e) {
// Expected in probably most cases.
// This is a string literal, so no need to evaluate anything.
return requiredMsgText;
}

if (xpathRequiredMsg != null) {
try {
TreeElement treeElement = form.getMainInstance().resolveReference(
index.getReference());
EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(),
treeElement.getRef());
Object value = xpathRequiredMsg.eval(form.getMainInstance(), ec);
if (!value.equals("")) {
return (String) value;
}
return requiredMsgText;
} catch (Exception e) {
return requiredMsgText;
}
} else {
return requiredMsgText;
}
}
return null;
}

public String getConstraintText() {
return getConstraintText(null);
}
Expand Down
43 changes: 0 additions & 43 deletions src/test/java/org/javarosa/form/api/ConstraintTextTest.java

This file was deleted.

168 changes: 168 additions & 0 deletions src/test/java/org/javarosa/form/api/FormEntryPromptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.javarosa.core.util.BindBuilderXFormsElement.bind;
import static org.javarosa.core.util.XFormsElement.body;
import static org.javarosa.core.util.XFormsElement.head;
import static org.javarosa.core.util.XFormsElement.html;
Expand Down Expand Up @@ -148,4 +150,170 @@ public void getSelectItemText_onSelectionsInRepeatInstances_returnsLabelInnerTex
assertThat(questionPrompt.getAnswerText(), is("A"));
}
//endregion

@Test
public void getRequiredText_shouldReturnNullIfRequiredTextNotSpecified() throws XFormParser.ParseException, IOException {
Scenario scenario = Scenario.init("Required questions", html(
head(
title("Required questions"),
model(
mainInstance(
t("data id='required-questions'",
t("q1")
)
),
bind("/data/q1").type("int")
)
),
body(
input("/data/q1")
)
));

scenario.next();
assertThat(scenario.getFormEntryPromptAtIndex().getRequiredText(), is(nullValue()));
}

@Test
public void getRequiredText_shouldReturnRequiredTextIfSpecifiedUsingItextFunction() throws XFormParser.ParseException, IOException {
Scenario scenario = Scenario.init("Required questions", html(
head(
title("Required questions"),
model(
t("itext",
t("translation lang='en'",
t("text id='/data/q1:requiredMsg'",
t("value", "message")
)
)
),
mainInstance(
t("data id='required-questions'",
t("q1")
)
),
bind("/data/q1").type("int").withAttribute("jr", "requiredMsg", "jr:itext('/data/q1:requiredMsg')")
)
),
body(
input("/data/q1")
)
));

scenario.next();
assertThat(scenario.getFormEntryPromptAtIndex().getRequiredText(), is("message"));
}

@Test
public void getRequiredText_shouldReturnRequiredTextIfSpecifiedUsingRawString() throws XFormParser.ParseException, IOException {
Scenario scenario = Scenario.init("Required questions", html(
head(
title("Required questions"),
model(
t("itext",
t("translation lang='en'",
t("text id='/data/q1:requiredMsg'",
t("value", "Your message")
)
)
),
mainInstance(
t("data id='required-questions'",
t("q1")
)
),
bind("/data/q1").type("int").withAttribute("jr", "requiredMsg", "message")
)
),
body(
input("/data/q1")
)
));

scenario.next();
assertThat(scenario.getFormEntryPromptAtIndex().getRequiredText(), is("message"));
}

@Test
public void getConstraintText_shouldReturnNullIfRConstraintTextNotSpecified() throws XFormParser.ParseException, IOException {
Scenario scenario = Scenario.init("Constrained questions", html(
head(
title("Constrained questions"),
model(
mainInstance(
t("data id='constrained-questions'",
t("q1")
)
),
bind("/data/q1").type("int").constraint(". > 10")
)
),
body(
input("/data/q1")
)
));

scenario.next();
assertThat(scenario.getFormEntryPromptAtIndex().getConstraintText(), is(nullValue()));
}

@Test
public void getConstraintText_shouldReturnConstraintTextIfSpecifiedUsingItextFunction() throws XFormParser.ParseException, IOException {
Scenario scenario = Scenario.init("Constrained questions", html(
head(
title("Constrained questions"),
model(
t("itext",
t("translation lang='en'",
t("text id='/data/q1:constraintMsg'",
t("value", "message")
)
)
),
mainInstance(
t("data id='constrained-questions'",
t("q1")
)
),
bind("/data/q1").type("int").constraint(". > 10").withAttribute("jr", "constraintMsg", "jr:itext('/data/q1:constraintMsg')")
)
),
body(
input("/data/q1")
)
));

scenario.next();
assertThat(scenario.getFormEntryPromptAtIndex().getConstraintText(), is("message"));
}

@Test
public void getConstraintText_shouldReturnConstraintTextIfSpecifiedUsingRawString() throws XFormParser.ParseException, IOException {
Scenario scenario = Scenario.init("Constrained questions", html(
head(
title("Constrained questions"),
model(
t("itext",
t("translation lang='en'",
t("text id='/data/q1:constraintMsg'",
t("value", "Your message")
)
)
),
mainInstance(
t("data id='constrained-questions'",
t("q1")
)
),
bind("/data/q1").type("int").constraint(". > 10").withAttribute("jr", "constraintMsg", "message")
)
),
body(
input("/data/q1")
)
));

scenario.next();
assertThat(scenario.getFormEntryPromptAtIndex().getConstraintText(), is("message"));
}
}

This file was deleted.