Skip to content

Commit 387c2c3

Browse files
authored
Reorganize parser, introspection and merger test (#6857)
* Reorganize parser, introspection and merger test * Add missing test fixtures
1 parent 1c54e7c commit 387c2c3

File tree

55 files changed

+1767
-2109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1767
-2109
lines changed

libraries/apollo-ast/src/commonTest/kotlin/com/apollographql/apollo/graphql/ast/test/CommonParserTest.kt

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.apollographql.apollo.graphql.ast.test
2+
3+
import com.apollographql.apollo.ast.GQLField
4+
import com.apollographql.apollo.ast.GQLFieldDefinition
5+
import com.apollographql.apollo.ast.GQLObjectTypeDefinition
6+
import com.apollographql.apollo.ast.GQLOperationDefinition
7+
import com.apollographql.apollo.ast.parseAsGQLDocument
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
class SourceLocationTest {
12+
private inline fun <reified T> Any.cast() = this as T
13+
14+
@Test
15+
fun endLineAndColumn() {
16+
"""
17+
# comment before
18+
query Test {
19+
# comment here
20+
field(
21+
first: 100
22+
)
23+
}
24+
""".trimIndent()
25+
.parseAsGQLDocument()
26+
.getOrThrow()
27+
.definitions
28+
.first()
29+
.cast<GQLOperationDefinition>()
30+
.selections
31+
.first()
32+
.cast<GQLField>()
33+
.sourceLocation!!
34+
.apply {
35+
assertEquals(55, start)
36+
assertEquals(82, end)
37+
assertEquals(4, line)
38+
assertEquals(3, column)
39+
}
40+
}
41+
42+
@Test
43+
fun endLineAndColumn2() {
44+
"""
45+
type Something {
46+
fieldA: String
47+
}
48+
""".trimIndent()
49+
.parseAsGQLDocument()
50+
.getOrThrow()
51+
.definitions
52+
.first()
53+
.cast<GQLObjectTypeDefinition>()
54+
.fields
55+
.first()
56+
.cast<GQLFieldDefinition>()
57+
.sourceLocation!!
58+
.apply {
59+
assertEquals(20, start)
60+
assertEquals(34, end)
61+
assertEquals(2, line)
62+
assertEquals(3, column)
63+
}
64+
}
65+
}

libraries/apollo-ast/src/commonTest/kotlin/com/apollographql/apollo/graphql/ast/test/StringTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.apollographql.apollo.graphql.ast.test
22

3+
import com.apollographql.apollo.ast.GQLStringValue
34
import com.apollographql.apollo.ast.encodeToGraphQLSingleQuoted
5+
import com.apollographql.apollo.ast.parseAsGQLValue
46
import kotlin.test.Test
57
import kotlin.test.assertEquals
68

@@ -14,4 +16,23 @@ class StringTest {
1416
fun controlCodesAreEscaped() {
1517
assertEquals("\\n\\u0003", "\n\u0003".encodeToGraphQLSingleQuoted())
1618
}
19+
20+
@Test
21+
fun blockString() {
22+
val value = "\"\"\" \\\"\"\" \"\"\"".parseAsGQLValue().getOrThrow()
23+
24+
assertEquals(" \"\"\" ", (value as GQLStringValue).value)
25+
}
26+
27+
@Test
28+
fun blockStringIndentationIsRemoved() {
29+
val value = ("\"\"\"\n" +
30+
" first line\n" +
31+
" \n" +
32+
" second line\n" +
33+
" \n" +
34+
"\"\"\"").parseAsGQLValue().getOrThrow()
35+
36+
assertEquals("first line\n \nsecond line", (value as GQLStringValue).value)
37+
}
1738
}

libraries/apollo-ast/src/jvmTest/kotlin/com/apollographql/apollo/graphql/ast/test/IntrospectionTest.jvm.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.apollographql.apollo.graphql.ast.test
2+
3+
import com.apollographql.apollo.ast.introspection.toGQLDocument
4+
import com.apollographql.apollo.ast.introspection.toIntrospectionSchema
5+
import com.apollographql.apollo.ast.validateAsSchema
6+
import com.google.testing.junit.testparameterinjector.TestParameter
7+
import com.google.testing.junit.testparameterinjector.TestParameterInjector
8+
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
9+
import org.junit.Test
10+
import org.junit.runner.RunWith
11+
import java.io.File
12+
13+
@RunWith(TestParameterInjector::class)
14+
class IntrospectionTest {
15+
@Test
16+
fun canReadIntrospectionResults(@TestParameter(valuesProvider = ParametersProvider::class) graphqlFile: File) {
17+
graphqlFile
18+
.toIntrospectionSchema()
19+
.toGQLDocument()
20+
.validateAsSchema()
21+
.getOrThrow()
22+
}
23+
24+
class ParametersProvider : TestParameterValuesProvider() {
25+
override fun provideValues(context: Context?): List<File> {
26+
return File("test-fixtures/introspection/")
27+
.listFiles()!!
28+
.filter { it.name.endsWith(".json") }
29+
.filter {
30+
testFilterMatches(it.name)
31+
}
32+
}
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.apollographql.apollo.graphql.ast.test
2+
3+
import com.apollographql.apollo.ast.MergeOptions
4+
import com.apollographql.apollo.ast.ParserOptions
5+
import com.apollographql.apollo.ast.mergeExtensions
6+
import com.apollographql.apollo.ast.parseAsGQLDocument
7+
import com.google.testing.junit.testparameterinjector.TestParameter
8+
import com.google.testing.junit.testparameterinjector.TestParameterInjector
9+
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
10+
import org.junit.runner.RunWith
11+
import java.io.File
12+
import kotlin.test.Test
13+
14+
@RunWith(TestParameterInjector::class)
15+
class MergerTest {
16+
@Test
17+
fun test(@TestParameter(valuesProvider = ParametersProvider::class) graphqlFile: File) {
18+
checkExpected(graphqlFile) {
19+
val pragmas = it.readLines().takeWhile { it.startsWith("# PRAGMA") }.map {
20+
it.substring("# PRAGMA".length).trim()
21+
}.toSet()
22+
it.parseAsGQLDocument()
23+
.getOrThrow()
24+
.mergeExtensions(MergeOptions("allowMergingFieldDefinitions" in pragmas))
25+
.serialize()
26+
}
27+
}
28+
29+
class ParametersProvider : TestParameterValuesProvider() {
30+
override fun provideValues(context: Context?): List<File> {
31+
return File("test-fixtures/merger/")
32+
.listFiles()!!
33+
.filter { it.name.endsWith(".graphql") && !it.name.endsWith(".expected.graphql") }
34+
.sortedBy { it.name }
35+
.filter {
36+
testFilterMatches(it.name)
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)