Skip to content

Commit 6398fd8

Browse files
committed
test(codegen): add testes for the enum generator and refactored AbstractCompilationTest
1 parent a5fca5e commit 6398fd8

File tree

8 files changed

+118
-65
lines changed

8 files changed

+118
-65
lines changed

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/_test/AbstractCompilationTest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class AbstractCompilationTest(
2222
* Will compile the given [main] and the given [dependencies]. This function will return the runtime reflection
2323
* reference to the [main] class. The [dependencies] will just be added to the compilation.
2424
*/
25-
private fun internalCompile(main: FileSpec, vararg dependencies: FileSpec): KClass<*> {
25+
private fun internalCompile(main: FileSpec, vararg dependencies: FileSpec): Result {
2626
// Generate source files for the given file specs.
2727
val mainSource = buildSourceFile(main, 0)
2828
val dependencySources = dependencies.mapIndexed() { index, dep -> buildSourceFile(dep, index + 1) }
@@ -39,7 +39,7 @@ abstract class AbstractCompilationTest(
3939
throw IllegalStateException("Kotlin compilation not successful!")
4040

4141
// Load the given main class using the ClassLoader.
42-
return result.classLoader.loadClass(main.packageName + "." + main.name).kotlin
42+
return Result(result.classLoader.loadClass(main.packageName + "." + main.name).kotlin, result.classLoader)
4343
}
4444

4545
/**
@@ -80,4 +80,9 @@ abstract class AbstractCompilationTest(
8080
.build()
8181
}).build()
8282
}
83+
84+
data class Result(
85+
val main: KClass<*>,
86+
val classLoader: ClassLoader
87+
)
8388
}

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/_test/AbstractMockCompilationTest.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/codeblock/ArgumentCodeBlockGeneratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class ArgumentCodeBlockGeneratorTest : AbstractCompilationTest() {
2424
val resolver = generator.buildResolver("argument", Scalars.GraphQLString, null)
2525

2626
// Wrap the resolver and compile the code.
27-
val compiled = compile(getWrapperClass(resolver))
27+
val compiled = compile(getWrapperClass(resolver)).main
2828

2929
// There has to be exactly one function.
3030
Assertions.assertEquals(1, compiled.declaredFunctions.size)

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/EnumGeneratorTest.kt

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@ package com.auritylab.graphql.kotlin.toolkit.codegen.generator
22

33
import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
44
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
5+
import com.squareup.kotlinpoet.FileSpec
6+
import com.squareup.kotlinpoet.TypeSpec
7+
import graphql.Scalars
8+
import graphql.schema.GraphQLArgument
9+
import graphql.schema.GraphQLDirective
510
import graphql.schema.GraphQLEnumType
11+
import java.lang.reflect.InvocationTargetException
12+
import kotlin.reflect.full.companionObject
13+
import kotlin.reflect.full.companionObjectInstance
14+
import kotlin.reflect.full.declaredMemberFunctions
15+
import kotlin.reflect.full.declaredMemberProperties
616
import kotlin.reflect.full.isSubclassOf
717
import org.junit.jupiter.api.Assertions
818
import org.junit.jupiter.api.Test
919

1020
internal class EnumGeneratorTest : AbstractCompilationTest() {
1121
@Test
1222
@Suppress("UNCHECKED_CAST")
13-
fun shouldGenerateCompilableCode() {
23+
fun `should generate compilable code`() {
1424
val generator =
1525
EnumGenerator(testEnum, TestObject.options, TestObject.kotlinTypeMapper, TestObject.generatedMapper)
1626

1727
// Compile the generator output.
18-
val generatedClass = compile(generator)
28+
val generatedClass = compile(generator).main
1929

2030
// Assert that the generated class is of type enum.
2131
Assertions.assertTrue(generatedClass.isSubclassOf(Enum::class))
@@ -30,6 +40,71 @@ internal class EnumGeneratorTest : AbstractCompilationTest() {
3040
Assertions.assertEquals(definition.name, enumConstants[i].name)
3141
}
3242
}
43+
44+
@Test
45+
fun `should build enum with representation correctly`() {
46+
val generator = EnumGenerator(
47+
testEnumWithRepresentation,
48+
TestObject.options,
49+
TestObject.kotlinTypeMapper,
50+
TestObject.generatedMapper
51+
)
52+
53+
// Compile the generator output and add the TestEnum file as dependency.
54+
val compileResult = compile(generator.generate(), testEnumFileSpec)
55+
val generatedClass = compileResult.main
56+
val testEnumConstants = compileResult.classLoader.loadClass("graphql.TestEnum").enumConstants
57+
58+
// Assert that the generated class is of type enum.
59+
Assertions.assertTrue(generatedClass.isSubclassOf(Enum::class))
60+
61+
// ... Not necessary to assert against the constants -> see test above.
62+
val constants = generatedClass.java.enumConstants
63+
64+
// Load the "representation" property reference.
65+
val representationProperty = generatedClass.declaredMemberProperties
66+
.firstOrNull { it.name == "representation" }
67+
68+
// The property has to be present.
69+
Assertions.assertNotNull(representationProperty)
70+
representationProperty!!
71+
72+
// The second and the first constant can be mapped correctly.
73+
Assertions.assertNotNull(representationProperty.call(constants[0]))
74+
Assertions.assertNotNull(representationProperty.call(constants[1]))
75+
76+
// The 'invalid' enum value has to throw an exception.
77+
Assertions.assertThrows(NoSuchElementException::class.java) {
78+
try {
79+
representationProperty.call(constants[2])
80+
} catch (ex: InvocationTargetException) {
81+
throw ex.targetException
82+
}
83+
}
84+
85+
// Load the "of" function reference.
86+
val ofFunction = generatedClass.companionObject?.declaredMemberFunctions
87+
?.firstOrNull { it.name == "of" }
88+
89+
// The function has to be present.
90+
Assertions.assertNotNull(ofFunction)
91+
ofFunction!!
92+
93+
val companionInstance = generatedClass.companionObjectInstance!!
94+
95+
// The first and the second constant can be mapped correctly.
96+
Assertions.assertNotNull(ofFunction.call(companionInstance, testEnumConstants[0]))
97+
Assertions.assertNotNull(ofFunction.call(companionInstance, testEnumConstants[1]))
98+
99+
// The 'internalInvalid' enum value has to throw an exception.
100+
Assertions.assertThrows(NoSuchElementException::class.java) {
101+
try {
102+
representationProperty.call(constants[2])
103+
} catch (ex: InvocationTargetException) {
104+
throw ex.targetException
105+
}
106+
}
107+
}
33108
}
34109

35110
private val testEnum = GraphQLEnumType.newEnum()
@@ -39,3 +114,30 @@ private val testEnum = GraphQLEnumType.newEnum()
39114
.value("third")
40115
.value("fourth")
41116
.build()
117+
118+
private val testEnumWithRepresentation = GraphQLEnumType.newEnum()
119+
.name("ETestEnum")
120+
.value("first")
121+
.value("second")
122+
.value("invalid")
123+
.withDirective(
124+
GraphQLDirective.newDirective()
125+
.name("kRepresentation")
126+
.argument(
127+
GraphQLArgument.newArgument()
128+
.type(Scalars.GraphQLString)
129+
.name("class")
130+
.value("graphql.TestEnum")
131+
)
132+
)
133+
.build()
134+
135+
private val testEnumFileSpec = FileSpec.builder("graphql", "TestEnum")
136+
.addType(
137+
TypeSpec.enumBuilder("TestEnum")
138+
.addEnumConstant("first")
139+
.addEnumConstant("second")
140+
.addEnumConstant("internalInvalid")
141+
.build()
142+
)
143+
.build()

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/InputObjectGeneratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class InputObjectGeneratorTest : AbstractCompilationTest() {
3131
@BeforeAll
3232
fun compileCode() {
3333
// Compile the code of the generator
34-
generatedClass = compile(generator)
34+
generatedClass = compile(generator).main
3535
}
3636

3737
@Test

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/ObjectTypeGeneratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal class ObjectTypeGeneratorTest : AbstractCompilationTest() {
2626
@BeforeAll
2727
fun compileCode() {
2828
// Compile the code of the generator
29-
generatedClass = compile(generator)
29+
generatedClass = compile(generator).main
3030
}
3131

3232
@Test

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/ValueWrapperGeneratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class ValueWrapperGeneratorTest : AbstractCompilationTest() {
1515
ValueWrapperGenerator(TestObject.options, TestObject.kotlinTypeMapper, TestObject.generatedMapper)
1616

1717
// Compile the generated code.
18-
val generatedClass = compile(generator)
18+
val generatedClass = compile(generator).main
1919

2020
// Assert against the member properties.
2121
val memberProperties = generatedClass.memberProperties

graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/generator/fieldResolver/FieldResolverGeneratorTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class FieldResolverGeneratorTest : AbstractCompilationTest(true) {
4545
@BeforeAll
4646
fun compileCode() {
4747
// Compile the code of the generator
48-
generatedClass = compile(generator)
48+
generatedClass = compile(generator).main
4949
}
5050

5151
@Test
@@ -142,7 +142,7 @@ internal class FieldResolverGeneratorTest : AbstractCompilationTest(true) {
142142
@BeforeAll
143143
fun compileCode() {
144144
// Compile the code of the generator
145-
generatedClass = compile(generator)
145+
generatedClass = compile(generator).main
146146
}
147147

148148
@Test
@@ -194,7 +194,7 @@ internal class FieldResolverGeneratorTest : AbstractCompilationTest(true) {
194194
@BeforeAll
195195
fun compileCode() {
196196
// Compile the code of the generator
197-
generatedClass = compile(generator)
197+
generatedClass = compile(generator).main
198198
}
199199

200200
@Test

0 commit comments

Comments
 (0)