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

Extend randomize seed tests #805

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Changes from 1 commit
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
73 changes: 55 additions & 18 deletions src/test/java/org/javarosa/xpath/expr/RandomizeTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,35 @@ public void stringNumberSeedConvertsWhenUsedInNodesetExpression() throws IOExcep
title("Randomize non-numeric seed"),
model(
mainInstance(t("data id=\"rand-non-numeric\"",
t("choice")
t("choices_numeric_seed"),
t("choices_stringified_numeric_seed")
)),
instance("choices",
item("a", "A"),
item("b", "B")
item("b", "B"),
item("c", "C"),
item("d", "D"),
item("e", "E"),
item("f", "F"),
item("g", "G"),
item("h", "H")
),
bind("/data/choice").type("string")
bind("/data/choices_numeric_seed").type("string"),
bind("/data/choices_stringified_numeric_seed").type("string")
)
),
body(
select1Dynamic("/data/choice", "randomize(instance('choices')/root/item, '1')")
select1Dynamic("/data/choices_numeric_seed", "randomize(instance('choices')/root/item, 1234)"),
select1Dynamic("/data/choices_stringified_numeric_seed", "randomize(instance('choices')/root/item, '1234')")
)
));

assertThat(scenario.choicesOf("/data/choice").get(0).getValue(), is("b"));
String[] shuffled = {"g", "f", "e", "d", "a", "h", "b", "c"};
String[] nodes = {"/data/choices_numeric_seed", "/data/choices_stringified_numeric_seed"};
for (int i = 0; i < shuffled.length; i++) {
for (String node : nodes) {
assertThat(scenario.choicesOf(node).get(i).getValue(), is(shuffled[i]));
}
}
}

@Test
Expand All @@ -54,21 +68,30 @@ public void stringNumberSeedConvertsWhenUsedInCalculate() throws IOException, XF
title("Randomize non-numeric seed"),
model(
mainInstance(t("data id=\"rand-non-numeric\"",
t("choice")
t("choices_numeric_seed"),
t("choices_stringified_numeric_seed")
)),
instance("choices",
item("a", "A"),
item("b", "B")
item("b", "B"),
item("c", "C"),
item("d", "D"),
item("e", "E"),
item("f", "F"),
item("g", "G"),
item("h", "H")
),
bind("/data/choice").type("string").calculate("selected-at(join(' ', randomize(instance('choices')/root/item/label, '1')), 0)")
bind("/data/choices_numeric_seed").type("string").calculate("join('', randomize(instance('choices')/root/item/label, 1234))"),
bind("/data/choices_stringified_numeric_seed").type("string").calculate("join('', randomize(instance('choices')/root/item/label, '1234'))")
)
),
body(
input("/data/choice")
input("/data/choices_numeric_seed"),
input("/data/choices_stringified_numeric_seed")
)
));

assertThat(scenario.answerOf("/data/choice").getDisplayText(), is("B"));
assertThat(scenario.answerOf("/data/choices_numeric_seed").getDisplayText(), is(scenario.answerOf("/data/choices_stringified_numeric_seed").getDisplayText()));
assertThat(scenario.answerOf("/data/choices_numeric_seed").getDisplayText(), is("GFEDAHBC"));
}

@Test
Expand All @@ -82,7 +105,13 @@ public void stringTextSeedConvertsWhenUsedInNodesetExpression() throws IOExcepti
)),
instance("choices",
item("a", "A"),
item("b", "B")
item("b", "B"),
item("c", "C"),
item("d", "D"),
item("e", "E"),
item("f", "F"),
item("g", "G"),
item("h", "H")
),
bind("/data/choice").type("string")
)
Expand All @@ -91,8 +120,10 @@ public void stringTextSeedConvertsWhenUsedInNodesetExpression() throws IOExcepti
select1Dynamic("/data/choice", "randomize(instance('choices')/root/item, 'foo')")
)
));

assertThat(scenario.choicesOf("/data/choice").get(0).getValue(), is("b"));
String[] shuffled = {"e", "a", "d", "b", "h", "g", "c", "f"};
for (int i = 0; i < shuffled.length; i++) {
assertThat(scenario.choicesOf("/data/choice").get(i).getValue(), is(shuffled[i]));
}
}

@Test
Expand All @@ -106,17 +137,23 @@ public void stringTextSeedConvertsWhenUsedInCalculate() throws IOException, XFor
)),
instance("choices",
item("a", "A"),
item("b", "B")
item("b", "B"),
item("c", "C"),
item("d", "D"),
item("e", "E"),
item("f", "F"),
item("g", "G"),
item("h", "H")
),
bind("/data/choice").type("string").calculate("selected-at(join(' ', randomize(instance('choices')/root/item/label, 'foo')), 0)")
bind("/data/choice").type("string").calculate("join('', randomize(instance('choices')/root/item/label, 'foo'))")
)
),
body(
input("/data/choice")
)
));

assertThat(scenario.answerOf("/data/choice").getDisplayText(), is("B"));
assertThat(scenario.answerOf("/data/choice").getDisplayText(), is("EADBHGCF"));
}

@Test
Expand Down