Skip to content

Commit

Permalink
Use correct builders for ServerEventStreamMarshallerGeneratorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Dec 20, 2022
1 parent a47597f commit 0b77d8f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ package software.amazon.smithy.rust.codegen.server.smithy.protocols.eventstream
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestModels
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestRequirements
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenConfig
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerBuilderGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerBuilderGeneratorWithoutPublicConstrainedTypes
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestRustSettings

data class TestCase(
val eventStreamTestCase: EventStreamTestModels.TestCase,
val publicConstrainedTypes: Boolean,
) {
override fun toString(): String = "$eventStreamTestCase, publicConstrainedTypes = $publicConstrainedTypes"
}

abstract class ServerEventStreamBaseRequirements : EventStreamTestRequirements<ServerCodegenContext> {
abstract val publicConstrainedTypes: Boolean

Expand All @@ -30,4 +43,26 @@ abstract class ServerEventStreamBaseRequirements : EventStreamTestRequirements<S
),
protocolShapeId,
)

override fun renderBuilderForShape(
writer: RustWriter,
codegenContext: ServerCodegenContext,
shape: StructureShape,
) {
if (codegenContext.settings.codegenConfig.publicConstrainedTypes) {
ServerBuilderGenerator(codegenContext, shape).apply {
render(writer)
writer.implBlock(shape, codegenContext.symbolProvider) {
renderConvenienceMethod(writer)
}
}
} else {
ServerBuilderGeneratorWithoutPublicConstrainedTypes(codegenContext, shape).apply {
render(writer)
writer.implBlock(shape, codegenContext.symbolProvider) {
renderConvenienceMethod(writer)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.generators.implBlock
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.EventStreamMarshallerGenerator
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestModels
Expand All @@ -23,25 +20,29 @@ import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestVariety
import software.amazon.smithy.rust.codegen.core.testutil.TestEventStreamProject
import software.amazon.smithy.rust.codegen.core.testutil.TestRuntimeConfig
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerBuilderGenerator
import java.util.stream.Stream

class MarshallTestCasesProvider : ArgumentsProvider {
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> =
// Don't include awsQuery or ec2Query for now since marshall support for them is unimplemented
EventStreamTestModels.TEST_CASES
.filter { testCase -> !testCase.protocolShapeId.contains("Query") }
.map { Arguments.of(it) }.stream()
.flatMap { testCase ->
listOf(
TestCase(testCase, publicConstrainedTypes = false),
TestCase(testCase, publicConstrainedTypes = true),
)
}.map { Arguments.of(it) }.stream()
}

class ServerEventStreamMarshallerGeneratorTest {
@ParameterizedTest
@ArgumentsSource(MarshallTestCasesProvider::class)
fun test(testCase: EventStreamTestModels.TestCase) {
fun test(testCase: TestCase) {
EventStreamTestTools.runTestCase(
testCase,
testCase.eventStreamTestCase,
object : ServerEventStreamBaseRequirements() {
override val publicConstrainedTypes: Boolean get() = true
override val publicConstrainedTypes: Boolean get() = testCase.publicConstrainedTypes

override fun renderGenerator(
codegenContext: ServerCodegenContext,
Expand All @@ -55,22 +56,9 @@ class ServerEventStreamMarshallerGeneratorTest {
project.symbolProvider,
project.streamShape,
protocol.structuredDataSerializer(project.operationShape),
testCase.requestContentType,
testCase.eventStreamTestCase.requestContentType,
).render()
}

override fun renderBuilderForShape(
writer: RustWriter,
codegenContext: ServerCodegenContext,
shape: StructureShape,
) {
ServerBuilderGenerator(codegenContext, shape).apply {
render(writer)
writer.implBlock(shape, codegenContext.symbolProvider) {
renderConvenienceMethod(writer)
}
}
}
},
CodegenTarget.SERVER,
EventStreamTestVariety.Marshall,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,13 @@ import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.serverBuilderSymbol
import java.util.stream.Stream

data class TestCase(
val eventStreamTestCase: EventStreamTestModels.TestCase,
val publicConstrainedTypes: Boolean,
) {
override fun toString(): String = "$eventStreamTestCase, publicConstrainedTypes = $publicConstrainedTypes"
}

class UnmarshallTestCasesProvider : ArgumentsProvider {
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> =
EventStreamTestModels.TEST_CASES.flatMap { testCase ->
listOf(
// TODO(https://github.com/awslabs/smithy-rs/issues/1442): Enable tests for `publicConstrainedTypes = false`
// TestCase(testCase, false),
TestCase(testCase, true),
// TestCase(testCase, publicConstrainedTypes = false),
TestCase(testCase, publicConstrainedTypes = true),
)
}.map { Arguments.of(it) }.stream()
}
Expand Down Expand Up @@ -69,38 +62,16 @@ class ServerEventStreamUnmarshallerGeneratorTest {
).render()
}

// TODO(https://github.com/awslabs/smithy-rs/issues/1442): Delete this function override to use the correct builder from the parent class
override fun renderBuilderForShape(
writer: RustWriter,
codegenContext: ServerCodegenContext,
shape: StructureShape,
) {
if (testCase.publicConstrainedTypes) {
// TODO(https://github.com/awslabs/smithy-rs/issues/1442): Use the ServerBuilderGenerator:
// ServerBuilderGenerator(codegenContext, shape).apply {
// render(writer)
// writer.implBlock(shape, codegenContext.symbolProvider) {
// renderConvenienceMethod(writer)
// }
// }
BuilderGenerator(codegenContext.model, codegenContext.symbolProvider, shape).apply {
render(writer)
writer.implBlock(shape, codegenContext.symbolProvider) {
renderConvenienceMethod(writer)
}
}
} else {
// TODO(https://github.com/awslabs/smithy-rs/issues/1442): Use the ServerBuilderGeneratorWithoutPublicConstraintedTypes:
// ServerBuilderGeneratorWithoutPublicConstrainedTypes(codegenContext, shape).apply {
// render(writer)
// writer.implBlock(shape, codegenContext.symbolProvider) {
// renderConvenienceMethod(writer)
// }
// }
BuilderGenerator(codegenContext.model, codegenContext.symbolProvider, shape).apply {
render(writer)
writer.implBlock(shape, codegenContext.symbolProvider) {
renderConvenienceMethod(writer)
}
BuilderGenerator(codegenContext.model, codegenContext.symbolProvider, shape).apply {
render(writer)
writer.implBlock(shape, codegenContext.symbolProvider) {
renderConvenienceMethod(writer)
}
}
}
Expand Down

0 comments on commit 0b77d8f

Please sign in to comment.