From 3ca6d1313230581a07e609ff54cce9694c93ae98 Mon Sep 17 00:00:00 2001 From: Julia Glaszka <85160468+jglaszka@users.noreply.github.com> Date: Fri, 20 Oct 2023 16:20:41 +0200 Subject: [PATCH] example of changed tests (#1) * poc refactor to handle removed values and added * remove unused check from tests * fix tests * move converters into field * change types * fix types --- .../languages/AbstractValuesTest.groovy | 58 +++++++++++-------- .../english/EnglishValuesTest.groovy | 17 ++++-- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/AbstractValuesTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/AbstractValuesTest.groovy index 016ab677..e0a4d1bb 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/AbstractValuesTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/AbstractValuesTest.groovy @@ -1,36 +1,36 @@ package pl.allegro.finance.tradukisto.internal.languages -import pl.allegro.finance.tradukisto.internal.Container import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter import pl.allegro.finance.tradukisto.internal.LongToStringConverter import spock.lang.Specification import spock.lang.Unroll abstract class AbstractValuesTest extends Specification { - - abstract ValuesTestInput input + abstract ValuesTestData testData + abstract IntegerToStringConverter intConverter + abstract LongToStringConverter longConverter @Unroll - def "should convert #value to '#words'"() { + def "should convert integer #input to #output"() { expect: - getInput().getIntConverter().asWords(value) == words + intConverter.asWords(input) == output where: - value << intNumbers - words << getInput().intWords + input << getTestData().getIntWords().keySet() + output << getTestData().getIntWords().values() } @Unroll - def "should convert long #value to '#words'"() { + def "should convert long #input to #output"() { expect: - getInput().getLongConverter().asWords(value) == words + longConverter.asWords(input) == output where: - value << longNumbers - words << getInput().longWords + input << getTestData().getLongWords().keySet() + output << getTestData().getLongWords().values() } - private static intNumbers = [ + private static requiredIntNumbers = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 20, 30, 40, 50, 60, 70, 80, 90, @@ -44,7 +44,7 @@ abstract class AbstractValuesTest extends Specification { 1_000_000_000, 2_147_483_647, ] - private static longNumbers = [ + private static requiredLongNumbers = [ 5_000_000_000, 1_000_000_000_000, 2_000_000_000_000, @@ -57,17 +57,29 @@ abstract class AbstractValuesTest extends Specification { Long.MAX_VALUE ] - static class ValuesTestInput { - IntegerToStringConverter intConverter - LongToStringConverter longConverter - Collection intWords - Collection longWords + static class ValuesTestData { + Map intWords + Map longWords + + ValuesTestData(Map intWords, Map longWords) { + this.intWords = prepareIntegerInput(intWords) // fills dataset with required data if was not specified, allows adding new entries + this.longWords = prepareLongInput(longWords) + } + + private static prepareIntegerInput(Map intWords) { + requiredIntNumbers.stream() + .forEach { + intWords.putIfAbsent(it, "⚠️Please specify expected output") // todo: how to handle if someone removes required input? throw exception or push fake data as here? + } + return intWords.sort{ it.key } + } - ValuesTestInput(Container container, Collection intWords, Collection longWords) { - this.intConverter = container.getIntegerConverter() - this.longConverter = container.getLongConverter() - this.intWords = intWords - this.longWords = longWords + private static prepareLongInput(Map longWords) { + requiredLongNumbers.stream() + .forEach { + longWords.putIfAbsent(it, "⚠️Please specify expected output") // todo: how to handle if someone removes required input? throw exception or push fake data as here? + } + return longWords.sort{ it.key } } } } diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/english/EnglishValuesTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/english/EnglishValuesTest.groovy index 535ea90e..4fe42e11 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/english/EnglishValuesTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/english/EnglishValuesTest.groovy @@ -6,12 +6,16 @@ import static pl.allegro.finance.tradukisto.internal.Container.englishContainer class EnglishValuesTest extends AbstractValuesTest { + def setup() { + intConverter = englishContainer().integerConverter + longConverter = englishContainer().longConverter + } + @Override - ValuesTestInput getInput() { - return new ValuesTestInput( - englishContainer(), - intWords.values(), - longWords.values() + ValuesTestData getTestData() { + testData = new ValuesTestData( + intWords, + longWords ) } @@ -21,7 +25,7 @@ class EnglishValuesTest extends AbstractValuesTest { 2 : "two", 3 : "three", 4 : "four", - 5 : "five", + 5 : "five", // if you comment it - this one test will fail 6 : "six", 7 : "seven", 8 : "eight", @@ -81,6 +85,7 @@ class EnglishValuesTest extends AbstractValuesTest { 3_000 : "three thousand", 4_000 : "four thousand", 5_000 : "five thousand", + 2_137 : "two thousand one hundred thirty-seven", // not required, but added to extend test data, passes 7_634 : "seven thousand six hundred thirty-four", 11_000 : "eleven thousand", 15_000 : "fifteen thousand",