From 1029dca6f6c8793c53c94624b4252ea0894d452c Mon Sep 17 00:00:00 2001 From: robbertvdzon Date: Sun, 8 May 2022 08:46:44 +0200 Subject: [PATCH] adding first set of tests: refactoring --- .../com/gildedrose/GildedRoseAgedBrieTest.kt | 46 ++++--------------- .../com/gildedrose/GildedRoseBackstageTest.kt | 41 ++++------------- .../com/gildedrose/GildedRoseBaseTest.kt | 38 ++++++++++++++- .../com/gildedrose/GildedRoseDefaultTest.kt | 46 ++++--------------- .../com/gildedrose/GildedRoseSulfurasTest.kt | 36 ++++----------- 5 files changed, 71 insertions(+), 136 deletions(-) diff --git a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseAgedBrieTest.kt b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseAgedBrieTest.kt index aa70850f..0324f6a7 100644 --- a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseAgedBrieTest.kt +++ b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseAgedBrieTest.kt @@ -1,15 +1,19 @@ package com.gildedrose import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments -import org.junit.jupiter.params.provider.MethodSource -import java.util.stream.Stream @TestInstance(TestInstance.Lifecycle.PER_CLASS) internal class GildedRoseAgedBrieTest : GildedRoseBaseTest() { - private val combinationsToTest = arrayOf( + // Below are all testcases for this test, with the following arguments: + // - name + // - initialSellIn + // - initialQuality + // - numberDays + // - resultingSellIn + // - resultingQuality + override val combinationsToTest = arrayOf( // tests where sellIn and quality are initially the same Arguments.of("Aged Brie", 5, 5, 1, 4, 6), Arguments.of("Aged Brie", 5, 5, 2, 3, 7), @@ -69,40 +73,6 @@ internal class GildedRoseAgedBrieTest : GildedRoseBaseTest() { Arguments.of("Aged Brie", -1, -2, 3, -4, 4), Arguments.of("Aged Brie", 100, -1, 1, 99, 0), Arguments.of("Aged Brie", 100, 100, 1, 99, 100), - Arguments.of("Aged Brie", 1, 0, 16, -15, 31), - ) - - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Legacy algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Legacy algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Refactored algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Refactored algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - - fun combinationsSource(): Stream = Stream.of(*combinationsToTest) - - } \ No newline at end of file diff --git a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBackstageTest.kt b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBackstageTest.kt index 75a415ed..47c590f9 100644 --- a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBackstageTest.kt +++ b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBackstageTest.kt @@ -1,16 +1,19 @@ package com.gildedrose import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments -import org.junit.jupiter.params.provider.MethodSource -import java.util.stream.Stream @TestInstance(TestInstance.Lifecycle.PER_CLASS) internal class GildedRoseBackstageTest : GildedRoseBaseTest(){ - - private val combinationsToTest = arrayOf( + // Below are all testcases for this test, with the following arguments: + // - name + // - initialSellIn + // - initialQuality + // - numberDays + // - resultingSellIn + // - resultingQuality + override val combinationsToTest = arrayOf( // tests where sellIn is initially less then the quality Arguments.of("Backstage passes to a TAFKAL80ETC concert", 12, 25, 1, 11, 26), Arguments.of("Backstage passes to a TAFKAL80ETC concert", 12, 25, 2, 10, 27),// 10 days before concert, quality goes up by 2 each day @@ -55,32 +58,4 @@ internal class GildedRoseBackstageTest : GildedRoseBaseTest(){ ) - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Legacy algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Legacy algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Refactored algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Refactored algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - fun combinationsSource(): Stream = Stream.of(*combinationsToTest) } \ No newline at end of file diff --git a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBaseTest.kt b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBaseTest.kt index 49e3c842..369bb993 100644 --- a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBaseTest.kt +++ b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseBaseTest.kt @@ -1,8 +1,41 @@ package com.gildedrose import org.assertj.core.api.Assertions +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource +import java.util.stream.Stream + +abstract class GildedRoseBaseTest { + + abstract val combinationsToTest : Array // this array is created in all subclass tests + + @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Legacy algorithm") + @MethodSource("combinationsSource") + fun `given input item, when some days are passed, then the item state is correctly modified, using Legacy algorithm`( + name: String, + initialSellIn: Int, + initialQuality: Int, + numberDays: Int, + resultingSellIn: Int, + resultingQuality: Int + ) { + testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) + } + + @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Refactored algorithm") + @MethodSource("combinationsSource") + fun `given input item, when some days are passed, then the item state is correctly modified, using Refactored algorithm`( + name: String, + initialSellIn: Int, + initialQuality: Int, + numberDays: Int, + resultingSellIn: Int, + resultingQuality: Int + ) { + testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) + } -open class GildedRoseBaseTest { fun testGildedRose( name: String, initialSellIn: Int, @@ -23,4 +56,7 @@ open class GildedRoseBaseTest { Assertions.assertThat(item.quality).isEqualTo(resultingQuality) } + fun combinationsSource(): Stream = Stream.of(*combinationsToTest) + + } \ No newline at end of file diff --git a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseDefaultTest.kt b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseDefaultTest.kt index d0651c9e..384b52a2 100644 --- a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseDefaultTest.kt +++ b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseDefaultTest.kt @@ -1,15 +1,19 @@ package com.gildedrose import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments -import org.junit.jupiter.params.provider.MethodSource -import java.util.stream.Stream @TestInstance(TestInstance.Lifecycle.PER_CLASS) -internal class GildedRoseDefaultTest : GildedRoseBaseTest(){ +internal class GildedRoseDefaultTest : GildedRoseBaseTest() { - private val combinationsToTest = arrayOf( + // Below are all testcases for this test, with the following arguments: + // - name + // - initialSellIn + // - initialQuality + // - numberDays + // - resultingSellIn + // - resultingQuality + override val combinationsToTest: Array = arrayOf( // tests where sellIn and quality are initially the same Arguments.of("foo", 5, 5, 1, 4, 4), Arguments.of("foo", 5, 5, 2, 3, 3), @@ -49,36 +53,6 @@ internal class GildedRoseDefaultTest : GildedRoseBaseTest(){ Arguments.of("foo", -1, -2, 3, -4, -2), Arguments.of("foo", 100, -1, 1, 99, -1), Arguments.of("foo", 100, 100, 1, 99, 99), + ) - ) - - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Legacy algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Legacy algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Refactored algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Refactored algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - - fun combinationsSource(): Stream = Stream.of(*combinationsToTest) } \ No newline at end of file diff --git a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseSulfurasTest.kt b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseSulfurasTest.kt index a01fe9c8..718faf3e 100644 --- a/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseSulfurasTest.kt +++ b/Kotlin/src/test/kotlin/com/gildedrose/GildedRoseSulfurasTest.kt @@ -9,7 +9,14 @@ import java.util.stream.Stream @TestInstance(TestInstance.Lifecycle.PER_CLASS) internal class GildedRoseSulfurasTest : GildedRoseBaseTest(){ - private val combinationsToTest = arrayOf( + // Below are all testcases for this test, with the following arguments: + // - name + // - initialSellIn + // - initialQuality + // - numberDays + // - resultingSellIn + // - resultingQuality + override val combinationsToTest = arrayOf( Arguments.of("Sulfuras, Hand of Ragnaros", 5, 5, 1, 5, 5),// sellIn and quality always stays unchanged Arguments.of("Sulfuras, Hand of Ragnaros", 5, 5, 2, 5, 5),// sellIn and quality always stays unchanged Arguments.of("Sulfuras, Hand of Ragnaros", 5, 5, 100, 5, 5),// sellIn and quality always stays unchanged @@ -19,31 +26,4 @@ internal class GildedRoseSulfurasTest : GildedRoseBaseTest(){ Arguments.of("Sulfuras, Hand of Ragnaros", 100, 100, 1, 100, 100),// sellIn and quality always stays unchanged ) - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Legacy algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Legacy algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - @ParameterizedTest(name = "{0}: initial sellIn:{1} and initial quality:{2}, after {3} days: sellIn:{4} and quality {5}, using Refactored algorithm") - @MethodSource("combinationsSource") - fun `given input item, when some days are passed, then the item state is correctly modified, using Refactored algorithm`( - name: String, - initialSellIn: Int, - initialQuality: Int, - numberDays: Int, - resultingSellIn: Int, - resultingQuality: Int - ) { - testGildedRose(name, initialSellIn, initialQuality, numberDays, resultingSellIn, resultingQuality) - } - - fun combinationsSource(): Stream = Stream.of(*combinationsToTest) } \ No newline at end of file