-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support generic qualifiers for
TypeName
Allows qualifying a `TypeName` with a generic qaulifier (`some` or `any`).
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/io/outfoxx/swiftpoet/GenericQualifiedTypeName.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.outfoxx.swiftpoet | ||
|
||
/** | ||
* Qualify a [TypeName] with a [GenericQualifier] (any or some). | ||
* | ||
* `DataProtocol` qualified with `any` is | ||
* ``` | ||
* any DataProtocol | ||
* ``` | ||
*/ | ||
class GenericQualifiedTypeName internal constructor( | ||
val type: TypeName, | ||
val qualifier: GenericQualifier, | ||
) : TypeName() { | ||
|
||
override fun emit(out: CodeWriter): CodeWriter { | ||
out.emitCode("${qualifier.name.lowercase()} ") | ||
if (type is ComposedTypeName || type is GenericQualifiedTypeName) { | ||
out.emitCode("(%T)", type) | ||
} else { | ||
out.emitCode("%T", type) | ||
} | ||
return out | ||
} | ||
|
||
companion object { | ||
|
||
fun of(type: TypeName, qualifier: GenericQualifier): GenericQualifiedTypeName { | ||
return GenericQualifiedTypeName(type, qualifier) | ||
} | ||
|
||
fun any(type: TypeName): GenericQualifiedTypeName { | ||
return of(type, GenericQualifier.ANY) | ||
} | ||
|
||
fun some(type: TypeName): GenericQualifiedTypeName { | ||
return of(type, GenericQualifier.SOME) | ||
} | ||
} | ||
|
||
} | ||
|
||
enum class GenericQualifier { | ||
ANY, | ||
SOME | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/io/outfoxx/swiftpoet/test/GenericQualifiedTypeNameTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2018 Outfox, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.outfoxx.swiftpoet.test | ||
|
||
import io.outfoxx.swiftpoet.ComposedTypeName | ||
import io.outfoxx.swiftpoet.DeclaredTypeName | ||
import io.outfoxx.swiftpoet.GenericQualifiedTypeName | ||
import io.outfoxx.swiftpoet.GenericQualifier | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
|
||
class GenericQualifiedTypeNameTests { | ||
|
||
@Test | ||
@DisplayName("Generates generic qualified using explicit builders") | ||
fun genericQualifiedTypeNameExplicit() { | ||
val type = DeclaredTypeName.typeName(".GenericType") | ||
|
||
assertThat(GenericQualifiedTypeName.any(type).name, equalTo("any GenericType")) | ||
assertThat(GenericQualifiedTypeName.some(type).name, equalTo("some GenericType")) | ||
} | ||
|
||
@Test | ||
@DisplayName("Generates generic qualified declared type names") | ||
fun genericQualifiedDeclaredTypeName() { | ||
val type = DeclaredTypeName.typeName(".GenericType") | ||
|
||
assertThat(type.qualify(GenericQualifier.ANY).name, equalTo("any GenericType")) | ||
assertThat(type.qualify(GenericQualifier.SOME).name, equalTo("some GenericType")) | ||
} | ||
|
||
@Test | ||
@DisplayName("Generates generic qualified composite type names") | ||
fun genericQualifiedCompositeTypeName() { | ||
val typeA = DeclaredTypeName.typeName(".A") | ||
val typeB = DeclaredTypeName.typeName(".B") | ||
val typeC = DeclaredTypeName.typeName(".C") | ||
val composed = ComposedTypeName.composed(typeA, typeB, typeC) | ||
|
||
assertThat(composed.qualify(GenericQualifier.ANY).name, equalTo("any (A & B & C)")) | ||
assertThat(composed.qualify(GenericQualifier.SOME).name, equalTo("some (A & B & C)")) | ||
} | ||
} |