Skip to content

Commit

Permalink
Improve output of AttributeSpec to accept CodeBlocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Dec 9, 2023
1 parent e9d7895 commit 23f62e9
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/main/java/io/outfoxx/swiftpoet/AttributeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class AttributeSpec internal constructor(
out.emitCode(identifier)
if (arguments.isNotEmpty()) {
out.emit("(")
out.emit(arguments.joinToString())
out.emitCode(
codeBlock = arguments.joinToCode(),
isConstantContext = true,
)
out.emit(")")
}
return out
Expand All @@ -37,18 +40,22 @@ class AttributeSpec internal constructor(
class Builder internal constructor(
val identifier: CodeBlock
) : Taggable.Builder<Builder>() {
internal val arguments = mutableListOf<String>()
internal val arguments = mutableListOf<CodeBlock>()

fun addArgument(code: String): Builder = apply {
arguments += CodeBlock.of(code)
}

fun addArgument(code: CodeBlock): Builder = apply {
arguments += code
}

fun addArguments(codes: List<String>): Builder = apply {
arguments += codes
arguments += codes.map(CodeBlock.Companion::of)
}

fun addArguments(vararg codes: String): Builder = apply {
arguments += codes
arguments += codes.map(CodeBlock.Companion::of)
}

fun build(): AttributeSpec =
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,16 @@ internal class CodeWriter constructor(

fun emitCode(format: String, vararg args: Any?) = emitCode(CodeBlock.of(format, *args))

fun emitCode(codeBlock: CodeBlock) = apply {
fun emitCode(
codeBlock: CodeBlock,
isConstantContext: Boolean = false,
) = apply {
var a = 0
val partIterator = codeBlock.formatParts.listIterator()
while (partIterator.hasNext()) {
val part = partIterator.next()
when (part) {
"%L" -> emitLiteral(codeBlock.args[a++])
"%L" -> emitLiteral(codeBlock.args[a++], isConstantContext)

"%N" -> emit(codeBlock.args[a++] as String)

Expand All @@ -213,7 +216,11 @@ internal class CodeWriter constructor(
// Emit null as a literal null: no quotes.
emit(
if (string != null)
stringLiteralWithQuotes(string) else
stringLiteralWithQuotes(
string,
isInsideRawString = false,
isConstantContext = isConstantContext,
) else
"null"
)
}
Expand Down Expand Up @@ -253,11 +260,11 @@ internal class CodeWriter constructor(
out.wrappingSpace(indentLevel + 2)
}

private fun emitLiteral(o: Any?) {
private fun emitLiteral(o: Any?, isConstantContext: Boolean) {
when (o) {
is AnyTypeSpec -> o.emit(this)
is PropertySpec -> o.emit(this, emptySet())
is CodeBlock -> emitCode(o)
is CodeBlock -> emitCode(o, isConstantContext = isConstantContext)
else -> emit(o.toString())
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ internal fun stringLiteralWithQuotes(
return result.toString()
} else {
val result = StringBuilder(value.length + 32)
// using pre-formatted strings allows us to get away with not escaping symbols that would
// normally require escaping, e.g. "foo ${"bar"} baz"
if (isInsideRawString) result.append("\"\"\"") else result.append('"')
for (c in value) {
// Trivial case: single quote must not be escaped.
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/io/outfoxx/swiftpoet/test/AttributeSpecTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.AttributeSpec
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.CodeWriter
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import java.io.StringWriter

@DisplayName("AttributeSpec Tests")
class AttributeSpecTests {
@Test
@DisplayName("Generates simple attribute")
fun testSimpleAttribute() {
val testAttr =
AttributeSpec.builder("objc")
.build()

val out = StringWriter()
testAttr.emit(CodeWriter(out))

assertThat(
out.toString(),
equalTo("@objc")
)
}

@Test
@DisplayName("Generates attribute with argument")
fun testAttributeWithArgument() {
val value = CodeBlock.of("%S", "someValue")
val testAttr =
AttributeSpec.builder("CustomAttribute")
.addArgument(CodeBlock.of("value: %L", value))
.build()

val out = StringWriter()
testAttr.emit(CodeWriter(out))

assertThat(
out.toString(),
equalTo("@CustomAttribute(value: \"someValue\")")
)
}
}
84 changes: 84 additions & 0 deletions src/test/java/io/outfoxx/swiftpoet/test/UtilTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.characterLiteralWithoutSingleQuotes
import io.outfoxx.swiftpoet.escapeIfNecessary
import io.outfoxx.swiftpoet.stringLiteralWithQuotes
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class UtilTests {
@Test fun characterLiteral() {
assertEquals("a", characterLiteralWithoutSingleQuotes('a'))
assertEquals("b", characterLiteralWithoutSingleQuotes('b'))
assertEquals("c", characterLiteralWithoutSingleQuotes('c'))
assertEquals("%", characterLiteralWithoutSingleQuotes('%'))
// common escapes
assertEquals("\\b", characterLiteralWithoutSingleQuotes('\b'))
assertEquals("\\t", characterLiteralWithoutSingleQuotes('\t'))
assertEquals("\\n", characterLiteralWithoutSingleQuotes('\n'))
assertEquals("\\u000c", characterLiteralWithoutSingleQuotes('\u000c'))
assertEquals("\\r", characterLiteralWithoutSingleQuotes('\r'))
assertEquals("\"", characterLiteralWithoutSingleQuotes('"'))
assertEquals("\\'", characterLiteralWithoutSingleQuotes('\''))
assertEquals("\\\\", characterLiteralWithoutSingleQuotes('\\'))
// octal escapes
assertEquals("\\u0000", characterLiteralWithoutSingleQuotes('\u0000'))
assertEquals("\\u0007", characterLiteralWithoutSingleQuotes('\u0007'))
assertEquals("?", characterLiteralWithoutSingleQuotes('\u003f'))
assertEquals("\\u007f", characterLiteralWithoutSingleQuotes('\u007f'))
assertEquals("¿", characterLiteralWithoutSingleQuotes('\u00bf'))
assertEquals("ÿ", characterLiteralWithoutSingleQuotes('\u00ff'))
// unicode escapes
assertEquals("\\u0000", characterLiteralWithoutSingleQuotes('\u0000'))
assertEquals("\\u0001", characterLiteralWithoutSingleQuotes('\u0001'))
assertEquals("\\u0002", characterLiteralWithoutSingleQuotes('\u0002'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u20AC'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u2603'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u2660'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u2663'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u2665'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u2666'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u2735'))
assertEquals("", characterLiteralWithoutSingleQuotes('\u273A'))
assertEquals("", characterLiteralWithoutSingleQuotes('\uFF0F'))
}

@Test fun stringLiteral() {
stringLiteral("abc")
stringLiteral("♦♥♠♣")
stringLiteral("\\t@\\t\${\'\$\'}", "\t@\t$")
assertThat(stringLiteralWithQuotes("abc();\ndef();"), equalTo("\"\"\"\n|abc();\n|def();\n\"\"\".trimMargin()"))
stringLiteral("This is \\\"quoted\\\"!", "This is \"quoted\"!")
stringLiteral("e^{i\\\\pi}+1=0", "e^{i\\pi}+1=0")
assertThat(stringLiteralWithQuotes("abc();\ndef();", isConstantContext = true), equalTo("\"abc();\\ndef();\""))
}

@Test fun escapeNonJavaIdentifiers() {
assertThat(escapeIfNecessary("8startWithNumber"), equalTo("`8startWithNumber`"))
assertThat(escapeIfNecessary("with-hyphen"), equalTo("`with-hyphen`"))
assertThat(escapeIfNecessary("with space"), equalTo("`with space`"))
assertThat(escapeIfNecessary("with_unicode_punctuation\\u2026"), equalTo("`with_unicode_punctuation\\u2026`"))
}

private fun stringLiteral(string: String) = stringLiteral(string, string)
private fun stringLiteral(expected: String, value: String) =
assertEquals("\"$expected\"", stringLiteralWithQuotes(value))
}

0 comments on commit 23f62e9

Please sign in to comment.