Spock Web Console

subscribe to the feed Subscribe
Types (via #spockwebconsole)
tweet this script Tweet


Published 1 year ago by Eric
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
package intro

import spock.lang.*

Programming Languages have a type system - a way to represent different types of data in the computer.

This example we will look at common types of data that computer programs store. 

Your task is to make this test pass by replacing any `null` with the correct values.

Run the test first to see how it is failing and give you hints on what the answers are!
class TypeSpec extends Specification {
    def "What `type` are these things?"() {
        expect:"Try to guess what types "
        whatTypeIs(2)   == null
        whatTypeIs("2") == null

        and: "Number precision"
        whatTypeIs(2.1)  == null
        whatTypeIs(2.1F) == null
        whatTypeIs(2.1D) == null

        and: "Strings"
        whatTypeIs("Some Text") == null
        whatTypeIs("")          == null
        and: "Booleans"
        whatTypeIs(false)   == null
        whatTypeIs(true)    == null

        and: "collections"
        whatTypeIs([1, 2, 3, 3])      == null
        whatTypeIs([key: "value"])    == null
        whatTypeIs([1, 2, 3].toSet()) == null

    // This is a helper to find the objects type
    // You can ignore this for now.
    static whatTypeIs(thing) {