Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 27, 2023
1 parent d1a6ad9 commit 6cfe82c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static FontDef getFontDef(Map<? super String, Object> 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)));
Expand Down
32 changes: 32 additions & 0 deletions utility/src/test/java/com/dua3/utility/text/FontDefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Font> 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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<TextAttributes> 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)
)
);
}
}

0 comments on commit 6cfe82c

Please sign in to comment.