From dcfd9f11f15c97649ee4b9462628c73b07c6c594 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Mar 2021 15:29:32 +0100 Subject: [PATCH] Add unit tests for shapes Signed-off-by: trivernis --- .github/workflows/gradle-test.yml | 36 +++++++++ build.gradle | 8 +- .../net/trivernis/chunkmaster/Chunkmaster.kt | 3 +- .../chunkmaster/lib/shapes/CircleTest.kt | 73 +++++++++++++++++++ .../chunkmaster/lib/shapes/SpiralTest.kt | 62 ++++++++++++++++ 5 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/gradle-test.yml create mode 100644 src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/CircleTest.kt create mode 100644 src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/SpiralTest.kt diff --git a/.github/workflows/gradle-test.yml b/.github/workflows/gradle-test.yml new file mode 100644 index 0000000..4f5651b --- /dev/null +++ b/.github/workflows/gradle-test.yml @@ -0,0 +1,36 @@ +name: Gradle Unit Tests +on: + workflow_dispatch: + push: + branches: [ main, develop, actions, feature/unit-tests ] + pull_request: + branches: [ main, develop, actions, feature/unit-tests ] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup Java + uses: actions/setup-java@v1 + with: + java-version: 1.8 + + - name: Cache build data + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('build.gradle') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Test + run: chmod +x gradlew && ./gradlew test + + - name: Cleanup Gradle Cache + run: | + rm -f ~/.gradle/caches/modules-2/modules-2.lock + rm -f ~/.gradle/caches/modules-2/gc.properties \ No newline at end of file diff --git a/build.gradle b/build.gradle index f4ef21f..a6d7dd3 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,10 @@ repositories { name 'mikeprimm' url 'https://repo.mikeprimm.com' } + + maven { + url 'https://jitpack.io' + } } dependencies { @@ -90,6 +94,8 @@ compileTestKotlin { jvmTarget = "1.8" } dependencies { - compileOnly group: 'junit', name: 'junit', version: '4.12' + implementation group: 'junit', name: 'junit', version: '4.12' + implementation 'com.github.seeseemelk:MockBukkit:v1.16-SNAPSHOT' + implementation 'io.kotest:kotest-runner-junit5:4.3.2' } } \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt index 442e549..65ceecf 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt @@ -10,7 +10,6 @@ import org.bukkit.plugin.java.JavaPlugin import org.bukkit.scheduler.BukkitTask import org.dynmap.DynmapAPI import java.lang.IllegalStateException -import java.lang.NullPointerException class Chunkmaster : JavaPlugin() { lateinit var sqliteManager: SqliteManager @@ -32,7 +31,7 @@ class Chunkmaster : JavaPlugin() { logger.finest("LogLevel: FINEST") configure() - val metrics = Metrics(this) + Metrics(this) langManager = LanguageManager(this) langManager.loadProperties() diff --git a/src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/CircleTest.kt b/src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/CircleTest.kt new file mode 100644 index 0000000..bd7726c --- /dev/null +++ b/src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/CircleTest.kt @@ -0,0 +1,73 @@ +package net.trivernis.chunkmaster.lib.shapes + +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.collections.shouldContainAll +import io.kotest.matchers.doubles.shouldBeBetween +import io.kotest.matchers.shouldBe +import org.junit.Test +import org.junit.jupiter.api.BeforeEach + +class CircleTest { + private val circle = Circle(center = Pair(0, 0), radius = 2, start = Pair(0, 0)) + + @BeforeEach + fun init() { + circle.reset() + } + + @Test + fun `it generates coordinates`() { + circle.next().shouldBe(Pair(0, 0)) + circle.next().shouldBe(Pair(-1, -1)) + circle.next().shouldBe(Pair(1, 0)) + circle.next().shouldBe(Pair(-1, 0)) + circle.next().shouldBe(Pair(1, -1)) + circle.next().shouldBe(Pair(-1, 1)) + circle.next().shouldBe(Pair(0, 1)) + circle.next().shouldBe(Pair(0, -1)) + circle.next().shouldBe(Pair(1, 1)) + } + + @Test + fun `it reports when reaching the end`() { + for (i in 1..25) { + circle.next() + } + circle.endReached().shouldBeTrue() + } + + @Test + fun `it reports the radius`() { + for (i in 1..9) { + circle.next() + } + circle.currentRadius().shouldBe(1) + } + + @Test + fun `it returns the right edges`() { + circle.getShapeEdgeLocations().shouldContainAll(listOf( + Pair(2, -1), + Pair(2, 0), + Pair(2, 1), + Pair(1, 2), + Pair(0, 2), + Pair(-1, 2), + Pair(-2, 1), + Pair(-2, 0), + Pair(-2, -1), + Pair(-1, -2), + Pair(0, -2), + Pair(1, -2), + )) + } + + @Test + fun `it returns the progress`() { + circle.progress(2).shouldBe(0) + for (i in 1..7) { + circle.next() + } + circle.progress(2).shouldBeBetween(.5, .8, .0) + } +} \ No newline at end of file diff --git a/src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/SpiralTest.kt b/src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/SpiralTest.kt new file mode 100644 index 0000000..f18c978 --- /dev/null +++ b/src/test/kotlin/net/trivernis/chunkmaster/lib/shapes/SpiralTest.kt @@ -0,0 +1,62 @@ +package net.trivernis.chunkmaster.lib.shapes + +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.collections.shouldContainAll +import io.kotest.matchers.shouldBe +import org.junit.Test +import org.junit.jupiter.api.BeforeEach + +class SpiralTest { + + private val spiral = Spiral(center = Pair(0, 0), radius = 2, start = Pair(0, 0)) + + @BeforeEach + fun init() { + spiral.reset() + } + + @Test + fun `it generates coordinates`() { + spiral.next().shouldBe(Pair(0, 0)) + spiral.next().shouldBe(Pair(0, 1)) + spiral.next().shouldBe(Pair(1, 1)) + spiral.next().shouldBe(Pair(1, 0)) + spiral.next().shouldBe(Pair(1, -1)) + spiral.next().shouldBe(Pair(0, -1)) + spiral.next().shouldBe(Pair(-1, -1)) + spiral.next().shouldBe(Pair(-1, 0)) + spiral.next().shouldBe(Pair(-1, 1)) + spiral.next().shouldBe(Pair(-1, 2)) + spiral.next().shouldBe(Pair(0, 2)) + } + + @Test + fun `it reports when reaching the end`() { + for (i in 1..25) { + spiral.next() + } + spiral.endReached().shouldBeTrue() + } + + @Test + fun `it reports the radius`() { + for (i in 1..9) { + spiral.next() + } + spiral.currentRadius().shouldBe(1) + } + + @Test + fun `it returns the right edges`() { + spiral.getShapeEdgeLocations().shouldContainAll(listOf(Pair(2, 2), Pair(-2, 2), Pair(2, -2), Pair(-2, -2))) + } + + @Test + fun `it returns the progress`() { + spiral.progress(2).shouldBe(0) + for (i in 1..8) { + spiral.next() + } + spiral.progress(2).shouldBe(0.5) + } +} \ No newline at end of file