-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement extension function to remove orphan words (#566)
* Implement QEText that will remove orphan words * Add tests for String.removeOrphans
- Loading branch information
1 parent
c8ac4dc
commit 873de57
Showing
5 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
Binary file added
BIN
+13.9 KB
....gravatar.quickeditor.ui.components.QESectionMessageTest.qeTextTestNoOrphan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions
8
gravatar-quickeditor/src/main/java/com/gravatar/quickeditor/ui/StringExtensions.kt
This file contains 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,8 @@ | ||
package com.gravatar.quickeditor.ui | ||
|
||
internal val String.removeOrphans: String | ||
get() { | ||
val space = " " | ||
val index = lastIndexOf(space, ignoreCase = true) | ||
return if (index < 0) this else this.replaceRange(index, index + space.length, "\u00A0") | ||
} |
This file contains 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
43 changes: 43 additions & 0 deletions
43
gravatar-quickeditor/src/test/java/com/gravatar/quickeditor/ui/StringExtensionsTest.kt
This file contains 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,43 @@ | ||
package com.gravatar.quickeditor.ui | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
internal class StringExtensionsTest { | ||
@Test | ||
fun `give a string with spaces when orphans removed then last space replaced with non-breaking`() { | ||
// Given | ||
val input = "This is a test string with spaces" | ||
val expected = "This is a test string with\u00A0spaces" | ||
|
||
// When | ||
val actual = input.removeOrphans | ||
|
||
// Then | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun `give a string without spaces when orphans removed then string remains unchanged`() { | ||
// Given | ||
val input = "ThisIsATestStringWithoutSpaces" | ||
|
||
// When | ||
val actual = input.removeOrphans | ||
|
||
// Then | ||
assertEquals(input, actual) | ||
} | ||
|
||
@Test | ||
fun `given an empty string when orphans removed then string remains unchanged`() { | ||
// Given | ||
val input = "" | ||
|
||
// When | ||
val actual = input.removeOrphans | ||
|
||
// Then | ||
assertEquals(input, actual) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...-quickeditor/src/test/java/com/gravatar/quickeditor/ui/components/QESectionMessageTest.kt
This file contains 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,20 @@ | ||
package com.gravatar.quickeditor.ui.components | ||
|
||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import com.gravatar.quickeditor.ui.gravatarScreenshotTest | ||
import com.gravatar.uitestutils.RoborazziTest | ||
import org.junit.Test | ||
|
||
class QESectionMessageTest : RoborazziTest() { | ||
@Test | ||
fun qeTextTestNoOrphan() = gravatarScreenshotTest { | ||
Surface( | ||
modifier = Modifier.width(220.dp), | ||
) { | ||
QESectionMessage(message = "This is a long text that should not break just before the last word.") | ||
} | ||
} | ||
} |