Skip to content

Commit

Permalink
improve multiplatform build
Browse files Browse the repository at this point in the history
move some tests to commonTest
use kotlin-test annotations everywhere instead of junit
get rid of test task override (breaks jvm build :-) )
rename remaining jvm tests to have Jvm in their file name (avoid clash with commonTest)
  • Loading branch information
jillesvangurp committed Aug 1, 2020
1 parent 442e611 commit c475f29
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 96 deletions.
116 changes: 56 additions & 60 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,93 +20,89 @@ val slf4jVersion = "1.7.26"
val junitVersion = "5.6.2"

kotlin {
jvm {
val main by compilations.getting {
kotlinOptions {
// Setup the Kotlin compiler options for the 'main' compilation:
jvmTarget = "1.8"
}
}
val test by compilations.getting {
kotlinOptions {
// Setup the Kotlin compiler options for the 'main' compilation:
jvmTarget = "1.8"
}
}
}
js {
browser {
}
}

sourceSets {

val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
dependencies {
implementation(kotlin("stdlib-common"))
}
}
}

js() {
// this.mavenPublication {
// groupId = artifactGroup
// artifactId = project.name
// }
val main by compilations.getting {
val commonTest by getting {
dependencies {
implementation(kotlin("stdlib-js"))
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
// yay kotest does multiplatform
implementation("io.kotest:kotest-assertions-core:4.1.3")
}
}
nodejs {

val jvmMain by getting {

dependencies {
implementation(kotlin("stdlib-jdk8"))
}
}
// JVM-specific tests and their dependencies:
jvm() {
val main by compilations.getting {
this.kotlinOptions {
jvmTarget = "1.8"
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
val jvmTest by getting {
dependencies {
runtimeOnly("org.junit.jupiter:junit-jupiter:$junitVersion")
implementation(kotlin("test-junit"))

implementation("org.hamcrest:hamcrest-all:1.3")

// kotlintest runner needs this to enable logging
implementation("org.slf4j:slf4j-api:$slf4jVersion")
implementation("org.slf4j:jcl-over-slf4j:$slf4jVersion")
implementation("org.slf4j:log4j-over-slf4j:$slf4jVersion")
implementation("org.slf4j:jul-to-slf4j:$slf4jVersion")
implementation("ch.qos.logback:logback-classic:1.2.3")

implementation("com.google.code.gson:gson:2.8.6")
}
val test by compilations.getting {
this.kotlinOptions {
jvmTarget = "1.8"
}
}

val jsMain by getting {
dependencies {
implementation("org.junit.jupiter:junit-jupiter:$junitVersion")
implementation("io.kotest:kotest-assertions-core-jvm:4.1.1")
implementation("org.hamcrest:hamcrest-all:1.3")

// kotlintest runner needs this to enable logging
implementation("org.slf4j:slf4j-api:$slf4jVersion")
implementation("org.slf4j:jcl-over-slf4j:$slf4jVersion")
implementation("org.slf4j:log4j-over-slf4j:$slf4jVersion")
implementation("org.slf4j:jul-to-slf4j:$slf4jVersion")
implementation("ch.qos.logback:logback-classic:1.2.3")

implementation("com.google.code.gson:gson:2.8.6")
implementation(kotlin("stdlib-js"))
}
}

val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
}
}

val artifactName = "geogeometry"
val artifactGroup = "com.github.jillesvangurp"

publishing {
repositories {
maven {
name="localrepo"
url = uri("file://$buildDir/$name")
url = uri("file://$buildDir/localRepo")
}
}
// publications {
//
// create<MavenPublication>("lib") {
// groupId = artifactGroup
// artifactId = artifactName
// from(components["jvmMain"])
// }
// }
}

kotlinter {
ignoreFailures = true
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging.exceptionFormat = TestExceptionFormat.FULL
testLogging.events = setOf(
TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT
)
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version=0.0.1-SNAPSHOT
group=com.github.jillesvangurp
54 changes: 54 additions & 0 deletions src/commonTest/kotlin/GeoHashUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import com.jillesvangurp.geo.GeoHashUtils
import com.jillesvangurp.geo.latitude
import com.jillesvangurp.geo.longitude
import io.kotest.data.blocking.forAll
import io.kotest.data.row
import io.kotest.matchers.shouldBe
import kotlin.test.Test

class GeoHashUtilsTest {
val coordinatesWithHashes = arrayOf(
row(0.1, -0.1, "ebpbtdpntc6e"),
row(52.530888, 13.394904, "u33dbfcyegk2")
)

fun lines() = arrayOf(
arrayOf(1, 1, 2, 2),
arrayOf(2, 2, 1, 1),
arrayOf(2, 1, 1, 1),
arrayOf(1, 2, 1, 1),
arrayOf(1, 1, 2, 1),
arrayOf(1, 1, 1, 2),
arrayOf(1, 1, 1, 2)
)

// @Test
// fun shouldBreak() {
// 42 shouldBe 40
// }

@Test
fun shouldDecodeHashes() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
val decoded = GeoHashUtils.decode(geoHash)
decoded.latitude shouldBeApproximately lat
decoded.longitude shouldBeApproximately lon
}
}

@Test
fun shouldEncodeHashes() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
GeoHashUtils.encode(lat, lon) shouldBe geoHash
GeoHashUtils.encode(doubleArrayOf(lon, lat)) shouldBe geoHash
}
}

@Test
fun shouldContainCoordinateInBbox() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
GeoHashUtils.contains(geoHash, lat, lon) shouldBe true
GeoHashUtils.contains(geoHash, lon, lat) shouldBe false
}
}
}
11 changes: 11 additions & 0 deletions src/commonTest/kotlin/helpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import io.kotest.matchers.doubles.shouldBeLessThan
import kotlin.math.abs

