-
Notifications
You must be signed in to change notification settings - Fork 240
Cleanup Paragraph class after having covered it with UT
#1311
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
8
commits into
FXMisc:master
Choose a base branch
from
Symeon94:clean-paragraph
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
Changes from all commits
Commits
Show all changes
8 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| import javafx.scene.control.IndexRange; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ParagraphTest { | ||
|
|
@@ -20,9 +21,9 @@ private <T> void checkStyle(Paragraph<?, ?, T> paragraph, int length, T[] styles | |
| 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(styles[i], style.getStyle(), "Incorrect style for " + 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); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -155,10 +156,21 @@ public void multiStyleParagraphReturnsCorrect_subSequenceOfLength() { | |
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Trim on empty text should return empty") | ||
| public void trimOnEmpty() { | ||
| Paragraph<Void, String, Void> p1 = createTextParagraph(SegmentOps.styledTextOps(), ""); | ||
| assertEquals("", p1.trim(-1).getText()); | ||
| assertEquals("", p1.trim(0).getText()); | ||
| assertEquals("", p1.trim(1).getText()); | ||
| assertEquals("", p1.trim(Integer.MAX_VALUE).getText()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Trim text should return the text until the provided position") | ||
| public void trimParagraph() { | ||
| Paragraph<Void, String, String> p1 = createTextParagraph(SegmentOps.styledTextOps(), "Alpha", "MyStyle"); | ||
| // Not very consistent that MIN_VALUE is throwing an exception while other negative numbers work | ||
| assertThrows(StringIndexOutOfBoundsException.class, () -> p1.trim(Integer.MIN_VALUE).getText()); | ||
| assertEquals("", p1.trim(Integer.MIN_VALUE).getText()); | ||
| assertEquals("", p1.trim(-10).getText()); | ||
| assertEquals("", p1.trim(-1).getText()); | ||
| assertEquals("Alpha", p1.trim(Integer.MAX_VALUE).getText()); | ||
|
|
@@ -186,7 +198,7 @@ public void deletePartOfParagraph() { | |
| assertThrows(IndexOutOfBoundsException.class, () -> paragraph.delete(4, 10).getText()); // Not consistent with -1 | ||
| assertEquals("gated", paragraph.delete(0, 4).getText()); | ||
| assertEquals("gated", paragraph.delete(-1, 4).getText()); | ||
| assertThrows(StringIndexOutOfBoundsException.class, () -> paragraph.delete(Integer.MIN_VALUE, 4).getText()); // Not very consistent with -1 | ||
| assertEquals("gated", paragraph.delete(Integer.MIN_VALUE, 4).getText()); | ||
|
|
||
| // Check style too | ||
| Paragraph<Void, String, String> p2 = paragraph.delete(2, 5); | ||
|
|
@@ -232,6 +244,10 @@ public void multipleStyle() { | |
| Paragraph<Void, String, String> p1 = createTextParagraph("To be or not to be", "text"); | ||
| checkStyle(p1, 18, new String[] {"text"}, 0, 18); | ||
|
|
||
| // Restyle with same style | ||
| Paragraph<Void, String, String> p1b = p1.restyle(0, 18, "text"); | ||
| checkStyle(p1b, 18, new String[] {"text"}, 0, 18); | ||
|
|
||
| // P1 is immutable, its style hasn't changed, but P2 has now three styles | ||
| Paragraph<Void, String, String> p2 = p1.restyle(9, 12, "keyword"); | ||
| checkStyle(p1, 18, new String[] {"text"}, 0, 18); | ||
|
|
@@ -241,8 +257,18 @@ public void multipleStyle() { | |
| Paragraph<Void, String, String> p3 = p2.restyle(3, 10, "unknown"); | ||
| checkStyle(p3, 18, new String[] {"text", "unknown", "keyword", "text"}, 0, 3, 3, 10, 10, 12, 12, 18); | ||
|
|
||
| // Restyle up to the end | ||
| checkStyle(p3.restyle(11, 17, "out"), 18, | ||
| new String[] {"text", "unknown", "keyword", "out", "text"}, | ||
| 0, 3, 3, 10, 10, 11, 11, 17, 17, 18); | ||
|
|
||
| // Restyle up to the end | ||
| // Bug | ||
| checkStyle(p3.restyle(11, 18, "out"), 18, | ||
|
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. I thought the bug was with out-of-bound indexes, but it is actually a bug when you go up to the length. I created #1310 to handle that one. |
||
| new String[] {"text", "unknown", "keyword", "out"}, | ||
| 0, 3, 3, 10, 0, 1, 0, 7); | ||
|
|
||
| // Restyle out of bound | ||
| // Bug: the styles are totally off | ||
| checkStyle(p3.restyle(11, 19, "out"), 19, | ||
| new String[] {"text", "unknown", "keyword", "out"}, | ||
| 0, 3, 3, 10, 0, 1, 0, 8); | ||
|
|
||
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.
This is a behaviour change (you can see the adaptation in the test for it), any trim with negative number should equal to trimming with 0 (it was already the case for some negative values, but not all).