Skip to content

Commit

Permalink
Add some and improve other tests (more assertk idiomatic)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgirro committed Apr 29, 2021
1 parent 3027242 commit d2135d8
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 61 deletions.
235 changes: 191 additions & 44 deletions api/stalla.api

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/test/kotlin/dev/stalla/Assertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,43 @@ internal fun Assert<GeographicLocation>.matchPattern(expected: GeographicLocatio
actual = geoLocation
)
}

/** Asserts that there exists a [MediaType.Parameter] with the expected key and value. */
@JvmName("hasMediaTypeParameterWithValue")
internal fun Assert<MediaType>.hasParameterWithValue(expectedKey: String, expectedValue: String) = given { mediaType ->
val actualValue = mediaType.parameter(expectedKey)
if (actualValue != null && actualValue == expectedValue) return@given
if (actualValue == null) {
expected(
"to be have a value for key: '$expectedKey'",
expected = expectedValue,
actual = actualValue
)
} else {
expected(
"to be: $expectedKey=$expectedValue but was: $expectedKey=$actualValue",
expected = expectedValue,
actual = actualValue
)
}
}

/** Asserts that there exists a [GeographicLocation.Parameter] with the expected key and value. */
@JvmName("hasGeographicLocationParameterWithValue")
internal fun Assert<GeographicLocation>.hasParameterWithValue(expectedKey: String, expectedValue: String) = given { geo ->
val actualValue = geo.parameter(expectedKey)
if (actualValue != null && actualValue == expectedValue) return@given
if (actualValue == null) {
expected(
"to be have a value for key: '$expectedKey'",
expected = expectedValue,
actual = actualValue
)
} else {
expected(
"to be: $expectedKey=$expectedValue but was: $expectedKey=$actualValue",
expected = expectedValue,
actual = actualValue
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package dev.stalla.builder.validating
import assertk.all
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.prop
import dev.stalla.builder.GeographicLocationBuilder
import dev.stalla.hasParameterWithValue
import dev.stalla.model.aPodcastindexGeographicLocation
import dev.stalla.model.podcastindex.GeographicLocation
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -43,6 +45,64 @@ internal class ValidatingGeographicLocationBuilderTest {
}
}

@Test
internal fun `should recognize a valid GeographicLocation CRS parameter when adding it as a normal parameter`() {
val geoBuilder = ValidatingGeographicLocationBuilder()
.latitude(1.0)
.longitude(2.0)
.addParameter("crs", "TEST")

assertAll {
assertThat(geoBuilder).prop(GeographicLocationBuilder::hasEnoughDataToBuild).isTrue()

assertThat(geoBuilder.build()).isNotNull().all {
prop(GeographicLocation::latitude).isEqualTo(1.0)
prop(GeographicLocation::longitude).isEqualTo(2.0)
prop(GeographicLocation::crs).isEqualTo("TEST")
prop(GeographicLocation::parameters).hasSize(0)
}
}
}

@Test
internal fun `should recognize a valid GeographicLocation uncertainty parameter when adding it as a normal parameter`() {
val geoBuilder = ValidatingGeographicLocationBuilder()
.latitude(1.0)
.longitude(2.0)
.addParameter("u", "4.12")

assertAll {
assertThat(geoBuilder).prop(GeographicLocationBuilder::hasEnoughDataToBuild).isTrue()

assertThat(geoBuilder.build()).isNotNull().all {
prop(GeographicLocation::latitude).isEqualTo(1.0)
prop(GeographicLocation::longitude).isEqualTo(2.0)
prop(GeographicLocation::uncertainty).isEqualTo(4.12)
prop(GeographicLocation::parameters).hasSize(0)
}
}
}

@Test
internal fun `should treat an invalid GeographicLocation uncertainty parameter as a normal parameter when adding it as a normal parameter`() {
val geoBuilder = ValidatingGeographicLocationBuilder()
.latitude(1.0)
.longitude(2.0)
.addParameter("u", "abc")

assertAll {
assertThat(geoBuilder).prop(GeographicLocationBuilder::hasEnoughDataToBuild).isTrue()

assertThat(geoBuilder.build()).isNotNull().all {
prop(GeographicLocation::latitude).isEqualTo(1.0)
prop(GeographicLocation::longitude).isEqualTo(2.0)
prop(GeographicLocation::uncertainty).isNull()
prop(GeographicLocation::parameters).hasSize(1)
hasParameterWithValue("u", "abc")
}
}
}

@Test
internal fun `should populate a GeographicLocation builder with all properties from an GeographicLocation model`() {
val geo = aPodcastindexGeographicLocation()
Expand Down
14 changes: 7 additions & 7 deletions src/test/kotlin/dev/stalla/model/MediaTypeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import assertk.assertions.prop
import dev.stalla.arguments
import dev.stalla.doesNotMatchSymmetrically
import dev.stalla.equalToString
import dev.stalla.hasParameterWithValue
import dev.stalla.matchPattern
import dev.stalla.matchesSymmetrically
import dev.stalla.model.podcastindex.TranscriptType
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ArgumentsProvider
Expand Down Expand Up @@ -113,7 +113,8 @@ internal class MediaTypeTest {
prop(MediaType::type).isEqualTo("text")
prop(MediaType::subtype).isEqualTo("plain")
prop(MediaType::parameters).hasSize(1)
prop("parameter") { MediaType::parameter.call(it, "charset") }.isNotNull().isEqualTo("utf-8")
hasParameterWithValue("charset", "utf-8")
// prop("parameter") { MediaType::parameter.call(it, "charset") }.isNotNull().isEqualTo("utf-8")
}
}

Expand Down Expand Up @@ -218,7 +219,7 @@ internal class MediaTypeTest {
prop(MediaType::type).isEqualTo("audio")
prop(MediaType::subtype).isEqualTo("*")
prop(MediaType::parameters).hasSize(1)
prop("parameter") { MediaType::parameter.call(it, "attr") }.isNotNull().isEqualTo("v>alue")
hasParameterWithValue("attr", "v>alue")
}
}

Expand Down Expand Up @@ -310,7 +311,7 @@ internal class MediaTypeTest {
prop(MediaType::type).isEqualTo("multipart")
prop(MediaType::subtype).isEqualTo("mixed")
prop(MediaType::parameters).hasSize(1)
prop("parameter") { MediaType::parameter.call(it, "boundary") }.isNotNull().isEqualTo("gc0pJq0M:08jU534c0p")
hasParameterWithValue("boundary", "gc0pJq0M:08jU534c0p")
}
}