fun Double.shouldBeApproximately(other: Double, marginOfError: Double = 0.0000001) {
// allow for tiny rounding errors
abs(this - other) shouldBeLessThan marginOfError
}

infix fun Double.shouldBeApproximately(other: Double) {
this.shouldBeApproximately(other, 0.0000001)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import com.jillesvangurp.geo.GeoGeometry.Companion.toDecimalDegree
import com.jillesvangurp.geo.GeoGeometry.Companion.translate
import com.jillesvangurp.geo.GeoGeometry.Companion.validate
import com.jillesvangurp.geo.GeoHashUtils.Companion.isWest
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.shouldBe
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.Test
import java.util.Random

class GeoGeometryTest {
class GeoGeometryJvmTest {
var sydney = doubleArrayOf(151.206146, -33.872796)
var buenosaires = doubleArrayOf(-58.380449, -34.602875)
var newyork = doubleArrayOf(-74.011237, 40.721119)
Expand Down Expand Up @@ -922,7 +922,7 @@ class GeoGeometryTest {
doubleArrayOf(0.0, 90.000001),
doubleArrayOf(0.0, -90.000001)
).forEach {
assertThrows<IllegalArgumentException> {
shouldThrow<IllegalArgumentException> {
validate(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import io.kotest.matchers.shouldBe
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test
import org.junit.Test
import kotlin.math.abs

class GeoHashUtilsTest {
class GeoHashUtilsJvmTest {
val coordinatesWithHashes = arrayOf(
row(0.1, -0.1, "ebpbtdpntc6e"),
row(52.530888, 13.394904, "u33dbfcyegk2")
Expand All @@ -57,31 +57,6 @@ class GeoHashUtilsTest {
arrayOf(1, 1, 1, 2)
)

@Test
fun `decode hash`() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
val decoded = GeoHashUtils.decode(geoHash)
decoded.latitude shouldBeApproximately lat
decoded.longitude shouldBeApproximately lon
}
}

@Test
fun `encode hash`() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
GeoHashUtils.encode(lat, lon) shouldBe geoHash
GeoHashUtils.encode(doubleArrayOf(lon, lat)) shouldBe geoHash
}
}

@Test
fun `hash bbox should contain coordinate`() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
GeoHashUtils.contains(geoHash, lat, lon) shouldBe true
GeoHashUtils.contains(geoHash, lon, lat) shouldBe false
}
}

@Test
fun `decode bbox`() {
forAll(*coordinatesWithHashes) { lat: Double, lon: Double, geoHash: String ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package com.jillesvangurp.geogeometry

import com.jillesvangurp.geo.FeatureCollection
import com.jillesvangurp.geo.GeoHashUtils
import com.jillesvangurp.geo.MultiPolygonCoordinates
import com.jillesvangurp.geo.MultiPolygonGeometry
import com.jillesvangurp.geo.PolygonGeometry
import org.junit.jupiter.api.Test
import org.junit.Test

class GeoJsonTest {
class GeoJsonJvmTest {
@Test
fun `cover berlin with hashes`() {
val berlinJson = this.javaClass.classLoader.getResource("berlin.geojson").readText()
val berlin = gson.fromJson(berlinJson, MultiPolygonGeometry::class.java)

val hashes = GeoHashUtils.geoHashesForPolygon(berlin.coordinates ?: throw IllegalArgumentException("coordinates missing"))
val hashes = GeoHashUtils.geoHashesForPolygon(
berlin.coordinates ?: throw IllegalArgumentException("coordinates missing")
)
val hashesCollection = FeatureCollection.fromGeoHashes(hashes)
println(gson.toJson(hashesCollection + FeatureCollection(listOf(berlin.asFeature()))))
}
Expand Down

0 comments on commit c475f29

Please sign in to comment.