From 6cfe82c585efd5ef26156d5cd0a8bb7a07d87019 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Mon, 27 Nov 2023 08:24:09 +0100 Subject: [PATCH] add unit tests --- README.md | 1 + .../com/dua3/utility/text/TextAttributes.java | 1 + .../com/dua3/utility/text/FontDefTest.java | 32 +++++++++++ .../dua3/utility/text/TextAttributesTest.java | 56 ++++++++++++++++++- 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f2817fb9..9dcb7fb1 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ the parameter. - fix build issues on windows‚ - fix smaller issues - fix issues in FontDef CSS conversion +- fix font size not extracted when getting FontDef directly from TextAttributes ### 11.1.3 diff --git a/utility/src/main/java/com/dua3/utility/text/TextAttributes.java b/utility/src/main/java/com/dua3/utility/text/TextAttributes.java index 813b3b19..70cb1f53 100644 --- a/utility/src/main/java/com/dua3/utility/text/TextAttributes.java +++ b/utility/src/main/java/com/dua3/utility/text/TextAttributes.java @@ -96,6 +96,7 @@ public static FontDef getFontDef(Map attributes) { FontDef fd = new FontDef(); DataUtil.ifPresent(attributes, Style.FONT_TYPE, v -> fd.setFamily(String.valueOf(v))); + DataUtil.ifPresent(attributes, Style.FONT_SIZE, v -> fd.setSize((Float) v)); DataUtil.ifPresent(attributes, Style.COLOR, v -> fd.setColor((Color) v)); DataUtil.ifPresent(attributes, Style.FONT_WEIGHT, v -> fd.setBold(Objects.equals(v, Style.FONT_WEIGHT_VALUE_BOLD))); DataUtil.ifPresent(attributes, Style.TEXT_DECORATION_UNDERLINE, v -> fd.setUnderline(Objects.equals(v, Style.TEXT_DECORATION_UNDERLINE_VALUE_LINE))); diff --git a/utility/src/test/java/com/dua3/utility/text/FontDefTest.java b/utility/src/test/java/com/dua3/utility/text/FontDefTest.java index 4e83abc0..255a2809 100644 --- a/utility/src/test/java/com/dua3/utility/text/FontDefTest.java +++ b/utility/src/test/java/com/dua3/utility/text/FontDefTest.java @@ -3,8 +3,13 @@ import com.dua3.utility.data.Color; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -118,4 +123,31 @@ void getCssStyle() { assertEquals(expected, actual); } + + @ParameterizedTest + @MethodSource("fontArguments") + public void testToFontDef(Font font) { + // Test with a Font set + FontDef fd = font.toFontDef(); + assertNotNull(fd); + + assertEquals(font.getFamily(), fd.getFamily()); + assertEquals(font.getSizeInPoints(), fd.getSize()); + assertEquals(font.getColor(), fd.getColor()); + assertEquals(font.isBold(), fd.getBold()); + assertEquals(font.isItalic(), fd.getItalic()); + assertEquals(font.isUnderline(), fd.getUnderline()); + assertEquals(font.isStrikeThrough(), fd.getStrikeThrough()); + } + + private static Stream fontArguments() { + return Stream.of( + new Font("Arial", 12f, Color.BLACK, false, false, false, false), + new Font("Times", 17f, Color.DARKBLUE, true, false, false, false), + new Font("Arial", 12f, Color.BLACK, false, true, false, false), + new Font("Arial", 12f, Color.BLACK, false, false, true, false), + new Font("Arial", 12f, Color.BLACK, false, false, false, true), + new Font("Helvetica", 10f, Color.WHITE, true, true, true, true) + ); + } } \ No newline at end of file diff --git a/utility/src/test/java/com/dua3/utility/text/TextAttributesTest.java b/utility/src/test/java/com/dua3/utility/text/TextAttributesTest.java index 1ac61507..cef76ab5 100644 --- a/utility/src/test/java/com/dua3/utility/text/TextAttributesTest.java +++ b/utility/src/test/java/com/dua3/utility/text/TextAttributesTest.java @@ -1,12 +1,23 @@ package com.dua3.utility.text; import com.dua3.utility.data.Color; -import com.dua3.utility.data.DataUtil; import com.dua3.utility.data.Pair; import org.junit.jupiter.api.Test; -import java.util.*; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -import static org.junit.jupiter.api.Assertions.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Unit test class for TextAttributes @@ -86,4 +97,43 @@ void equalsAndHashCode() { assertNotEquals(ta3.hashCode(), ta4.hashCode()); assertTrue(ta1.equals(ta2)); } + + @ParameterizedTest + @MethodSource("textAttributesArguments") + public void testToFontDef(TextAttributes ta) { + FontDef fd = TextAttributes.getFontDef(ta); + assertNotNull(fd); + assertEquals(ta.get(Style.FONT_TYPE), fd.getFamily()); + assertEquals(ta.get(Style.FONT_SIZE), fd.getSize()); + assertEquals(ta.get(Style.COLOR), fd.getColor()); + assertEquals(Optional.ofNullable(ta.get(Style.FONT_WEIGHT)).map(s -> s.equals(Style.FONT_WEIGHT_VALUE_BOLD)).orElse(null), fd.getBold()); + assertEquals(Optional.ofNullable(ta.get(Style.TEXT_DECORATION_UNDERLINE)).map(s -> s.equals(Style.TEXT_DECORATION_UNDERLINE_VALUE_LINE)).orElse(null), fd.getUnderline()); + assertEquals(Optional.ofNullable(ta.get(Style.TEXT_DECORATION_LINE_THROUGH)).map(s -> s.equals(Style.TEXT_DECORATION_LINE_THROUGH_VALUE_LINE)).orElse(null), fd.getStrikeThrough()); + assertEquals(Optional.ofNullable(ta.get(Style.FONT_STYLE)).map(s -> s.equals(Style.FONT_STYLE_VALUE_ITALIC)).orElse(null), fd.getItalic()); + } + + private static Stream textAttributesArguments() { + return Stream.of( + TextAttributes.none(), + TextAttributes.of(Pair.of(Style.FONT_TYPE, "Arial")), + TextAttributes.of(Pair.of(Style.FONT_SIZE, 17f)), + TextAttributes.of(Pair.of(Style.COLOR, Color.BLUE)), + TextAttributes.of(Pair.of(Style.FONT_WEIGHT, Style.FONT_WEIGHT_VALUE_BOLD)), + TextAttributes.of(Pair.of(Style.FONT_WEIGHT, Style.FONT_WEIGHT_VALUE_NORMAL)), + TextAttributes.of(Pair.of(Style.TEXT_DECORATION_UNDERLINE, Style.TEXT_DECORATION_UNDERLINE_VALUE_LINE)), + TextAttributes.of(Pair.of(Style.TEXT_DECORATION_UNDERLINE, Style.TEXT_DECORATION_UNDERLINE_VALUE_NO_LINE)), + TextAttributes.of(Pair.of(Style.TEXT_DECORATION_LINE_THROUGH, Style.TEXT_DECORATION_LINE_THROUGH_VALUE_LINE)), + TextAttributes.of(Pair.of(Style.TEXT_DECORATION_LINE_THROUGH, Style.TEXT_DECORATION_LINE_THROUGH_VALUE_LINE)), + TextAttributes.of(Pair.of(Style.FONT_STYLE, Style.FONT_STYLE_VALUE_ITALIC)), + TextAttributes.of(Pair.of(Style.FONT_STYLE, Style.FONT_STYLE_VALUE_NORMAL)), + TextAttributes.of(Pair.of(Style.FONT_TYPE, "Arial"), + Pair.of(Style.FONT_SIZE, 17f), + Pair.of(Style.COLOR, Color.BLUE), + Pair.of(Style.FONT_WEIGHT, Style.FONT_WEIGHT_VALUE_BOLD), + Pair.of(Style.TEXT_DECORATION_UNDERLINE, Style.TEXT_DECORATION_UNDERLINE_VALUE_NO_LINE), + Pair.of(Style.TEXT_DECORATION_LINE_THROUGH, Style.TEXT_DECORATION_LINE_THROUGH_VALUE_LINE), + Pair.of(Style.FONT_STYLE, Style.FONT_STYLE_VALUE_ITALIC) + ) + ); + } }