-
Notifications
You must be signed in to change notification settings - Fork 240
Issue 1310 test #1312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Symeon94
wants to merge
7
commits into
FXMisc:master
Choose a base branch
from
Symeon94:issue-1310-restyle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Issue 1310 test #1312
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
richtextfx/src/test/java/org/fxmisc/richtext/model/StyleSpanTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.fxmisc.richtext.model; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| public class StyleSpanTest { | ||
| private void checkSpan(StyleSpan<String> span, String style, int pos, int len) { | ||
| assertEquals(len, span.getLength(), "Incorrect length"); | ||
| assertEquals(pos, span.getStart(), "Incorrect position"); | ||
| assertEquals(style, span.getStyle(), "Incorrect style"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Style do not equal if their length or style is different") | ||
| void equals() { | ||
| assertEquals( | ||
| new StyleSpan<>("myStyle", 2, 3), | ||
| new StyleSpan<>("myStyle", 1, 3) | ||
| ); | ||
| assertNotEquals( | ||
| new StyleSpan<>("myStyle", 2, 3), | ||
| new StyleSpan<>("myStyle", 2, 2) | ||
| ); | ||
| assertNotEquals( | ||
| new StyleSpan<>("myStyle", 2, 3), | ||
| new StyleSpan<>("other", 2, 3) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Create a valid span") | ||
| void createValidSpan() { | ||
| StyleSpan<String> a = new StyleSpan<>("myStyle", 10); | ||
| StyleSpan<String> b = new StyleSpan<>("myStyle", 0, 10); | ||
| checkSpan(a, "myStyle", 0, 10); | ||
| checkSpan(b, "myStyle", 0, 10); | ||
| assertEquals(a, b); | ||
| assertEquals(a.hashCode(), b.hashCode()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Move span to a different location") | ||
| void moveSpan() { | ||
| StyleSpan<String> a = new StyleSpan<>("myStyle", 10); | ||
| StyleSpan<String> b = new StyleSpan<>("myStyle", 2, 10); | ||
| assertEquals(a, b); // position does not matter for equality | ||
| assertEquals(a.hashCode(), b.hashCode()); | ||
| StyleSpan<String> c = a.moveTo(2); | ||
| assertEquals(c, b); | ||
| checkSpan(a, "myStyle", 0, 10); | ||
| checkSpan(b, "myStyle", 2, 10); | ||
| checkSpan(c, "myStyle", 2, 10); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Cannot create a span with a negative length") | ||
| void cannotCreateWithNegativeLength() { | ||
| // This is bad practice to throw exception in constructor, but that's the way it is. Other solutions would | ||
| // have been to use a static create method, a factory, or to change length to 0 | ||
| assertThrows(IllegalArgumentException.class, () -> new StyleSpan<>("test", -1)); | ||
| // This is not consistent, as it doesn't throw | ||
| checkSpan(new StyleSpan<>("test", 0, -1), "test", 0, -1); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
richtextfx/src/test/java/org/fxmisc/richtext/model/StyleSpansBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package org.fxmisc.richtext.model; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.fxmisc.richtext.model.StyleSpansChecker.checkStyle; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| public class StyleSpansBuilderTest { | ||
| @Test | ||
| @DisplayName("Cannot create an empty style span") | ||
| void empty() { | ||
| assertThrows(IllegalStateException.class, () -> new StyleSpansBuilder<>().create()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Creating a single-style style span") | ||
| void createStyleSpan() { | ||
| StyleSpans<String> style = new StyleSpansBuilder<String>().add("alpha", 10).create(); | ||
| checkStyle(style, 10, new String[] {"alpha"}, 0, 10); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Adding only empty styles") | ||
| void onlyEmptyStyles() { | ||
| StyleSpans<String> style = new StyleSpansBuilder<String>() | ||
| .add("alpha", 0) | ||
| .add("beta", 0) | ||
| .add("charlie", 0) | ||
| .create(); | ||
| checkStyle(style, 0, new String[] {"alpha"}, 0, 0); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Adding style on only empty, overwrites it") | ||
| void nonEmptyOnEmpty() { | ||
| StyleSpans<String> style = new StyleSpansBuilder<String>() | ||
| .add("alpha", 0) | ||
| .add("beta", 0) | ||
| .add("charlie", 0) | ||
| .add("delta", 1) | ||
| .create(); | ||
| checkStyle(style, 1, new String[] {"delta"}, 0, 1); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Add a list of only one style will merge them all") | ||
| void addListOfOneStyle() { | ||
| StyleSpans<String> style = new StyleSpansBuilder<String>() | ||
| .addAll(List.of(new StyleSpan<>("alpha", 1), new StyleSpan<>("alpha", 2), new StyleSpan<>("alpha", 3))) | ||
| .create(); | ||
| checkStyle(style, 6, new String[] {"alpha"}, 0, 6); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Creating a multi-style style span") | ||
| void multiStyleSpan() { | ||
| StyleSpans<String> style = new StyleSpansBuilder<String>() | ||
| .add("alpha", 1) | ||
| .add("alpha", 2) | ||
| .add("beta", 3) | ||
| .add("delta", 0) | ||
| .add("charlie", 4) | ||
| .add("alpha", 5) | ||
| .create(); | ||
| checkStyle(style, 15, new String[] {"alpha", "beta", "charlie", "alpha"}, 0, 3, 3, 6, 6, 10, 10, 15); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
richtextfx/src/test/java/org/fxmisc/richtext/model/StyleSpansChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package org.fxmisc.richtext.model; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class StyleSpansChecker<T> { | ||
| private final StyleSpans<T> styleSpans; | ||
|
|
||
| public StyleSpansChecker(StyleSpans<T> styleSpans) { | ||
| this.styleSpans = styleSpans; | ||
| } | ||
|
|
||
| public void check(int length, T[] styles, int... ranges) { | ||
| if(ranges.length % 2 == 1 || styles.length != ranges.length / 2) { | ||
| throw new IllegalArgumentException("Ranges must come in pair [start;end] and correspond to the style count"); | ||
| } | ||
| assertEquals(length, styleSpans.length()); | ||
| assertEquals(ranges.length/2, styleSpans.getSpanCount(), "Style segment count invalid"); | ||
| for (int i = 0; i < ranges.length/2 ; i++) { | ||
| StyleSpan<T> style = styleSpans.getStyleSpan(i); | ||
| assertEquals(ranges[i*2], style.getStart(), "Start not matching for " + i); | ||
| assertEquals(ranges[i*2 + 1] - ranges[i*2], style.getLength(), "Length not matching for " + i); | ||
| assertEquals(styles[i], style.getStyle(), "Incorrect style for " + i); | ||
| } | ||
| } | ||
|
|
||
| public static <T> void checkStyle(Paragraph<?, ?, T> paragraph, int length, T[] styles, int... ranges) { | ||
| checkStyle(paragraph.getStyleSpans(), length, styles, ranges); | ||
| } | ||
|
|
||
| public static <T> void checkStyle(StyleSpans<T> styleSpans, int length, T[] styles, int... ranges) { | ||
| new StyleSpansChecker<T>(styleSpans).check(length, styles, ranges); | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
richtextfx/src/test/java/org/fxmisc/richtext/model/StyleSpansTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package org.fxmisc.richtext.model; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.fxmisc.richtext.model.StyleSpansChecker.checkStyle; | ||
|
|
||
| public class StyleSpansTest { | ||
| private StyleSpans<String> create(String style, int len) { | ||
| StyleSpansBuilder<String> builder = new StyleSpansBuilder<>(); | ||
| builder.add(style, len); | ||
| return builder.create(); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Extract subview") | ||
| class SubViewTest { | ||
| @Test | ||
| @DisplayName("Extract subview from a single style span should return the same style") | ||
| void createSubViewFromSingleStyle() { | ||
| StyleSpans<String> subStyle = create("text", 10).subView(2, 8); | ||
| checkStyle(subStyle, 6, new String[] {"text"}, 0, 6); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Extract subview cutting in different styles") | ||
| void createSubViewFromMultiple() { | ||
| StyleSpansBuilder<String> builder = new StyleSpansBuilder<>(); | ||
| builder.add("alpha", 6); | ||
| builder.add("beta", 7); | ||
| builder.add("charlie", 8); | ||
| StyleSpans<String> styleSpans = builder.create(); | ||
| checkStyle(styleSpans, 21, new String[] {"alpha", "beta", "charlie"}, 0, 6, 6, 13, 13, 21); | ||
| checkStyle(styleSpans.subView(6, 13), 7, new String[] {"beta"}, 0, 7); | ||
| // Empty | ||
| checkStyle(styleSpans.subView(7, 7), 0, new String[] {"beta"}, 0, 0); // Strange, why isn't it empty? | ||
| // Inverted indexes | ||
| checkStyle(styleSpans.subView(14, 5), 0, new String[] {"charlie"}, 0, 0); | ||
| // Out of bound | ||
| checkStyle(styleSpans.subView(-1,22), 23, new String[] {"alpha", "beta", "charlie"}, 0, 6, 6, 13, 0, 10); | ||
| // Bug | ||
| checkStyle(styleSpans.subView(6, 14), 8, new String[] {"beta", "charlie"}, 0, 7, 0, 1); | ||
| checkStyle(styleSpans.subView(5, 13), 8, new String[] {"alpha", "beta"}, 0, 1, 0, 7); | ||
| checkStyle(styleSpans.subView(5, 14), 9, new String[] {"alpha", "beta", "charlie"}, 0, 1, 6, 13, 0, 1); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of these tests are clear bugs |
||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Append") | ||
| class AppendTest { | ||
| private StyleSpans<String> base; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| base = create("text", 10); | ||
| checkStyle(base, 10, new String[]{"text"}, 0, 10); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Append empty style at the end of an existing style does not do anything") | ||
| void appendEmpty() { | ||
| checkStyle(base.append("text", 0), 10, | ||
| new String[]{"text"}, 0, 10); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Append on an empty style will create a new style with the provided values") | ||
| void appendOnEmpty() { | ||
| checkStyle(create("test", 0).append("text", 2), 2, | ||
| new String[]{"text"}, 0, 2); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Append a different style to an existing style span should add it at the end") | ||
| void appendDifferentStyle() { | ||
| checkStyle(base.append("else", 2), 12, | ||
| new String[]{"text", "else"}, 0, 10, 0, 2); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Append an existing style at the end of a style should extend the existing one") | ||
| void appendSameStyle() { | ||
| checkStyle(base.append("text", 2), 12, | ||
| new String[]{"text"}, 0, 12); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change that in another PR.