Spock Web Console

subscribe to the feed Subscribe
sisg - 02 Spock Basics: Groovy Strings (via #spockwebconsole)
tweet this script Tweet

sisg - 02 Spock Basics: Groovy Strings

Published 7 months ago by Matthias Schulz with tags sisg Spock Basics Groovy String
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
package basics
import spock.lang.Specification

class GroovyStringsSpec extends Specification
{
    def "test 1: String - a simple Java like string"()
    {
        given:
        String name = "Tom"
        int age = 30

        when:
        String string = 'My name is ' + name + ', I am ' + age + ' years old.'
        
        then:
        string == 'My name is Tom, I am 30 years old.'
    }
    
    def "test 2: GString with Object bindings"()
    {
        given:
        String name = "Tom"
        int age = 30

        when:
        GString gstring = "My name is $name, I am $age years old."
        
        then:
        gstring == 'My name is Tom, I am 30 years old.'
    }    
    
    def "test 3: Multiline GString with Object bindings"()
    {
        given:
        String name = "Tom"
        int age = 30

        when:
        GString gstringMultiLine = """Hello!
My name is $name and I am $age years old.
Best regards,
$name
"""
        
        then:
        gstringMultiLine == """Hello!
My name is $name and I am $age years old.
Best regards,
$name
"""
    }    
        
}