Skip to content

Commit eba1741

Browse files
smyrickgscheibel
authored andcommitted
feat: add jacoco reports with tests (#61)
1 parent adc5b4b commit eba1741

File tree

8 files changed

+119
-25
lines changed

8 files changed

+119
-25
lines changed

pom.xml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
<properties>
7373
<reflections.version>0.9.11</reflections.version>
74-
<kotlin.version>1.2.70</kotlin.version>
74+
<kotlin.version>1.2.71</kotlin.version>
7575
<kotlin-ktlint.version>0.29.0</kotlin-ktlint.version>
7676
<kotlin-detekt.version>1.0.0.RC8</kotlin-detekt.version>
7777
<junit.version>4.12</junit.version>
@@ -179,6 +179,25 @@
179179
</dependency>
180180
</dependencies>
181181
</plugin>
182+
<plugin>
183+
<groupId>org.jacoco</groupId>
184+
<artifactId>jacoco-maven-plugin</artifactId>
185+
<version>0.8.2</version>
186+
<executions>
187+
<execution>
188+
<goals>
189+
<goal>prepare-agent</goal>
190+
</goals>
191+
</execution>
192+
<execution>
193+
<id>report</id>
194+
<phase>test</phase>
195+
<goals>
196+
<goal>report</goal>
197+
</goals>
198+
</execution>
199+
</executions>
200+
</plugin>
182201
</plugins>
183202
</build>
184203

src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import com.expedia.graphql.annotations.GraphQLDescription
44
import com.expedia.graphql.annotations.GraphQLID
55
import com.expedia.graphql.annotations.GraphQLIgnore
66
import com.expedia.graphql.schema.exceptions.CouldNotGetNameOfAnnotationException
7-
import com.expedia.graphql.schema.generator.isNotBlackListed
87
import com.expedia.graphql.schema.generator.types.defaultGraphQLScalars
98
import com.google.common.base.CaseFormat
109
import graphql.schema.GraphQLArgument
1110
import graphql.schema.GraphQLDirective
1211
import graphql.schema.GraphQLInputType
1312
import kotlin.reflect.KAnnotatedElement
1413
import kotlin.reflect.KClass
15-
import kotlin.reflect.full.declaredMemberFunctions
1614
import kotlin.reflect.full.findAnnotation
1715
import com.expedia.graphql.annotations.GraphQLDirective as DirectiveAnnotation
1816

@@ -71,8 +69,6 @@ internal fun KAnnotatedElement.isGraphQLID() = this.findAnnotation<GraphQLID>()
7169
internal fun Annotation.getDirectiveInfo(): DirectiveAnnotation? =
7270
this.annotationClass.annotations.find { it is DirectiveAnnotation } as? DirectiveAnnotation
7371

74-
internal fun KClass<out Annotation>.properties() = this.declaredMemberFunctions.filter(isNotBlackListed)
75-
7672
internal fun KAnnotatedElement.directives() =
7773
this.annotations.asSequence()
7874
.mapNotNull { it.getDirectiveInfo() }
@@ -94,11 +90,11 @@ private fun DirectiveAnnotation.getGraphQLDirective(): GraphQLDirective {
9490
.validLocations(*this.locations)
9591
.description(this.description)
9692

97-
kClass.properties().forEach { prop ->
98-
val propertyName = prop.name
99-
val value = prop.call(kClass)
93+
kClass.getValidFunctions().forEach { kFunction ->
94+
val propertyName = kFunction.name
95+
val value = kFunction.call(kClass)
10096
@Suppress("Detekt.UnsafeCast")
101-
val type = defaultGraphQLScalars(prop.returnType) as GraphQLInputType
97+
val type = defaultGraphQLScalars(kFunction.returnType) as GraphQLInputType
10298
builder.argument(GraphQLArgument.newArgument().name(propertyName).value(value).type(type).build())
10399
}
104100

src/main/kotlin/com/expedia/graphql/schema/extensions/kClassExtensions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import kotlin.reflect.KClass
77
import kotlin.reflect.full.declaredMemberFunctions
88
import kotlin.reflect.full.declaredMemberProperties
99

10-
internal fun KClass<*>.getValidProperties(hooks: SchemaGeneratorHooks) = this.declaredMemberProperties
11-
.filter { hooks.isValidProperty(it) }
10+
internal fun KClass<*>.getValidProperties(hooks: SchemaGeneratorHooks? = null) = this.declaredMemberProperties
11+
.filter { hooks?.isValidProperty(it) ?: true }
1212
.filter { prop -> propertyFilters.all { it.invoke(prop) } }
1313

14-
internal fun KClass<*>.getValidFunctions(hooks: SchemaGeneratorHooks) = this.declaredMemberFunctions
15-
.filter { hooks.isValidFunction(it) }
14+
internal fun KClass<*>.getValidFunctions(hooks: SchemaGeneratorHooks? = null) = this.declaredMemberFunctions
15+
.filter { hooks?.isValidFunction(it) ?: true }
1616
.filter { func -> functionFilters.all { it.invoke(func) } }
1717

1818
internal fun KClass<*>.canBeGraphQLInterface(): Boolean = this.java.isInterface

src/main/kotlin/com/expedia/graphql/schema/generator/schemaFilters.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import kotlin.reflect.KFunction
77
import kotlin.reflect.KProperty
88
import kotlin.reflect.KVisibility
99

10-
private val blackListFunctions: List<String> = listOf("annotationType", "toString", "copy", "equals", "hashCode")
11-
private val componentFunctionRegex = Regex("component([0-9]+)")
12-
1310
private typealias CallableFilter = (KCallable<*>) -> Boolean
1411
private typealias AnnotatedElementFilter = (KAnnotatedElement) -> Boolean
15-
16-
private val isPublic: CallableFilter = { it.visibility == KVisibility.PUBLIC }
17-
private val isNotGraphQLIgnored: AnnotatedElementFilter = { it.isGraphQLIgnored().not() }
18-
1912
private typealias PropertyFilter = (KProperty<*>) -> Boolean
2013
private typealias FunctionFilter = (KFunction<*>) -> Boolean
2114

22-
internal val isNotBlackListed: FunctionFilter = { (blackListFunctions.contains(it.name) || it.name.matches(componentFunctionRegex)).not() }
15+
private val blackListFunctions: List<String> = listOf("annotationType", "toString", "copy", "equals", "hashCode")
16+
private val componentFunctionRegex = Regex("component([0-9]+)")
17+
18+
private val isPublic: CallableFilter = { it.visibility == KVisibility.PUBLIC }
19+
private val isNotGraphQLIgnored: AnnotatedElementFilter = { it.isGraphQLIgnored().not() }
20+
private val isBlacklistedFunction: FunctionFilter = { blackListFunctions.contains(it.name) }
21+
private val isComponentFunction: FunctionFilter = { it.name.matches(componentFunctionRegex) }
22+
private val isNotBlackListed: FunctionFilter = { (isBlacklistedFunction(it) || isComponentFunction(it)).not() }
2323

2424
internal val propertyFilters: List<PropertyFilter> = listOf(isPublic, isNotGraphQLIgnored)
2525
internal val functionFilters: List<FunctionFilter> = listOf(isPublic, isNotGraphQLIgnored, isNotBlackListed)

src/test/kotlin/com/expedia/graphql/schema/extensions/KClassExtensionsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class KClassExtensionsTest {
3131

3232
@Test
3333
fun `test getting valid properties with no hooks`() {
34-
val properties = MyTestClass::class.getValidProperties(NoopSchemaGeneratorHooks())
34+
val properties = MyTestClass::class.getValidProperties()
3535
assertEquals(listOf("filteredProperty", "publicProperty"), properties.map { it.name })
3636
}
3737

@@ -43,7 +43,7 @@ class KClassExtensionsTest {
4343

4444
@Test
4545
fun `test getting valid functions with no hooks`() {
46-
val properties = MyTestClass::class.getValidFunctions(NoopSchemaGeneratorHooks())
46+
val properties = MyTestClass::class.getValidFunctions()
4747
assertEquals(listOf("filteredFunction", "publicFunction"), properties.map { it.name })
4848
}
4949

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.expedia.graphql.schema.generator
2+
3+
import com.expedia.graphql.annotations.GraphQLIgnore
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.KProperty
6+
import kotlin.reflect.full.declaredMemberFunctions
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertFalse
10+
import kotlin.test.assertTrue
11+
12+
class SchenaFiltersTest {
13+
14+
private data class MyDataClass(val id: Int = 0)
15+
16+
private class MyClass {
17+
18+
val publicProperty: Int = 0
19+
20+
internal val nonPublicProperty: Int = 0
21+
22+
@GraphQLIgnore
23+
internal val ignoredProperty: Int = 0
24+
25+
fun publicFunction() = privateFunction()
26+
27+
@GraphQLIgnore
28+
fun ignoredFunction() = privateFunction()
29+
30+
internal fun privateFunction() = nonPublicProperty
31+
}
32+
33+
@Test
34+
fun `test function filters`() {
35+
assertTrue(testFunction(MyClass::publicFunction))
36+
assertFalse(testFunction(MyClass::ignoredFunction))
37+
assertFalse(testFunction(MyClass::privateFunction))
38+
}
39+
40+
@Test
41+
fun `test generated function filters`() {
42+
val functions = MyDataClass::class.declaredMemberFunctions
43+
val size = functions.filter { func -> functionFilters.all { it(func) } }.size
44+
assertEquals(expected = 0, actual = size)
45+
}
46+
47+
@Test
48+
fun `test property filters`() {
49+
assertTrue(testProperty(MyClass::publicProperty))
50+
assertFalse(testProperty(MyClass::nonPublicProperty))
51+
assertFalse(testProperty(MyClass::ignoredProperty))
52+
assertTrue(testProperty(MyDataClass::id))
53+
}
54+
55+
private fun testFunction(function: KFunction<*>): Boolean = functionFilters.all { it(function) }
56+
57+
private fun testProperty(property: KProperty<*>): Boolean = propertyFilters.all { it(property) }
58+
}

src/test/kotlin/com/expedia/graphql/schema/generator/TypesCacheTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import com.expedia.graphql.schema.models.KGraphQLType
44
import graphql.schema.GraphQLType
55
import org.junit.jupiter.api.Test
66
import kotlin.reflect.full.starProjectedType
7+
import kotlin.test.assertEquals
78
import kotlin.test.assertFalse
89
import kotlin.test.assertNotNull
910
import kotlin.test.assertNull
1011
import kotlin.test.assertTrue
1112

1213
class TypesCacheTest {
1314

14-
data class MyType(val id: Int = 0)
15+
internal data class MyType(val id: Int = 0)
1516

1617
private val graphQLType: GraphQLType = GraphQLType { "MyType" }
1718
private val secondGraphQLType: GraphQLType = GraphQLType { "MySecondType" }
@@ -26,7 +27,9 @@ class TypesCacheTest {
2627

2728
cache.put(cacheKey, cacheValue)
2829

29-
assertNotNull(cache.get(cacheKey))
30+
val cacheHit = cache.get(cacheKey)
31+
assertNotNull(cacheHit)
32+
assertEquals(expected = cacheValue.graphQLType, actual = cacheHit)
3033
}
3134

3235
@Test
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.expedia.graphql.schema.models
2+
3+
import graphql.schema.GraphQLType
4+
import org.junit.jupiter.api.Test
5+
import kotlin.test.assertEquals
6+
7+
internal class KGraphQLTypeTest {
8+
9+
private data class MyType(val id: Int = 0)
10+
private val graphQLType = GraphQLType { "MyType" }
11+
12+
@Test
13+
fun `properties are set`() {
14+
val kGraphQLType = KGraphQLType(MyType::class, graphQLType)
15+
assertEquals(expected = MyType::class, actual = kGraphQLType.kClass)
16+
assertEquals(expected = graphQLType, actual = kGraphQLType.graphQLType)
17+
}
18+
}

0 commit comments

Comments
 (0)