From f9be5e545875001ed0d9f9776baf5512eaccf6cd Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Mon, 11 Dec 2023 22:50:38 +0100 Subject: [PATCH] add unit test --- .../dua3/utility/text/AnsiConverterTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 utility/src/test/java/com/dua3/utility/text/AnsiConverterTest.java diff --git a/utility/src/test/java/com/dua3/utility/text/AnsiConverterTest.java b/utility/src/test/java/com/dua3/utility/text/AnsiConverterTest.java new file mode 100644 index 00000000..ea1a80df --- /dev/null +++ b/utility/src/test/java/com/dua3/utility/text/AnsiConverterTest.java @@ -0,0 +1,81 @@ +package com.dua3.utility.text; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class AnsiConverterTest { + + @Test + void create() { + Style[] styles = { + Style.BOLD, + Style.ITALIC, + Style.UNDERLINE, + Style.LINE_THROUGH, + Style.RED + }; + RichTextBuilder b = new RichTextBuilder(); + b.push(Style.BOLD).append("AnsiConverterTest").pop(Style.BOLD); + b.append("\n"); + for (int i = 0; i < (1 << styles.length); i++) { + StringBuilder sb = new StringBuilder("Styles:"); + for (int j = 0; j < styles.length; j++) { + if ((i & (1 << j)) != 0) { + b.push(styles[j]); + sb.append(" ").append(styles[j].name()); + } + } + b.append(sb); + for (int j = styles.length - 1; j >= 0; j--) { + if ((i & (1 << j)) != 0) { + b.pop(styles[j]); + } + } + b.append("\n"); + } + b.append("normal\n"); + RichText rt = b.toRichText(); + + AnsiConverter converter = AnsiConverter.create(); + String s = converter.convert(rt); + System.out.println(s); + String expected = """ + AnsiConverterTest + Styles: + Styles: bold + Styles: italic + Styles: bold italic + Styles: underline + Styles: bold underline + Styles: italic underline + Styles: bold italic underline + Styles: line-through + Styles: bold line-through + Styles: italic line-through + Styles: bold italic line-through + Styles: underline line-through + Styles: bold underline line-through + Styles: italic underline line-through + Styles: bold italic underline line-through + Styles: red + Styles: bold red + Styles: italic red + Styles: bold italic red + Styles: underline red + Styles: bold underline red + Styles: italic underline red + Styles: bold italic underline red + Styles: line-through red + Styles: bold line-through red + Styles: italic line-through red + Styles: bold italic line-through red + Styles: underline line-through red + Styles: bold underline line-through red + Styles: italic underline line-through red + Styles: bold italic underline line-through red + normal + """; + assertEquals(expected, s); + } +} \ No newline at end of file