All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.reflect.TypeOfSpec.groovy Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.reflect

import spock.lang.Specification

import java.lang.reflect.Type

import static org.gradle.api.reflect.TypeOf.parameterizedTypeOf
import static org.gradle.api.reflect.TypeOf.typeOf

class TypeOfSpec extends Specification {

    static class InnerClass {}

    def "to string"() {
        expect:
        new TypeOf() {}.toString() == 'org.gradle.api.reflect.TypeOfSpec.InnerClass'
        new TypeOf>() {}.toString() == 'java.util.List'
        new TypeOf() {}.toString() == 'java.lang.String'
        new TypeOf>() {}.toString() == 'java.util.List'
        new TypeOf() {}.toString() == 'java.lang.Object'
    }

    def "simple generic name"() {
        expect:
        new TypeOf() {}.simpleName == 'String'
        new TypeOf>() {}.simpleName == 'List'
        new TypeOf() {}.simpleName == 'Object'
    }

    def "equality"() {
        given:
        def a = new TypeOf() {}
        def b = new TypeOf() {}
        def c = new TypeOf>() {}

        expect:
        a != b
        b != c
        c != a

        and:
        a == new TypeOf() {}
        b == new TypeOf() {}
        c == new TypeOf>() {}
    }

    def "hashCode semantics"() {
        given:
        def a = new TypeOf() {}.hashCode()
        def b = new TypeOf() {}.hashCode()
        def c = new TypeOf>() {}.hashCode()

        expect:
        a != b
        b != c
        c != a

        and:
        a == new TypeOf() {}.hashCode()
        b == new TypeOf() {}.hashCode()
        c == new TypeOf>() {}.hashCode()
    }

    def "factory methods"() {
        expect:
        typeOf(String) == new TypeOf() {}
        typeOf((Type) String) == new TypeOf() {}
        parameterizedTypeOf(new TypeOf>() {}, typeOf(String)) == new TypeOf>() {}
        parameterizedTypeOf(new TypeOf>() {}, typeOf(String), typeOf(Integer)) == new TypeOf>() {}
    }

    def "componentType is null on non-array types"() {
        expect:
        typeOf(String).componentType == null
        new TypeOf>() {}.componentType == null
        new TypeOf>() {}.actualTypeArguments[0].with { wildcard && componentType == null }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy