adding first set of tests: refactoring

This commit is contained in:
robbertvdzon 2022-05-08 08:46:44 +02:00
parent f6aab4941b
commit 1029dca6f6
5 changed files with 71 additions and 136 deletions

View File

@ -1,15 +1,19 @@
package com.gildedrose package com.gildedrose
import org.junit.jupiter.api.TestInstance 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.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class GildedRoseAgedBrieTest : GildedRoseBaseTest() { 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 // tests where sellIn and quality are initially the same
Arguments.of("Aged Brie", 5, 5, 1, 4, 6), Arguments.of("Aged Brie", 5, 5, 1, 4, 6),
Arguments.of("Aged Brie", 5, 5, 2, 3, 7), 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", -1, -2, 3, -4, 4),
Arguments.of("Aged Brie", 100, -1, 1, 99, 0), Arguments.of("Aged Brie", 100, -1, 1, 99, 0),
Arguments.of("Aged Brie", 100, 100, 1, 99, 100), Arguments.of("Aged Brie", 100, 100, 1, 99, 100),
Arguments.of("Aged Brie", 1, 0, 16, -15, 31), 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<Arguments> = Stream.of(*combinationsToTest)
} }

View File

@ -1,16 +1,19 @@
package com.gildedrose package com.gildedrose
import org.junit.jupiter.api.TestInstance 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.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class GildedRoseBackstageTest : GildedRoseBaseTest(){ internal class GildedRoseBackstageTest : GildedRoseBaseTest(){
// Below are all testcases for this test, with the following arguments:
private val combinationsToTest = arrayOf( // - name
// - initialSellIn
// - initialQuality
// - numberDays
// - resultingSellIn
// - resultingQuality
override val combinationsToTest = arrayOf(
// tests where sellIn is initially less then the quality // 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, 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 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<Arguments> = Stream.of(*combinationsToTest)
} }

View File

@ -1,8 +1,41 @@
package com.gildedrose package com.gildedrose
import org.assertj.core.api.Assertions 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<Arguments> // 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( fun testGildedRose(
name: String, name: String,
initialSellIn: Int, initialSellIn: Int,
@ -23,4 +56,7 @@ open class GildedRoseBaseTest {
Assertions.assertThat(item.quality).isEqualTo(resultingQuality) Assertions.assertThat(item.quality).isEqualTo(resultingQuality)
} }
fun combinationsSource(): Stream<Arguments> = Stream.of(*combinationsToTest)
} }

View File

@ -1,15 +1,19 @@
package com.gildedrose package com.gildedrose
import org.junit.jupiter.api.TestInstance 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.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @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<Arguments> = arrayOf(
// tests where sellIn and quality are initially the same // tests where sellIn and quality are initially the same
Arguments.of("foo", 5, 5, 1, 4, 4), Arguments.of("foo", 5, 5, 1, 4, 4),
Arguments.of("foo", 5, 5, 2, 3, 3), 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", -1, -2, 3, -4, -2),
Arguments.of("foo", 100, -1, 1, 99, -1), Arguments.of("foo", 100, -1, 1, 99, -1),
Arguments.of("foo", 100, 100, 1, 99, 99), 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<Arguments> = Stream.of(*combinationsToTest)
} }

View File

@ -9,7 +9,14 @@ import java.util.stream.Stream
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class GildedRoseSulfurasTest : GildedRoseBaseTest(){ 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, 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, 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 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 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<Arguments> = Stream.of(*combinationsToTest)
} }