/*
 * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.objcexport.tests

import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.objcexport.analysisApiUtils.isObjCObjectType
import org.jetbrains.kotlin.export.test.InlineSourceCodeAnalysis
import org.jetbrains.kotlin.export.test.getClassOrFail
import org.junit.jupiter.api.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class IsObjCObjectTypeTest(
    private val inlineSourceCodeAnalysis: InlineSourceCodeAnalysis,
) {
    @Test
    fun `test - simple class`() {
        val file = inlineSourceCodeAnalysis.createKtFile("class Foo")
        analyze(file) {
            val fooSymbol = getClassOrFail(file, "Foo")
            assertFalse(isObjCObjectType(fooSymbol.memberScope.constructors.single().returnType))
        }
    }

    @Test
    fun `test - class implementing ObjCObject`() {
        val file = inlineSourceCodeAnalysis.createKtFile(
            """
                class Foo: kotlinx.cinterop.ObjCObject()
            """.trimIndent()
        )

        analyze(file) {
            val fooSymbol = getClassOrFail(file, "Foo")
            assertTrue(isObjCObjectType(fooSymbol.memberScope.constructors.single().returnType))
        }
    }

    @Test
    fun `test - class implementing ObjCObject transitively`() {
        val file = inlineSourceCodeAnalysis.createKtFile(
            """
                abstract class A: kotlinx.cinterop.ObjCObject()
                abstract class B: A()
                class Foo: B()
            """.trimIndent()
        )

        analyze(file) {
            val fooSymbol = getClassOrFail(file, "Foo")
            assertTrue(isObjCObjectType(fooSymbol.memberScope.constructors.single().returnType))
        }
    }
}
