Skip to content

Commit

Permalink
Add test for function predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Oct 4, 2023
1 parent 81d8b0b commit c561210
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/test/java/org/javarosa/core/model/PredicateCachingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void firstPredicateInMultipleEqPredicatesAreOnlyEvaluatedOnce() throws Ex
}

@Test
public void repeatedCompPredicatesWithSameAnswerAreOnlyEvaluatedOnce() throws Exception {
public void repeatedCompPredicatesWithSameAbsoluteValueAreOnlyEvaluatedOnce() throws Exception {
Scenario scenario = Scenario.init("Some form", html(
head(
title("Some form"),
Expand Down Expand Up @@ -255,6 +255,42 @@ public void repeatedCompPredicatesWithSameAnswerAreOnlyEvaluatedOnce() throws Ex
assertThat(evaluations, lessThan(4));
}

@Test
public void repeatedIdempotentFuncPredicatesWithSameAbsoluteValueAreOnlyEvaluatedOnce() throws Exception {
Scenario scenario = Scenario.init("Some form", html(
head(
title("Some form"),
model(
mainInstance(t("data id=\"some-form\"",
t("choice"),
t("calculate1"),
t("calculate2")
)),
instance("instance",
item("a", "A"),
item("b", "B")
),
bind("/data/choice").type("string"),
bind("/data/calculate1").type("string")
.calculate("instance('instance')/root/item[regex(value,/data/choice)]/label"),
bind("/data/calculate2").type("string")
.calculate("instance('instance')/root/item[regex(value,/data/choice)]/value")
)
),
body(
input("/data/choice")
)
));

int evaluations = Measure.withMeasure(asList("PredicateEvaluation", "IndexEvaluation"), () -> {
scenario.answer("/data/choice", "a");
scenario.answer("/data/choice", "a");
});

// Check that we do less than size of secondary instance * number of times we answer
assertThat(evaluations, lessThan(4));
}

/**
* A form with multiple secondary instances can have expressions with "equivalent" predicates that filter on
* different sets of children. It's pretty possible to write a bug where these predicates are treated as the same
Expand Down

0 comments on commit c561210

Please sign in to comment.