Skip to content

Commit

Permalink
fix: do not wrap shuffled-index in <span>
Browse files Browse the repository at this point in the history
and combine tests
  • Loading branch information
janbritz committed Jan 14, 2025
1 parent 23660a4 commit b3bf478
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 80 deletions.
11 changes: 2 additions & 9 deletions questionpy_sdk/webserver/question_ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,8 @@ def _replace_shuffled_indices(container: etree._Element, element: etree._Element
index_str = str(index)

# Replace the index element with the new index string
new_text_node = etree.Element("span") # Using span to replace the custom element
new_text_node.text = index_str

if index_element.tail:
new_text_node.tail = index_element.tail

parent = index_element.getparent()
if parent is not None:
parent.replace(index_element, new_text_node)
index_element.tail = index_str + ("" if index_element.tail is None else index_element.tail)
_remove_preserving_tail(index_element)


def _int_to_letter(index: int) -> str:
Expand Down
20 changes: 16 additions & 4 deletions tests/questionpy_sdk/webserver/test_data/shuffle.xhtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:qpy="http://questionpy.org/ns/question" qpy:shuffle-contents="">
<span>Element 1</span>
<span>Element 2</span>
<span>Element 3</span>
<span>Element 4</span>
<span>Element 1, shuffled to <qpy:shuffled-index/></span>
<span>Element 2, shuffled to <qpy:shuffled-index format="123"/></span>
<span>Element 3, shuffled to <qpy:shuffled-index format="abc"/></span>
<span>Element 4, shuffled to <qpy:shuffled-index format="III"/></span>
<div>
Element 5, shuffled to <qpy:shuffled-index/>
<div qpy:shuffle-contents="">
<span>Nested element 1, shuffled to <qpy:shuffled-index/></span>
<span>Nested element 2, shuffled to <qpy:shuffled-index/></span>
</div>
</div>
<div qpy:shuffle-contents="">
<!-- Nested shuffle being a direct child is an edge case we need to support. -->
<span>Nested element 1, shuffled to <qpy:shuffled-index/></span>
<span>Nested element 2, shuffled to <qpy:shuffled-index/></span>
</div>
</div>

This file was deleted.

16 changes: 0 additions & 16 deletions tests/questionpy_sdk/webserver/test_data/shuffled-index.xhtml

This file was deleted.

59 changes: 16 additions & 43 deletions tests/questionpy_sdk/webserver/test_question_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,53 +312,26 @@ def test_should_format_floats_in_en(renderer: QuestionUIRenderer) -> None:

@pytest.mark.ui_file("shuffle")
@pytest.mark.render_params(seed=42)
def test_should_shuffle_the_same_way_in_same_attempt(renderer: QuestionUIRenderer, xml_content: str) -> None:
expected_html, _ = renderer.render()
for _ in range(10):
html, errors = QuestionUIRenderer(xml_content, {}, QuestionDisplayOptions(), seed=42).render()
assert len(errors) == 0
assert html == expected_html, "Shuffled order should remain consistent across renderings with the same seed"


@pytest.mark.ui_file("shuffled-index")
@pytest.mark.render_params(seed=42)
def test_should_replace_shuffled_index(renderer: QuestionUIRenderer) -> None:
expected = """
<div xmlns="http://www.w3.org/1999/xhtml">
<fieldset>
<label>
<input type="radio" name="choice" value="B" class="qpy-input"/>
<span>i</span>. B
</label>
<label>
<input type="radio" name="choice" value="A" class="qpy-input"/>
<span>ii</span>. A
</label>
<label>
<input type="radio" name="choice" value="C" class="qpy-input"/>
<span>iii</span>. C
</label>
</fieldset>
</div>
"""
html, errors = renderer.render()
assert len(errors) == 0
assert_html_is_equal(html, expected)


@pytest.mark.ui_file("shuffled-index-nested")
@pytest.mark.render_params(seed=42)
def test_should_replace_shuffled_index_in_nested(renderer: QuestionUIRenderer) -> None:
def test_should_handle_complex_shuffle_scenario(renderer: QuestionUIRenderer, xml_content: str) -> None:
expected = """
<div xmlns="http://www.w3.org/1999/xhtml">
<p><span>i</span>. B</p>
<p><span>ii</span>. A</p>
<span>Element 4, shuffled to I</span>
<span>Element 2, shuffled to 2</span>
<span>Element 3, shuffled to c</span>
<div>
<p><span>i</span>. D</p>
<p><span>ii</span>. C</p>
Element 5, shuffled to 4
<div>
<span>Nested element 2, shuffled to 1</span>
<span>Nested element 1, shuffled to 2</span>
</div>
</div>
<span>Element 1, shuffled to 5</span>
<div>
<span>Nested element 2, shuffled to 1</span>
<span>Nested element 1, shuffled to 2</span>
</div>
</div>
"""
"""
html, errors = renderer.render()
assert len(errors) == 0
assert_html_is_equal(html, expected)
Expand Down Expand Up @@ -410,7 +383,7 @@ def test_errors_should_be_collected(renderer: QuestionUIRenderer) -> None:
<div xmlns="http://www.w3.org/1999/xhtml">
<span>&lt;qpy:format-float xmlns:qpy="http://questionpy.org/ns/question" xmlns="http://www.w3.org/1999/xhtml" thousands-separator="maybe" precision="invalid"&gt;Unknown value.&lt;/qpy:format-float&gt;</span>
<fieldset>
<label>Invalid shuffle format.<span>1</span>. A</label>
<label>Invalid shuffle format. 1. A</label>
Invalid text placement.
</fieldset>
<div>Missing placeholder.</div>
Expand Down

0 comments on commit b3bf478

Please sign in to comment.