Bael 2666 concatenate strings with groovy (#7307)
* BAEL-2666 code for concatenate strings with groovy * BAEL-2666 add placeholder link to readme * BAEL-2666 remove article link in README, move code to core-groovy-2
This commit is contained in:
parent
19df0fbb74
commit
38000941b5
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.concatenate
|
||||||
|
|
||||||
|
class Wonder {
|
||||||
|
|
||||||
|
String numOfWonder = 'seven'
|
||||||
|
|
||||||
|
String operator_plus() {
|
||||||
|
return 'The ' + numOfWonder + ' wonders of the world'
|
||||||
|
}
|
||||||
|
|
||||||
|
String operator_left() {
|
||||||
|
return 'The ' << numOfWonder << ' wonders of ' << 'the world'
|
||||||
|
}
|
||||||
|
|
||||||
|
String interpolation_one() {
|
||||||
|
return "The $numOfWonder wonders of the world"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
String interpolation_two() {
|
||||||
|
return "The ${numOfWonder} wonders of the world"
|
||||||
|
}
|
||||||
|
|
||||||
|
String multilineString() {
|
||||||
|
return """
|
||||||
|
There are $numOfWonder wonders of the world.
|
||||||
|
Can you name them all?
|
||||||
|
1. The Great Pyramid of Giza
|
||||||
|
2. Hanging Gardens of Babylon
|
||||||
|
3. Colossus of Rhode
|
||||||
|
4. Lighthouse of Alexendra
|
||||||
|
5. Temple of Artemis
|
||||||
|
6. Status of Zeus at Olympia
|
||||||
|
7. Mausoleum at Halicarnassus
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|
||||||
|
String method_concat() {
|
||||||
|
return 'The '.concat(numOfWonder).concat(' wonders of the world')
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
String method_builder() {
|
||||||
|
return new StringBuilder()
|
||||||
|
.append('The ').append(numOfWonder).append(' wonders of the world')
|
||||||
|
}
|
||||||
|
|
||||||
|
String method_buffer() {
|
||||||
|
return new StringBuffer()
|
||||||
|
.append('The ').append(numOfWonder).append(' wonders of the world')
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.baeldung.concatenate
|
||||||
|
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
class WonderUnitTest {
|
||||||
|
|
||||||
|
static final String EXPECTED_SINGLE_LINE = "The seven wonders of the world"
|
||||||
|
|
||||||
|
Wonder wonder
|
||||||
|
|
||||||
|
@Before
|
||||||
|
void before() {
|
||||||
|
wonder = new Wonder()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingOperatorPlus_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_plus())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingOperatorLeft_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_left())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingInterpolationOne_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_one())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingInterpolationTwo_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_two())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingMultiline_thenConcatCorrectly() {
|
||||||
|
def expectedMultiline = """
|
||||||
|
There are seven wonders of the world.
|
||||||
|
Can you name them all?
|
||||||
|
1. The Great Pyramid of Giza
|
||||||
|
2. Hanging Gardens of Babylon
|
||||||
|
3. Colossus of Rhode
|
||||||
|
4. Lighthouse of Alexendra
|
||||||
|
5. Temple of Artemis
|
||||||
|
6. Status of Zeus at Olympia
|
||||||
|
7. Mausoleum at Halicarnassus
|
||||||
|
"""
|
||||||
|
assertEquals(expectedMultiline, wonder.multilineString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingMethodConcat_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_concat())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingMethodBuilder_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_builder())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingMethodBuffer_thenConcatCorrectly() {
|
||||||
|
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_buffer())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue