Skip to content

Commit

Permalink
Deprecate match() method in MediaType and GeographicLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgirro committed May 4, 2021
1 parent 057fb6d commit f59bb8c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 35 deletions.
4 changes: 4 additions & 0 deletions api/stalla.api
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ public class dev/stalla/model/MediaType {
public fun hashCode ()I
public final fun match (Ldev/stalla/model/MediaType;)Z
public final fun match (Ljava/lang/String;)Z
public final fun matches (Ldev/stalla/model/MediaType;)Z
public final fun matches (Ljava/lang/String;)Z
public static fun of (Ljava/lang/String;)Ldev/stalla/model/MediaType;
public final fun parameter (Ljava/lang/String;)Ljava/lang/String;
public fun toString ()Ljava/lang/String;
Expand Down Expand Up @@ -1419,6 +1421,8 @@ public final class dev/stalla/model/podcastindex/GeographicLocation {
public fun hashCode ()I
public final fun match (Ldev/stalla/model/podcastindex/GeographicLocation;)Z
public final fun match (Ljava/lang/String;)Z
public final fun matches (Ldev/stalla/model/podcastindex/GeographicLocation;)Z
public final fun matches (Ljava/lang/String;)Z
public static fun of (Ljava/lang/String;)Ldev/stalla/model/podcastindex/GeographicLocation;
public final fun parameter (Ljava/lang/String;)Ljava/lang/String;
public fun toString ()Ljava/lang/String;
Expand Down
23 changes: 21 additions & 2 deletions src/main/kotlin/dev/stalla/model/MediaType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public open class MediaType private constructor(
/**
* Checks if `this` type matches a [pattern] type taking into account placeholder symbols `*` and parameters.
*/
public fun match(pattern: MediaType?): Boolean {
public fun matches(pattern: MediaType?): Boolean {
contract {
returns(true) implies (pattern != null)
}
Expand All @@ -109,7 +109,26 @@ public open class MediaType private constructor(
* Checks if `this` type matches a [pattern] type taking
* into account placeholder symbols `*` and parameters.
*/
public fun match(pattern: String): Boolean = match(of(pattern))
public fun matches(pattern: String): Boolean = matches(of(pattern))

/**
* Checks if `this` type matches a [pattern] type taking into account placeholder symbols `*` and parameters.
*/
@Deprecated(
message = "This method is scheduled for removal in v2.0.0",
replaceWith = ReplaceWith("matches")
)
public fun match(pattern: MediaType?): Boolean = matches(pattern)

/**
* Checks if `this` type matches a [pattern] type taking
* into account placeholder symbols `*` and parameters.
*/
@Deprecated(
message = "This method is scheduled for removal in v2.0.0",
replaceWith = ReplaceWith("matches")
)
public fun match(pattern: String): Boolean = matches(pattern)

private fun match(parameters1: List<Parameter>, parameters2: List<Parameter>): Boolean {
for ((patternName, patternValue) in parameters1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class GeographicLocation public constructor(
public fun withoutParameters(): GeographicLocation = GeographicLocation(latitude, longitude, altitude)

/** Checks if `this` type matches a [pattern] type taking parameters into account. */
public fun match(pattern: GeographicLocation?): Boolean {
public fun matches(pattern: GeographicLocation?): Boolean {
contract {
returns(true) implies (pattern != null)
}
Expand All @@ -132,7 +132,21 @@ public class GeographicLocation public constructor(
}

/** Checks if `this` type matches a [pattern] type taking parameters into account. */
public fun match(pattern: String): Boolean = match(of(pattern))
public fun matches(pattern: String): Boolean = matches(of(pattern))

/** Checks if `this` type matches a [pattern] type taking parameters into account. */
@Deprecated(
message = "This method is scheduled for removal in v2.0.0",
replaceWith = ReplaceWith("matches")
)
public fun match(pattern: GeographicLocation?): Boolean = matches(pattern)

/** Checks if `this` type matches a [pattern] type taking parameters into account. */
@Deprecated(
message = "This method is scheduled for removal in v2.0.0",
replaceWith = ReplaceWith("matches")
)
public fun match(pattern: String): Boolean = matches(pattern)

private fun match(parameters1: List<Parameter>, parameters2: List<Parameter>): Boolean {
for (param1 in parameters1) {
Expand Down
26 changes: 13 additions & 13 deletions src/test/kotlin/dev/stalla/Assertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,44 +158,44 @@ internal fun Assert<MediaType>.equalToString(expected: String) = given { mediaTy

/** Asserts that [MediaType.toString] matches the expected value. */
internal fun Assert<MediaType>.equalToString(expected: MediaType) = given { mediaType ->
if (mediaType.match(expected)) return@given
if (mediaType.matches(expected)) return@given
expected(
"to be: '$expected' but was: '$mediaType'",
expected = expected,
actual = mediaType
)
}

/** Asserts that [MediaType.match] matches the expected value. */
/** Asserts that [MediaType.matches] matches the expected value. */
internal fun Assert<MediaType>.matchPattern(expected: String) = given { mediaType ->
if (mediaType.match(expected)) return@given
if (mediaType.matches(expected)) return@given
expected(
"to have the string form: '$expected' but was: '$mediaType'",
expected = expected,
actual = mediaType.toString()
)
}

/** Asserts that [MediaType.match] matches the expected value. */
/** Asserts that [MediaType.matches] matches the expected value. */
internal fun Assert<MediaType>.matchPattern(expected: MediaType) = given { mediaType ->
if (mediaType.match(expected)) return@given
if (mediaType.matches(expected)) return@given
expected(
"to be: '$expected' but was: '$mediaType'",
expected = expected,
actual = mediaType
)
}

/** Asserts that this matches [expected] symmetrically using [MediaType.match]. */
/** Asserts that this matches [expected] symmetrically using [MediaType.matches]. */
internal fun Assert<MediaType>.matchesSymmetrically(expected: MediaType?) = given { mediaType ->
if (!mediaType.match(expected)) {
if (!mediaType.matches(expected)) {
expected(
"to match symmetrically, but the direction: '$mediaType' match '$expected' failed",
expected = true,
actual = false
)
}
if (!expected.match(mediaType)) {
if (!expected.matches(mediaType)) {
expected(
"to match symmetrically, but the direction: '$expected' match '$mediaType' failed",
expected = true,
Expand All @@ -204,16 +204,16 @@ internal fun Assert<MediaType>.matchesSymmetrically(expected: MediaType?) = give
}
}

/** Asserts that this does not match [expected] symmetrically using [MediaType.match]. */
/** Asserts that this does not match [expected] symmetrically using [MediaType.matches]. */
internal fun Assert<MediaType>.doesNotMatchSymmetrically(expected: MediaType?) = given { mediaType ->
if (mediaType.match(expected)) {
if (mediaType.matches(expected)) {
expected(
"to not match symmetrically, but the direction: '$mediaType' match '$expected' succeeded",
expected = false,
actual = true
)
}
if (expected != null && expected.match(mediaType)) {
if (expected != null && expected.matches(mediaType)) {
expected(
"to not match symmetrically, but the direction: '$expected' match '$mediaType' succeeded",
expected = false,
Expand All @@ -222,9 +222,9 @@ internal fun Assert<MediaType>.doesNotMatchSymmetrically(expected: MediaType?) =
}
}

/** Asserts that [GeographicLocation.match] matches the expected value. */
/** Asserts that [GeographicLocation.matches] matches the expected value. */
internal fun Assert<GeographicLocation>.matchPattern(expected: GeographicLocation) = given { geoLocation ->
if (geoLocation.match(expected)) return@given
if (geoLocation.matches(expected)) return@given
expected(
"to be: '$expected', but was: '$geoLocation'",
expected = expected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ internal class ValidatingPodcastBuilderTest {
feedpressBuilder.newsletterId("feedpress newsletterId")
fyydBuilder.verify("fyyd verify")
googleplayBuilder.description("play description")
podcastPodcastindexBuilder.lockedBuilder(expectedLockedBuilder)
podcastindexBuilder.lockedBuilder(expectedLockedBuilder)
}

assertAll {
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/dev/stalla/model/MediaTypeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ internal class MediaTypeTest {

@Test
fun `should match a predefined instance from a string pattern`() {
assertThat(MediaType.AAC_AUDIO.match("audio/aac")).isTrue()
assertThat(MediaType.AAC_AUDIO.matches("audio/aac")).isTrue()
}

@Test
fun `should match a predefined instance with a wildcard subtype`() {
assertThat(MediaType.AAC_AUDIO.match("audio/*")).isTrue()
assertThat(MediaType.AAC_AUDIO.matches("audio/*")).isTrue()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,76 +164,76 @@ class GeographicLocationTest {
fun `should match Geo URIs when WGS-84 special pole case applies correctly`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:90,-22.43;crs=WGS84"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:90,46;crs=WGS84"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should not match Geo URIs in WGS-84 special pole case if the latitude is different`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:90,10"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:45,20"))
assertThat(geoLocation1.match(geoLocation2)).isFalse()
assertThat(geoLocation1.matches(geoLocation2)).isFalse()
}

@Test
fun `should no match Geo URIs in WGS-84 special pole case if the latitude has a different sign`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:90,10"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:-90,10"))
assertThat(geoLocation1.match(geoLocation2)).isFalse()
assertThat(geoLocation1.matches(geoLocation2)).isFalse()
}

@Test
fun `should match Geo URIs in WGS-84 special pole case by ignoring the longitude`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:90,5"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:90,10"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should interprete a missing CRS value as WGS-84 when matching two Geo URIs with WGS-84 special pole case`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:90,5"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:90,10;crs=WGS84"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should match Geo URIs in WGS-84 special date line case if the longitude has a different sign`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:10,180"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:10,-180"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should interprete a missing CRS value as WGS-84 when matching two Geo URIs with WGS-84 special date line case`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:10,180"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:10,-180;crs=WGS84"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should match Geo URIs parameters bitwise identical after percent-decoding parameter names are case insensitive`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:66,30;u=6.500;FOo=this%2dthat"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:66.0,30;u=6.5;foo=this-that"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should match Geo URIs with parameter order being insignificant`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:47,11;foo=blue;bar=white"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:47,11;bar=white;foo=blue"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should match Geo URIs with parameter keys being case-insensitive`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:22,0;bar=blue"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:22,0;BAR=blue"))
assertThat(geoLocation1.match(geoLocation2)).isTrue()
assertThat(geoLocation1.matches(geoLocation2)).isTrue()
}

@Test
fun `should not match Geo URIs with parameter values being case-sensitive`() {
val geoLocation1 = checkNotNull(GeographicLocation.of("geo:22,0;bar=BLUE"))
val geoLocation2 = checkNotNull(GeographicLocation.of("geo:22,0;bar=blue"))
assertThat(geoLocation1.match(geoLocation2)).isFalse()
assertThat(geoLocation1.matches(geoLocation2)).isFalse()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal class PodcastindexParserTest : NamespaceParserTest() {
val builder = FakePodcastBuilder()
channel.parseChannelChildNodes(builder)

assertThat(builder.podcastPodcastindexBuilder, "channel.podcastindex").isNotNull().all {
assertThat(builder.podcastindexBuilder, "channel.podcastindex").isNotNull().all {
prop(FakePodcastPodcastindexBuilder::lockedBuilderValue).isEqualTo(expectedLockedBuilder)
prop(FakePodcastPodcastindexBuilder::fundingBuilders).containsExactly(expectedFundingBuilder)
prop(FakePodcastPodcastindexBuilder::personBuilders).containsExactly(expectedPodcastPersonBuilder)
Expand All @@ -111,7 +111,7 @@ internal class PodcastindexParserTest : NamespaceParserTest() {
val builder = FakePodcastBuilder()
channel.parseChannelChildNodes(builder)

assertThat(builder.podcastPodcastindexBuilder, "channel.podcastindex").all {
assertThat(builder.podcastindexBuilder, "channel.podcastindex").all {
prop(FakePodcastPodcastindexBuilder::lockedBuilderValue).isNull()
prop(FakePodcastPodcastindexBuilder::fundingBuilders).isEmpty()
prop(FakePodcastPodcastindexBuilder::personBuilders).isEmpty()
Expand All @@ -125,7 +125,7 @@ internal class PodcastindexParserTest : NamespaceParserTest() {
val builder = FakePodcastBuilder()
channel.parseChannelChildNodes(builder)

assertThat(builder.podcastPodcastindexBuilder, "channel.podcastindex").all {
assertThat(builder.podcastindexBuilder, "channel.podcastindex").all {
prop(FakePodcastPodcastindexBuilder::lockedBuilderValue).isNull()
prop(FakePodcastPodcastindexBuilder::fundingBuilders).isEmpty()
prop(FakePodcastPodcastindexBuilder::personBuilders).isEmpty()
Expand Down Expand Up @@ -191,7 +191,7 @@ internal class PodcastindexParserTest : NamespaceParserTest() {
val builder = FakePodcastBuilder()
channel.parseChannelChildNodes(builder)

assertThat(builder.podcastPodcastindexBuilder, "channel.podcastindex").all {
assertThat(builder.podcastindexBuilder, "channel.podcastindex").all {
prop(FakePodcastPodcastindexBuilder::lockedBuilderValue).isNull()
}
}
Expand Down

0 comments on commit f59bb8c

Please sign in to comment.