|
| 1 | +import com.baeldung.strings.Concatenate; |
| 2 | + |
| 3 | +class ConcatenateTest extends GroovyTestCase { |
| 4 | + |
| 5 | + void testSimpleConcat() { |
| 6 | + def name = new Concatenate() |
| 7 | + name.first = 'Joe'; |
| 8 | + name.last = 'Smith'; |
| 9 | + def expected = 'My name is Joe Smith' |
| 10 | + assertToString(name.doSimpleConcat(), expected) |
| 11 | + } |
| 12 | + |
| 13 | + void testConcatUsingGString() { |
| 14 | + def name = new Concatenate() |
| 15 | + name.first = "Joe"; |
| 16 | + name.last = "Smith"; |
| 17 | + def expected = "My name is Joe Smith" |
| 18 | + assertToString(name.doConcatUsingGString(), expected) |
| 19 | + } |
| 20 | + |
| 21 | + void testConcatUsingGStringClosures() { |
| 22 | + def name = new Concatenate() |
| 23 | + name.first = "Joe"; |
| 24 | + name.last = "Smith"; |
| 25 | + def expected = "My name is Joe Smith" |
| 26 | + assertToString(name.doConcatUsingGStringClosures(), expected) |
| 27 | + } |
| 28 | + |
| 29 | + void testConcatUsingStringConcatMethod() { |
| 30 | + def name = new Concatenate() |
| 31 | + name.first = "Joe"; |
| 32 | + name.last = "Smith"; |
| 33 | + def expected = "My name is Joe Smith" |
| 34 | + assertToString(name.doConcatUsingStringConcatMethod(), expected) |
| 35 | + } |
| 36 | + |
| 37 | + void testConcatUsingLeftShiftOperator() { |
| 38 | + def name = new Concatenate() |
| 39 | + name.first = "Joe"; |
| 40 | + name.last = "Smith"; |
| 41 | + def expected = "My name is Joe Smith" |
| 42 | + assertToString(name.doConcatUsingLeftShiftOperator(), expected) |
| 43 | + } |
| 44 | + |
| 45 | + void testConcatUsingArrayJoinMethod() { |
| 46 | + def name = new Concatenate() |
| 47 | + name.first = "Joe"; |
| 48 | + name.last = "Smith"; |
| 49 | + def expected = "My name is Joe Smith" |
| 50 | + assertToString(name.doConcatUsingArrayJoinMethod(), expected) |
| 51 | + } |
| 52 | + |
| 53 | + void testConcatUsingArrayInjectMethod() { |
| 54 | + def name = new Concatenate() |
| 55 | + name.first = "Joe"; |
| 56 | + name.last = "Smith"; |
| 57 | + def expected = "My name is Joe Smith" |
| 58 | + assertToString(name.doConcatUsingArrayInjectMethod(), expected) |
| 59 | + } |
| 60 | + |
| 61 | + void testConcatUsingStringBuilder() { |
| 62 | + def name = new Concatenate() |
| 63 | + name.first = "Joe"; |
| 64 | + name.last = "Smith"; |
| 65 | + def expected = "My name is Joe Smith" |
| 66 | + assertToString(name.doConcatUsingStringBuilder(), expected) |
| 67 | + } |
| 68 | + |
| 69 | + void testConcatUsingStringBuffer() { |
| 70 | + def name = new Concatenate() |
| 71 | + name.first = "Joe"; |
| 72 | + name.last = "Smith"; |
| 73 | + def expected = "My name is Joe Smith" |
| 74 | + assertToString(name.doConcatUsingStringBuffer(), expected) |
| 75 | + } |
| 76 | + |
| 77 | + void testConcatMultilineUsingStringConcatMethod() { |
| 78 | + def name = new Concatenate() |
| 79 | + name.first = '''Joe |
| 80 | + Smith |
| 81 | + '''; |
| 82 | + name.last = 'Junior'; |
| 83 | + def expected = '''My name is Joe |
| 84 | + Smith |
| 85 | + Junior'''; |
| 86 | + assertToString(name.doConcatUsingStringConcatMethod(), expected) |
| 87 | + } |
| 88 | + |
| 89 | + void testGStringvsClosure(){ |
| 90 | + def first = "Joe"; |
| 91 | + def last = "Smith"; |
| 92 | + def eagerGString = "My name is $first $last" |
| 93 | + def lazyGString = "My name is ${-> first} ${-> last}" |
| 94 | + |
| 95 | + assert eagerGString == "My name is Joe Smith" |
| 96 | + assert lazyGString == "My name is Joe Smith" |
| 97 | + first = "David"; |
| 98 | + assert eagerGString == "My name is Joe Smith" |
| 99 | + assert lazyGString == "My name is David Smith" |
| 100 | + } |
| 101 | +} |
0 commit comments