|
| 1 | +package com.demcha.compose.document.backend.semantic; |
| 2 | + |
| 3 | +import com.demcha.compose.GraphCompose; |
| 4 | +import com.demcha.compose.document.api.DocumentSession; |
| 5 | +import com.demcha.compose.document.node.ListMarker; |
| 6 | +import com.demcha.compose.document.style.DocumentInsets; |
| 7 | +import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| 8 | +import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; |
| 11 | + |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.util.List; |
| 14 | +import java.util.function.Consumer; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +/** |
| 19 | + * PDF ↔ DOCX list parity: the semantic Word export must resolve markers and |
| 20 | + * item text through the same shared rules as fixed-layout rendering — the |
| 21 | + * {@link ListMarker#defaultForDepth(int)} cascade for nested fallbacks and |
| 22 | + * {@link ListMarker#normalizeItemText(String, boolean)} for flat items — |
| 23 | + * so both outputs of one session agree. |
| 24 | + */ |
| 25 | +@DisabledIfSystemProperty(named = "no.poi", matches = "true", |
| 26 | + disabledReason = "DocxSemanticBackend requires poi-ooxml; the no-poi profile validates the rest of the suite without it") |
| 27 | +class DocxListParityTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void nestedFallbackFollowsTheDepthCascade() throws Exception { |
| 31 | + List<String> texts = exportTexts(flow -> flow |
| 32 | + .addList(list -> list |
| 33 | + .name("Outline") |
| 34 | + .addItem("alpha", l1 -> l1 |
| 35 | + .addItem("beta", l2 -> l2 |
| 36 | + .addItem("gamma"))))); |
| 37 | + |
| 38 | + assertThat(texts).contains("• alpha", " ◦ beta", " ▪ gamma"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void explicitMarkersStillBeatTheCascade() throws Exception { |
| 43 | + List<String> texts = exportTexts(flow -> flow |
| 44 | + .addList(list -> list |
| 45 | + .name("Outline") |
| 46 | + .markerFor(1, ListMarker.custom("→")) |
| 47 | + .addItem("alpha", l1 -> l1.addItem("beta")))); |
| 48 | + |
| 49 | + assertThat(texts).contains(" → beta"); |
| 50 | + assertThat(texts).doesNotContain(" ◦ beta"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void flatItemsStripAuthorTypedMarkers() throws Exception { |
| 55 | + List<String> texts = exportTexts(flow -> flow |
| 56 | + .addList("- dashed", "• bulleted", "* starred", "+ plussed")); |
| 57 | + |
| 58 | + assertThat(texts).contains("• dashed", "• bulleted", "• starred", "• plussed"); |
| 59 | + assertThat(texts).noneMatch(t -> t.startsWith("• - ") || t.startsWith("• • ")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void boldLeadIsNotMistakenForAMarker() throws Exception { |
| 64 | + List<String> texts = exportTexts(flow -> flow |
| 65 | + .addList("**bold** lead stays intact")); |
| 66 | + |
| 67 | + assertThat(texts).contains("• **bold** lead stays intact"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void blankFlatItemsAreDropped() throws Exception { |
| 72 | + List<String> texts = exportTexts(flow -> flow |
| 73 | + .addList("kept", "", " ")); |
| 74 | + |
| 75 | + assertThat(texts).contains("• kept"); |
| 76 | + // No marker-only paragraphs for the blank items. |
| 77 | + assertThat(texts).noneMatch(t -> t.trim().equals("•")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void normalizeMarkersFalsePreservesRawItems() throws Exception { |
| 82 | + List<String> texts = exportTexts(flow -> flow |
| 83 | + .addList(list -> list |
| 84 | + .name("Raw") |
| 85 | + .normalizeMarkers(false) |
| 86 | + .items("- raw dash survives"))); |
| 87 | + |
| 88 | + assertThat(texts).contains("• - raw dash survives"); |
| 89 | + } |
| 90 | + |
| 91 | + private static List<String> exportTexts( |
| 92 | + Consumer<com.demcha.compose.document.dsl.PageFlowBuilder> author) throws Exception { |
| 93 | + byte[] docxBytes; |
| 94 | + try (DocumentSession session = GraphCompose.document() |
| 95 | + .pageSize(595, 842) |
| 96 | + .margin(DocumentInsets.of(36)) |
| 97 | + .create()) { |
| 98 | + var flow = session.dsl().pageFlow().name("Flow"); |
| 99 | + author.accept(flow); |
| 100 | + flow.build(); |
| 101 | + docxBytes = session.export(new DocxSemanticBackend()); |
| 102 | + } |
| 103 | + try (XWPFDocument document = new XWPFDocument(new ByteArrayInputStream(docxBytes))) { |
| 104 | + return document.getParagraphs().stream() |
| 105 | + .map(XWPFParagraph::getText) |
| 106 | + .toList(); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments