From 2e851f51b1d1ecf7e215df95866e2821ff317e2f Mon Sep 17 00:00:00 2001 From: "anastasia.birillo" Date: Wed, 16 Oct 2024 20:00:25 +0200 Subject: [PATCH] Add tests for smoothie cooking task --- .../culinaryServerCookSmoothie/task-info.yaml | 3 ++ .../test/SmoothieFunctions.kt | 31 ++++++++++++ .../culinaryServerCookSmoothie/test/Tests.kt | 49 +++++++++++++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 culinaryServer/culinaryServerCookSmoothie/test/SmoothieFunctions.kt diff --git a/culinaryServer/culinaryServerCookSmoothie/task-info.yaml b/culinaryServer/culinaryServerCookSmoothie/task-info.yaml index 13cc488..630ddac 100644 --- a/culinaryServer/culinaryServerCookSmoothie/task-info.yaml +++ b/culinaryServer/culinaryServerCookSmoothie/task-info.yaml @@ -71,3 +71,6 @@ files: - name: test/TomatoSoupFunctions.kt visible: false propagatable: false + - name: test/SmoothieFunctions.kt + visible: false + propagatable: false diff --git a/culinaryServer/culinaryServerCookSmoothie/test/SmoothieFunctions.kt b/culinaryServer/culinaryServerCookSmoothie/test/SmoothieFunctions.kt new file mode 100644 index 0000000..445b6dc --- /dev/null +++ b/culinaryServer/culinaryServerCookSmoothie/test/SmoothieFunctions.kt @@ -0,0 +1,31 @@ +import org.jetbrains.academy.test.system.core.models.TestKotlinType +import org.jetbrains.academy.test.system.core.models.classes.TestClass +import org.jetbrains.academy.test.system.core.models.method.TestMethod +import org.jetbrains.academy.test.system.core.models.variable.TestVariable + +internal val getFruitsForSmoothieMethod = TestMethod( + "getFruitsForSmoothie", + TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.Fruit")), +) + +internal val cookSmoothieMethod = TestMethod( + "cookSmoothie", + TestKotlinType("Unit"), + arguments = listOf( + TestVariable( + name = "fruit", + javaType = "List", + kotlinType = TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.Fruit")), + ), + ), + returnTypeJava = "void", +) + +internal val smoothieKtTestClass = TestClass( + "SmoothieKt", + "org.jetbrains.kotlin.course.culinary.game.recipes", + customMethods = listOf( + getFruitsForSmoothieMethod, + cookSmoothieMethod + ), +) diff --git a/culinaryServer/culinaryServerCookSmoothie/test/Tests.kt b/culinaryServer/culinaryServerCookSmoothie/test/Tests.kt index 87aadbb..6c6a826 100644 --- a/culinaryServer/culinaryServerCookSmoothie/test/Tests.kt +++ b/culinaryServer/culinaryServerCookSmoothie/test/Tests.kt @@ -3,6 +3,8 @@ import org.jetbrains.academy.test.system.core.invokeWithoutArgs import org.jetbrains.kotlin.course.culinary.converters.buildAction import org.jetbrains.kotlin.course.culinary.game.actions import org.jetbrains.kotlin.course.culinary.game.clearActions +import org.jetbrains.kotlin.course.culinary.game.fridge +import org.jetbrains.kotlin.course.culinary.game.kitchen import org.jetbrains.kotlin.course.culinary.game.recipes.NUMBER_OF_TOMATOES import org.jetbrains.kotlin.course.culinary.game.recipes.NUM_VEGETABLES_FOR_SALAD import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl @@ -11,14 +13,53 @@ import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl.RA import org.jetbrains.kotlin.course.culinary.models.ItemType import org.jetbrains.kotlin.course.culinary.models.action.Action import org.jetbrains.kotlin.course.culinary.models.action.ActionType -import org.jetbrains.kotlin.course.culinary.models.food.CutVegetable -import org.jetbrains.kotlin.course.culinary.models.food.SpiceType -import org.jetbrains.kotlin.course.culinary.models.food.Vegetable -import org.jetbrains.kotlin.course.culinary.models.food.VegetableType +import org.jetbrains.kotlin.course.culinary.models.food.* import org.junit.jupiter.api.Test import java.lang.reflect.InvocationTargetException class Test { + @Test + fun getFruitsForSmoothieMethodTest() { + clearActions() + val fruits = getFruitsForSmoothie() + + assert(fruits.toSet().size == 2) { "The ${getFruitsForSmoothieMethod.name} method should return a lit of Citrus and Berry fruits only" } + val expectedActions = buildList{ + add(Action(ActionType.SHOW_ON_COUNTER, ItemType.BERRY_BASKET)) + add(Action(ActionType.SHOW_ON_COUNTER, ItemType.CITRUS_BASKET)) + } + assert(expectedActions == actions) { "The ${getFruitsForSmoothieMethod.name} method should return a lit of fruits for smoothie, sorted by sugarContent amount" } + } + + private fun getFruitsForSmoothie(): List { + val clazz = smoothieKtTestClass.checkBaseDefinition() + val method = clazz.declaredMethods.findMethod(getFruitsForSmoothieMethod) + + return try { + method.invokeWithoutArgs(clazz = clazz) as List + } catch(e: InvocationTargetException) { + assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" } + emptyList() + } + } + + @Test + fun cookSmoothieMethodTest() { + val fruits = getFruitsForSmoothie() + + clearActions() + val clazz = smoothieKtTestClass.checkBaseDefinition() + val method = clazz.declaredMethods.findMethod(cookSmoothieMethod) + try { + method(clazz, fruits) + } catch(e: InvocationTargetException) { + assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" } + } + + val expectedActions = fruits.map{ buildAction(ActionType.ADD_TO_BLENDER, it) } + assert(expectedActions + Action(ActionType.BLEND) == actions) { "The ${method.name} method should add to the blender all fruits and then blend them" } + } + @Test fun getFreshVegetablesMethodTest() { val randomVegetables = fillFridge()