Skip to content

Commit

Permalink
fix: empty* comparison (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez authored Jun 18, 2024
1 parent 677be9b commit 8209f96
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class DefaultEndpointProviderTestGenerator(
fun render() {
writer.addImport("*", namespace = "kotlin.test")
writer.withBlock("public class #L {", "}", CLASS_NAME) {
writer.write("")
writer.withBlock(
"private fun expectEqualEndpoints(expected: #1T, actual: #1T) {",
"}",
Expand All @@ -79,19 +78,15 @@ class DefaultEndpointProviderTestGenerator(
RuntimeTypes.Core.Collections.toMutableAttributes,
)
writer.write(
"if (actual.attributes.contains(#1T)) newActualAttributes.remove(#1T)",
"newActualAttributes.remove(#T)",
RuntimeTypes.Core.BusinessMetrics.ServiceEndpointOverride,
)
writer.write(
"if (actual.attributes.contains(#1T)) newActualAttributes.remove(#1T)",
"newActualAttributes.remove(#T)",
RuntimeTypes.Core.BusinessMetrics.AccountIdBasedEndpointAccountId,
)
writer.write(
"val newActualAttributesOrEmpty = if (newActualAttributes.isEmpty) #T() else newActualAttributes",
RuntimeTypes.Core.Collections.emptyAttributes,
)
writer.write(
"val newActual = #T(actual.uri, actual.headers, newActualAttributesOrEmpty)",
"val newActual = #T(actual.uri, actual.headers, newActualAttributes)",
RuntimeTypes.SmithyClient.Endpoints.Endpoint,
)
writer.write("assertEquals(expected, newActual)")
Expand Down
2 changes: 2 additions & 0 deletions runtime/protocol/http/api/http.api
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class aws/smithy/kotlin/runtime/http/HeadersBuilder : aws/smithy/ko

public abstract class aws/smithy/kotlin/runtime/http/HttpBody {
public static final field Companion Laws/smithy/kotlin/runtime/http/HttpBody$Companion;
public fun equals (Ljava/lang/Object;)Z
public fun getContentLength ()Ljava/lang/Long;
public fun isDuplex ()Z
public fun isOneShot ()Z
Expand All @@ -74,6 +75,7 @@ public final class aws/smithy/kotlin/runtime/http/HttpBody$Companion {

public final class aws/smithy/kotlin/runtime/http/HttpBody$Empty : aws/smithy/kotlin/runtime/http/HttpBody {
public static final field INSTANCE Laws/smithy/kotlin/runtime/http/HttpBody$Empty;
public fun equals (Ljava/lang/Object;)Z
public fun getContentLength ()Ljava/lang/Long;
public final fun isOneShot ()Z
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private object EmptyDeferredHeaders : DeferredHeaders {
override fun entries(): Set<Map.Entry<String, List<Deferred<String>>>> = emptySet()
override fun contains(name: String): Boolean = false
override fun isEmpty(): Boolean = true
override fun equals(other: Any?): Boolean = other is DeferredHeaders && other.isEmpty()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private object EmptyHeaders : Headers {
override fun entries(): Set<Map.Entry<String, List<String>>> = emptySet()
override fun contains(name: String): Boolean = false
override fun isEmpty(): Boolean = true
override fun equals(other: Any?): Boolean = other is Headers && other.isEmpty()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ public sealed class HttpBody {
*/
public open val isDuplex: Boolean = false

override fun equals(other: Any?): Boolean =
other is HttpBody &&
other.contentLength == contentLength &&
other.isOneShot == isOneShot &&
other.isDuplex == isDuplex

/**
* Variant of a [HttpBody] without a payload
*/
public object Empty : HttpBody() {
final override val isOneShot: Boolean = false
override val contentLength: Long = 0
override fun equals(other: Any?): Boolean = other is HttpBody && other.contentLength == contentLength
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.smithy.kotlin.runtime.http

import kotlin.test.Test
import kotlin.test.assertEquals

class DeferredHeadersTest {
@Test
fun testEmptyEquals() {
val explicitlyEmpty = DeferredHeaders.Empty
val implicitlyEmpty = DeferredHeadersBuilder().build()

assertEquals(implicitlyEmpty, explicitlyEmpty)
assertEquals(explicitlyEmpty, implicitlyEmpty)

assertEquals(explicitlyEmpty, explicitlyEmpty)
assertEquals(implicitlyEmpty, implicitlyEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ class HeadersTest {
assertEquals(firstExpected.entries, first.entries())
assertEquals(secondExpected.entries, second.entries())
}

@Test
fun testEmptyEquals() {
val explicitlyEmpty = Headers.Empty
val implicitlyEmpty = HeadersBuilder().build()

assertEquals(implicitlyEmpty, explicitlyEmpty)
assertEquals(explicitlyEmpty, implicitlyEmpty)

assertEquals(explicitlyEmpty, explicitlyEmpty)
assertEquals(implicitlyEmpty, implicitlyEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ class HttpBodyTest {

assertEquals(expected, body.readAll()!!.decodeToString())
}

@Test
fun testEmptyEquals() {
val explicitlyEmpty = HttpBody.Empty
val implicitlyEmpty = HttpBody.fromBytes(byteArrayOf())

assertEquals(implicitlyEmpty, explicitlyEmpty)
assertEquals(explicitlyEmpty, implicitlyEmpty)

assertEquals(implicitlyEmpty, implicitlyEmpty)
assertEquals(explicitlyEmpty, explicitlyEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ private object EmptyAttributes : Attributes {
override val keys: Set<AttributeKey<*>> = emptySet()
override fun contains(key: AttributeKey<*>): Boolean = false
override fun <T : Any> getOrNull(key: AttributeKey<T>): T? = null
override fun equals(other: Any?): Boolean = other is Attributes && other.isEmpty
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,16 @@ class AttributesTest {
assertEquals("foo", attrs[attr1])
assertEquals(57, attrs[attr2])
}

@Test
fun testEmptyEquals() {
val explicitlyEmpty = emptyAttributes()
val implicitlyEmpty = AttributesBuilder().attributes

assertEquals(implicitlyEmpty, explicitlyEmpty)
assertEquals(explicitlyEmpty, implicitlyEmpty)

assertEquals(explicitlyEmpty, explicitlyEmpty)
assertEquals(implicitlyEmpty, implicitlyEmpty)
}
}

0 comments on commit 8209f96

Please sign in to comment.