How to Remove a Prefix From Strings in Groovy: refactor to use spock testing framework
This commit is contained in:
parent
f6fa7f8909
commit
41d8fffcc7
@ -1,70 +1,74 @@
|
|||||||
package com.baeldung.removeprefix
|
package com.baeldung.removeprefix
|
||||||
|
|
||||||
import org.junit.Assert
|
import spock.lang.Specification
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class RemovePrefixTest {
|
class RemovePrefixTest extends Specification {
|
||||||
|
|
||||||
|
def "whenCasePrefixIsRemoved_thenReturnTrue"() {
|
||||||
@Test
|
given:
|
||||||
public void whenCasePrefixIsRemoved_thenReturnTrue() {
|
|
||||||
def trimPrefix = {
|
def trimPrefix = {
|
||||||
it.startsWith('Groovy-') ? it.minus('Groovy-') : it
|
it.startsWith('Groovy-') ? it.minus('Groovy-') : it
|
||||||
}
|
}
|
||||||
|
|
||||||
|
when:
|
||||||
def actual = trimPrefix("Groovy-Tutorials at Baeldung")
|
def actual = trimPrefix("Groovy-Tutorials at Baeldung")
|
||||||
def expected = "Tutorials at Baeldung"
|
def expected = "Tutorials at Baeldung"
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual)
|
then:
|
||||||
|
expected == actual
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenPrefixIsRemoved_thenReturnTrue"() {
|
||||||
public void whenPrefixIsRemoved_thenReturnTrue() {
|
given:
|
||||||
|
|
||||||
String prefix = "groovy-"
|
String prefix = "groovy-"
|
||||||
String trimPrefix = "Groovy-Tutorials at Baeldung"
|
String trimPrefix = "Groovy-Tutorials at Baeldung"
|
||||||
def actual;
|
|
||||||
if(trimPrefix.startsWithIgnoreCase(prefix)) {
|
when:
|
||||||
|
def actual
|
||||||
|
if (trimPrefix.startsWithIgnoreCase(prefix)) {
|
||||||
actual = trimPrefix.substring(prefix.length())
|
actual = trimPrefix.substring(prefix.length())
|
||||||
}
|
}
|
||||||
|
|
||||||
def expected = "Tutorials at Baeldung"
|
def expected = "Tutorials at Baeldung"
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual)
|
then:
|
||||||
|
expected == actual
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenPrefixIsRemovedUsingRegex_thenReturnTrue"() {
|
||||||
public void whenPrefixIsRemovedUsingRegex_thenReturnTrue() {
|
given:
|
||||||
|
|
||||||
def regex = ~"^([Gg])roovy-"
|
def regex = ~"^([Gg])roovy-"
|
||||||
String trimPrefix = "Groovy-Tutorials at Baeldung"
|
String trimPrefix = "Groovy-Tutorials at Baeldung"
|
||||||
String actual = trimPrefix - regex
|
|
||||||
|
|
||||||
|
when:
|
||||||
|
String actual = trimPrefix - regex
|
||||||
def expected = "Tutorials at Baeldung"
|
def expected = "Tutorials at Baeldung"
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual)
|
then:
|
||||||
|
expected == actual
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue"() {
|
||||||
public void whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() {
|
given:
|
||||||
def regex = ~"^groovy"
|
def regex = ~"^groovy"
|
||||||
String trimPrefix = "groovyTutorials at Baeldung's groovy page"
|
String trimPrefix = "groovyTutorials at Baeldung's groovy page"
|
||||||
String actual = trimPrefix.replaceFirst(regex, "")
|
|
||||||
|
|
||||||
|
when:
|
||||||
|
String actual = trimPrefix.replaceFirst(regex, "")
|
||||||
def expected = "Tutorials at Baeldung's groovy page"
|
def expected = "Tutorials at Baeldung's groovy page"
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual)
|
then:
|
||||||
|
expected == actual
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenPrefixIsRemovedUsingReplaceAll_thenReturnTrue"() {
|
||||||
public void whenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() {
|
given:
|
||||||
|
|
||||||
String trimPrefix = "groovyTutorials at Baeldung groovy"
|
String trimPrefix = "groovyTutorials at Baeldung groovy"
|
||||||
String actual = trimPrefix.replaceAll(/^groovy/, "")
|
|
||||||
|
|
||||||
|
when:
|
||||||
|
String actual = trimPrefix.replaceAll(/^groovy/, "")
|
||||||
def expected = "Tutorials at Baeldung groovy"
|
def expected = "Tutorials at Baeldung groovy"
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual)
|
then:
|
||||||
|
expected == actual
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user