|
| 1 | +import re |
| 2 | + |
| 3 | +import polars as pl |
| 4 | + |
| 5 | +import rtflite as rtf |
| 6 | + |
| 7 | + |
| 8 | +def test_page_by_column_alignment(): |
| 9 | + """Test that column properties are correctly aligned when page_by is used.""" |
| 10 | + df = pl.DataFrame( |
| 11 | + { |
| 12 | + "section": ["-----", "Age", "Age"], |
| 13 | + "item": ["Participant in Population", " <60", " >=60"], |
| 14 | + "value": [55, 25, 30], |
| 15 | + } |
| 16 | + ) |
| 17 | + |
| 18 | + doc = rtf.RTFDocument( |
| 19 | + df=df, |
| 20 | + rtf_body=rtf.RTFBody( |
| 21 | + page_by="section", |
| 22 | + col_rel_width=[1], |
| 23 | + text_justification=["l", "l", "c"], |
| 24 | + border_top=["single", "", ""], |
| 25 | + border_bottom=["single", "", ""], |
| 26 | + ), |
| 27 | + ) |
| 28 | + |
| 29 | + rtf_content = doc.rtf_encode() |
| 30 | + |
| 31 | + # We look for the row containing "55" (value column) and "Participant in Population" |
| 32 | + rows = rtf_content.split("\\row") |
| 33 | + row_with_55 = None |
| 34 | + for row in rows: |
| 35 | + if "55" in row and "Participant in Population" in row: |
| 36 | + row_with_55 = row |
| 37 | + break |
| 38 | + |
| 39 | + assert row_with_55 is not None, "Could not find the row with data" |
| 40 | + |
| 41 | + # Check alignment for "Participant in Population" |
| 42 | + idx_part = row_with_55.find("Participant in Population") |
| 43 | + pre_part = row_with_55[:idx_part] |
| 44 | + aligns_part = re.findall(r"\\q[lcrj]", pre_part) |
| 45 | + |
| 46 | + # Note: The table definition (\trowd...) also contains \trqc etc. but that's row |
| 47 | + # alignment. Cell content alignment is \ql, \qc etc. inside \pard. |
| 48 | + # Usually \pard resets, so we should see \ql inside the \pard block. |
| 49 | + # \trowd... contains \clvertalt etc. but not \ql/\qc usually (unless \clq...?) |
| 50 | + # The output shows: \pard\hyphpar0... \ql ... content. |
| 51 | + |
| 52 | + assert len(aligns_part) > 0, "No alignment found for Participant" |
| 53 | + last_align_part = aligns_part[-1] |
| 54 | + assert last_align_part == "\\ql", ( |
| 55 | + f"Expected \\ql for Participant, got {last_align_part}" |
| 56 | + ) |
| 57 | + |
| 58 | + # Check alignment for "55" |
| 59 | + idx_55 = row_with_55.find("55") |
| 60 | + pre_55 = row_with_55[:idx_55] |
| 61 | + aligns_55 = re.findall(r"\\q[lcrj]", pre_55) |
| 62 | + |
| 63 | + assert len(aligns_55) > 0, "No alignment found for 55" |
| 64 | + last_align_55 = aligns_55[-1] |
| 65 | + assert last_align_55 == "\\qc", f"Expected \\qc for 55, got {last_align_55}" |
0 commit comments