fun testCodeGen()

in graphql-dgs-codegen-core/src/integTest/kotlin/com/netflix/graphql/dgs/codegen/Kotlin2CodeGenTest.kt [41:114]


    fun testCodeGen(testName: String) {
        val schema = readResource("/$testName/schema.graphql")

        val codeGenResult =
            CodeGen(
                CodeGenConfig(
                    schemas = setOf(schema),
                    packageName = "com.netflix.graphql.dgs.codegen.cases.$testName.expected",
                    language = Language.KOTLIN,
                    generateClientApi = true,
                    generateKotlinNullableClasses = true,
                    generateKotlinClosureProjections = true,
                    typeMapping =
                        when (testName) {
                            "dataClassWithMappedTypes" ->
                                mapOf(
                                    "Long" to "kotlin.Long",
                                    "DateTime" to "java.time.OffsetDateTime",
                                    "PageInfo" to "graphql.relay.PageInfo",
                                    "EntityConnection" to
                                        "graphql.relay.SimpleListConnection<com.netflix.graphql.dgs.codegen.cases.dataClassWithMappedTypes.expected.types.EntityEdge>",
                                )
                            "dataClassWithMappedInterfaces" ->
                                mapOf(
                                    "Node" to "com.netflix.graphql.dgs.codegen.fixtures.Node",
                                )
                            "inputWithDefaultBigDecimal" ->
                                mapOf(
                                    "Decimal" to "java.math.BigDecimal",
                                )
                            "inputWithDefaultCurrency" ->
                                mapOf(
                                    "Currency" to "java.util.Currency",
                                )
                            else -> emptyMap()
                        },
                ),
            ).generate()

        val fileNames =
            codeGenResult
                .kotlinSources()
                .groupingBy { it.packageName.substringAfterLast('.') to it.name }
                .eachCount()

        // fail if any file was defined twice
        fileNames
            .filterValues { it > 1 }
            .keys
            .forEach { fail("Duplicate file: ${it.first}.${it.second}") }

        // fail if any file was expected that's not generated
        listAllFiles("/$testName/expected")
            .map {
                it.getName(it.nameCount - 2).toString() to it.getName(it.nameCount - 1).toString().removeSuffix(".kt")
            }.toSet()
            .subtract(fileNames.keys)
            .forEach { fail("Missing expected file: ${it.first}.${it.second}") }

        codeGenResult.kotlinSources().forEach { spec ->

            val type = spec.packageName.substringAfterLast("expected").trimStart('.')
            val fileName = "/$testName/expected/$type/${spec.name}.kt"
            val actual = spec.toString()

            if (updateExpected) {
                writeExpected(fileName, actual)
            } else {
                assertThat(actual).isEqualTo(readResource(fileName))
            }
        }

        assertCompilesKotlin(codeGenResult)
    }