Skip to content

Commit 699c6f9

Browse files
Add a test case for the alignment example provided by @FedericoPonzi
1 parent 5f846ce commit 699c6f9

File tree

2 files changed

+37
-4
lines changed
  • src
    • main/java/com/opencastsoftware/prettier4j
    • test/java/com/opencastsoftware/prettier4j

2 files changed

+37
-4
lines changed

src/main/java/com/opencastsoftware/prettier4j/Doc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: © 2022-2024 Opencast Software Europe Ltd <https://opencastsoftware.com>
2+
* SPDX-FileCopyrightText: © 2022-2025 Opencast Software Europe Ltd <https://opencastsoftware.com>
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
package com.opencastsoftware.prettier4j;
@@ -446,10 +446,10 @@ public String text() {
446446
@Override
447447
public Doc append(Doc other) {
448448
// By string concat equivalency law
449-
/*if (other instanceof Text) {
449+
if (other instanceof Text) {
450450
Text otherText = (Text) other;
451451
return text(this.text() + otherText.text());
452-
} else*/ if (other instanceof Empty) {
452+
} else if (other instanceof Empty) {
453453
// By left unit law
454454
return this;
455455
}

src/test/java/com/opencastsoftware/prettier4j/DocTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: © 2022-2024 Opencast Software Europe Ltd <https://opencastsoftware.com>
2+
* SPDX-FileCopyrightText: © 2022-2025 Opencast Software Europe Ltd <https://opencastsoftware.com>
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
package com.opencastsoftware.prettier4j;
@@ -276,6 +276,39 @@ void testNestedBracketFlattening() {
276276
assertThat(inputDoc.render(20), is(equalTo(expectedWidth20)));
277277
}
278278

279+
@Test
280+
void testAlignWithMultipleLines() {
281+
String expected =
282+
"∧ ∨ A ∨ B\n" +
283+
" ∨ C\n" +
284+
"∧ ∨ D\n" +
285+
" ∨ E ∧ F\n" +
286+
" ∨ G";
287+
288+
// (A ∨ B) ∨ C
289+
List<Doc> left = List.of(
290+
text("A").appendSpace(text("∨")).appendSpace(text("B")),
291+
text("C")
292+
);
293+
294+
// D ∨ (E ∧ F) ∨ G
295+
List<Doc> right = List.of(
296+
text("D"),
297+
text("E").appendSpace(text("∧")).appendSpace(text("F")),
298+
text("G")
299+
);
300+
301+
Doc alignedLeft = align(text("∨").appendSpace(intersperse(line().append(text("∨ ")), left)));
302+
Doc leftJunctions = text("∧").appendSpace(alignedLeft);
303+
304+
Doc alignedRight = align(text("∨").appendSpace(intersperse(line().append(text("∨ ")), right)));
305+
Doc rightJunctions = text("∧").appendSpace(alignedRight);
306+
307+
String result = leftJunctions.appendLine(rightJunctions).render(80);
308+
309+
assertThat(result, is(equalTo(expected)));
310+
}
311+
279312
@Test
280313
void testMarginWithLineSeparator() {
281314
assertThrows(IllegalArgumentException.class, () -> {

0 commit comments

Comments
 (0)