Expand Down Expand Up @@ -359,7 +360,7 @@ internal class MediaTypeTest {
@Test
fun `should render parameters correctly`() {
assertThat(MediaType.PLAIN_TEXT.withParameter("p1", "v1")).isNotNull()
.prop("toString") { MediaType::toString.call(it) }.isEqualTo("text/plain; p1=v1")
.equalToString("text/plain; p1=v1")
}

@Test
Expand Down Expand Up @@ -413,8 +414,7 @@ internal class MediaTypeTest {
@ParameterizedTest
@ArgumentsSource(MediaTypeNameProvider::class)
fun `should retrieve all predefined types from the factory method`(typeName: String) {
assertThat(MediaType.of(typeName)).isNotNull()
.prop("toString") { TranscriptType::toString.call(it) }.isEqualTo(typeName)
assertThat(MediaType.of(typeName)).isNotNull().equalToString(typeName)
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.prop
import dev.stalla.hasParameterWithValue
import org.junit.jupiter.api.Test

class GeographicLocationTest {
Expand Down Expand Up @@ -60,7 +61,7 @@ class GeographicLocationTest {
prop(GeographicLocation::crs).isEqualTo("wgs84")
prop(GeographicLocation::uncertainty).isEqualTo(12.0)
prop(GeographicLocation::parameters).hasSize(1)
prop("parameter") { GeographicLocation::parameter.call(it, "param") }.isNotNull().isEqualTo("value")
hasParameterWithValue("param", "value")
}
}

Expand Down Expand Up @@ -109,7 +110,7 @@ class GeographicLocationTest {
prop(GeographicLocation::crs).isNull()
prop(GeographicLocation::uncertainty).isNull()
prop(GeographicLocation::parameters).hasSize(1)
prop("parameter") { GeographicLocation::parameter.call(it, "u") }.isNotNull().isEqualTo("invalid")
hasParameterWithValue("u", "invalid")
}
}

Expand Down Expand Up @@ -142,7 +143,7 @@ class GeographicLocationTest {
prop(GeographicLocation::crs).isNull()
prop(GeographicLocation::uncertainty).isNull()
prop(GeographicLocation::parameters).hasSize(1)
prop("parameter") { GeographicLocation::parameter.call(it, "param") }.isNotNull().isEqualTo("with = special & chars")
hasParameterWithValue("param", "with = special & chars")
}
}

Expand All @@ -155,8 +156,8 @@ class GeographicLocationTest {
prop(GeographicLocation::crs).isEqualTo("theCrs")
prop(GeographicLocation::uncertainty).isEqualTo(12.0)
prop(GeographicLocation::parameters).hasSize(2)
prop("parameter") { GeographicLocation::parameter.call(it, "param") }.isNotNull().isEqualTo("value")
prop("parameter") { GeographicLocation::parameter.call(it, "param2") }.isNotNull().isEqualTo("value2")
hasParameterWithValue("param", "value")
hasParameterWithValue("param2", "value2")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.prop
import dev.stalla.arguments
import dev.stalla.equalToString
import dev.stalla.model.MediaType
import dev.stalla.model.TranscriptTypeNameProvider
import dev.stalla.model.allTranscriptTypeNames
Expand Down Expand Up @@ -40,8 +40,7 @@ internal class TranscriptTypeTest {
@ParameterizedTest
@ArgumentsSource(TranscriptTypeNameProvider::class)
fun `should retrieve all Transcript Types from the factory method`(typeName: String) {
assertThat(TranscriptType.of(typeName)).isNotNull()
.prop("toString") { TranscriptType::toString.call(it) }.isEqualTo(typeName)
assertThat(TranscriptType.of(typeName)).isNotNull().equalToString(typeName)
}

@ParameterizedTest
Expand Down Expand Up @@ -77,8 +76,7 @@ internal class TranscriptTypeTest {

@Test
fun `should be case insensitive in the Podcastindex transcript type factory method`() {
assertThat(TranscriptType.of("TEXT/PLAIN")).isNotNull()
.prop("toString") { TranscriptType::toString.call(it) }.isEqualTo("text/plain")
assertThat(TranscriptType.of("TEXT/PLAIN")).isNotNull().equalToString("text/plain")
}

@Test
Expand Down

0 comments on commit d2135d8

Please sign in to comment.