Skip to content

Commit

Permalink
Add tests for smoothie cooking task
Browse files Browse the repository at this point in the history
  • Loading branch information
nbirillo committed Oct 16, 2024
1 parent bb8efaa commit 2e851f5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
3 changes: 3 additions & 0 deletions culinaryServer/culinaryServerCookSmoothie/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ files:
- name: test/TomatoSoupFunctions.kt
visible: false
propagatable: false
- name: test/SmoothieFunctions.kt
visible: false
propagatable: false
Original file line number Diff line number Diff line change
@@ -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
),
)
49 changes: 45 additions & 4 deletions culinaryServer/culinaryServerCookSmoothie/test/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Fruit> {
val clazz = smoothieKtTestClass.checkBaseDefinition()
val method = clazz.declaredMethods.findMethod(getFruitsForSmoothieMethod)

return try {
method.invokeWithoutArgs(clazz = clazz) as List<Fruit>
} 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()
Expand Down

0 comments on commit 2e851f5

Please sign in to comment.