Skip to content

Commit b7acf6b

Browse files
committed
Fix the course structure and add more tests
1 parent 5bf1ed2 commit b7acf6b

File tree

12 files changed

+362
-11
lines changed

12 files changed

+362
-11
lines changed

course-info.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ content:
1717
- duckShopServer
1818
- oldSchoolServer
1919
- backToTheFutureServer
20-
- FeedbackSurvey
2120
- culinaryServer
21+
- FeedbackSurvey
2222
environment_settings:
2323
jvm_language_level: JDK_17

culinaryServer/culinaryServerAddSpices/test/Tests.kt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,48 @@ import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl.RA
1111
import org.jetbrains.kotlin.course.culinary.models.ItemType
1212
import org.jetbrains.kotlin.course.culinary.models.action.Action
1313
import org.jetbrains.kotlin.course.culinary.models.action.ActionType
14+
import org.jetbrains.kotlin.course.culinary.models.food.SpiceType
1415
import org.jetbrains.kotlin.course.culinary.models.food.Vegetable
1516
import org.jetbrains.kotlin.course.culinary.models.food.VegetableType
1617
import org.junit.jupiter.api.Test
1718
import java.lang.reflect.InvocationTargetException
1819

1920
class Test {
20-
// TODO: add tests for the current task
21+
@Test
22+
fun generateSpicesMethodTest() {
23+
val spices = generateSpices().take(5).toList()
24+
if (spices.isEmpty()) {
25+
assert(false) { "The method ${generateSpicesMethod.name} should generate random spices! Now you always generate an empty sequence!" }
26+
}
27+
assert(spices.toSet().size > 1) { "The method ${generateSpicesMethod.name} should generate random spices! Now you always generate ${spices.first()}" }
28+
}
29+
30+
private fun generateSpices(): Sequence<SpiceType> {
31+
val clazz = tomatoSoupKtTestClass.checkBaseDefinition()
32+
val method = clazz.declaredMethods.findMethod(generateSpicesMethod)
33+
34+
return try {
35+
method.invokeWithoutArgs(clazz = clazz) as Sequence<SpiceType>
36+
} catch(e: InvocationTargetException) {
37+
assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" }
38+
emptySequence()
39+
}
40+
}
41+
42+
@Test
43+
fun addSpicesMethodTest() {
44+
clearActions()
45+
val clazz = tomatoSoupKtTestClass.checkBaseDefinition()
46+
val method = clazz.declaredMethods.findMethod(addSpicesMethod)
47+
48+
try {
49+
method.invoke(clazz, generateSpices())
50+
} catch(e: InvocationTargetException) {
51+
assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" }
52+
}
53+
54+
assert(actions.isNotEmpty() && actions.all{ it.type == ActionType.PUT_IN_POT }) { "The ${method.name} should generate spices and add them into the pot." }
55+
}
2156

2257
@Test
2358
fun getTomatoesForSoupMethodTest() {

culinaryServer/culinaryServerAddSpices/test/TomatoSoupFunctions.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,31 @@ internal val prepareTomatoesMethod = TestMethod(
2121
)
2222
)
2323

24+
internal val generateSpicesMethod = TestMethod(
25+
"generateSpices",
26+
TestKotlinType("Sequence", params = listOf("org.jetbrains.kotlin.course.culinary.models.food.SpiceType")),
27+
)
28+
29+
internal val addSpicesMethod = TestMethod(
30+
"addSpices",
31+
TestKotlinType("Unit"),
32+
returnTypeJava = "void",
33+
arguments = listOf(
34+
TestVariable(
35+
name = "spices",
36+
javaType = "Sequence",
37+
kotlinType = TestKotlinType("Sequence", params = listOf("org.jetbrains.kotlin.course.culinary.models.food.SpiceType")),
38+
),
39+
)
40+
)
41+
2442
internal val tomatoSoupKtTestClass = TestClass(
2543
"TomatoSoupKt",
2644
"org.jetbrains.kotlin.course.culinary.game.recipes",
2745
customMethods = listOf(
2846
getTomatoesForSoupMethod,
29-
prepareTomatoesMethod
47+
prepareTomatoesMethod,
48+
generateSpicesMethod,
49+
addSpicesMethod
3050
),
3151
)

culinaryServer/culinaryServerCookSalad/task-info.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ files:
6262
- name: test/FridgeImplTestClass.kt
6363
visible: false
6464
propagatable: false
65+
- name: test/Tests.kt
66+
visible: false
67+
propagatable: false
6568
- name: test/TomatoSoupFunctions.kt
6669
visible: false
6770
propagatable: false
68-
- name: test/Tests.kt
71+
- name: test/SaladFunctions.kt
6972
visible: false
7073
propagatable: false
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.jetbrains.academy.test.system.core.models.TestKotlinType
2+
import org.jetbrains.academy.test.system.core.models.classes.TestClass
3+
import org.jetbrains.academy.test.system.core.models.method.TestMethod
4+
import org.jetbrains.academy.test.system.core.models.variable.TestVariable
5+
6+
internal val getFreshVegetablesMethod = TestMethod(
7+
"getFreshVegetables",
8+
TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.Vegetable")),
9+
)
10+
11+
internal val cutMethod = TestMethod(
12+
"cut",
13+
TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.CutVegetable")),
14+
arguments = listOf(
15+
TestVariable(
16+
name = "vegetables",
17+
javaType = "List",
18+
kotlinType = TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.Vegetable")),
19+
),
20+
)
21+
)
22+
23+
internal val saladKtTestClass = TestClass(
24+
"SaladKt",
25+
"org.jetbrains.kotlin.course.culinary.game.recipes",
26+
customMethods = listOf(
27+
getFreshVegetablesMethod,
28+
cutMethod
29+
),
30+
)

culinaryServer/culinaryServerCookSalad/test/Tests.kt

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,114 @@ import org.jetbrains.academy.test.system.core.models.classes.ConstructorGetter
55
import org.jetbrains.kotlin.course.culinary.game.actions
66
import org.jetbrains.kotlin.course.culinary.game.clearActions
77
import org.jetbrains.kotlin.course.culinary.game.recipes.NUMBER_OF_TOMATOES
8+
import org.jetbrains.kotlin.course.culinary.game.recipes.NUM_VEGETABLES_FOR_SALAD
89
import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl
910
import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl.RANDOM_FRESH_VEGETABLES_NUMBER
1011
import org.jetbrains.kotlin.course.culinary.implementation.storage.FridgeImpl.RANDOM_VEGETABLES_NUMBER
1112
import org.jetbrains.kotlin.course.culinary.models.ItemType
1213
import org.jetbrains.kotlin.course.culinary.models.action.Action
1314
import org.jetbrains.kotlin.course.culinary.models.action.ActionType
15+
import org.jetbrains.kotlin.course.culinary.models.food.CutVegetable
16+
import org.jetbrains.kotlin.course.culinary.models.food.SpiceType
1417
import org.jetbrains.kotlin.course.culinary.models.food.Vegetable
1518
import org.jetbrains.kotlin.course.culinary.models.food.VegetableType
1619
import org.junit.jupiter.api.Test
1720
import java.lang.reflect.InvocationTargetException
1821

1922
class Test {
20-
// TODO: add tests for the current task
23+
// TODO: add tests for mixVegetablesForSalad
24+
@Test
25+
fun getFreshVegetablesMethodTest() {
26+
val randomVegetables = fillFridge()
27+
val vegetables = getFreshVegetables()
28+
assert(randomVegetables.filter{ it.isFresh }.sortedBy { it.type } == vegetables.sortedBy { it.type }) { "The ${getFreshVegetablesMethod.name} should return all fresh vegetables from the fridge." }
29+
assert(FridgeImpl.vegetables.none { it.isFresh }) { "The ${getFreshVegetablesMethod.name} should return all fresh vegetables from the fridge." }
30+
}
31+
32+
private fun fillFridge(): List<Vegetable> {
33+
val randomVegetables = generateRandomVegetables()
34+
FridgeImpl.vegetables.clear()
35+
FridgeImpl.vegetables.addAll(randomVegetables)
36+
return randomVegetables
37+
}
38+
39+
private fun getFreshVegetables(): List<Vegetable> {
40+
val clazz = saladKtTestClass.checkBaseDefinition()
41+
val method = clazz.declaredMethods.findMethod(getFreshVegetablesMethod)
42+
43+
return try {
44+
method.invokeWithoutArgs(clazz = clazz) as List<Vegetable>
45+
} catch(e: InvocationTargetException) {
46+
assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" }
47+
emptyList()
48+
}
49+
}
50+
51+
@Test
52+
fun cutMethodTest() {
53+
clearActions()
54+
fillFridge()
55+
56+
val clazz = saladKtTestClass.checkBaseDefinition()
57+
val method = clazz.declaredMethods.findMethod(cutMethod)
58+
val vegetables = getFreshVegetables()
59+
val expectedVegetables = vegetables.take(NUM_VEGETABLES_FOR_SALAD).map{ CutVegetable(it.type) }
60+
61+
val cutVegetables = try {
62+
method(clazz, vegetables) as List<CutVegetable>
63+
} catch(e: InvocationTargetException) {
64+
assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" }
65+
emptyList()
66+
}
67+
68+
assert(expectedVegetables.size == cutVegetables.size) { "The ${method.name} method should take all vegetables from the list of vegetables and cut them!" }
69+
assert(expectedVegetables.sortedBy { it.type } == cutVegetables.sortedBy { it.type }) { "The ${method.name} method should take vegetables and cut them!" }
70+
71+
val expectedActions = buildList {
72+
addAll(List(vegetables.size) { ActionType.SHOW_ON_COUNTER })
73+
repeat(vegetables.size) {
74+
addAll(listOf(ActionType.CUT_ON_COUNTER, ActionType.SHOW_ON_COUNTER))
75+
}
76+
addAll(List(NUM_VEGETABLES_FOR_SALAD) { ActionType.REMOVE_FROM_COUNTER })
77+
}
78+
assert(expectedActions == actions.map{ it.type }) { "The ${method.name} method should take vegetables and cut them: take each of them from the fridge, and then cut" }
79+
}
80+
81+
@Test
82+
fun generateSpicesMethodTest() {
83+
val spices = generateSpices().take(5).toList()
84+
if (spices.isEmpty()) {
85+
assert(false) { "The method ${generateSpicesMethod.name} should generate random spices! Now you always generate an empty sequence!" }
86+
}
87+
assert(spices.toSet().size > 1) { "The method ${generateSpicesMethod.name} should generate random spices! Now you always generate ${spices.first()}" }
88+
}
89+
90+
private fun generateSpices(): Sequence<SpiceType> {
91+
val clazz = tomatoSoupKtTestClass.checkBaseDefinition()
92+
val method = clazz.declaredMethods.findMethod(generateSpicesMethod)
93+
94+
return try {
95+
method.invokeWithoutArgs(clazz = clazz) as Sequence<SpiceType>
96+
} catch(e: InvocationTargetException) {
97+
assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" }
98+
emptySequence()
99+
}
100+
}
101+
102+
@Test
103+
fun addSpicesMethodTest() {
104+
clearActions()
105+
val clazz = tomatoSoupKtTestClass.checkBaseDefinition()
106+
val method = clazz.declaredMethods.findMethod(addSpicesMethod)
107+
108+
try {
109+
method.invoke(clazz, generateSpices())
110+
} catch(e: InvocationTargetException) {
111+
assert(false) { "Can not invoke method ${method.name}. Please, add an implementation!" }
112+
}
113+
114+
assert(actions.isNotEmpty() && actions.all{ it.type == ActionType.PUT_IN_POT }) { "The ${method.name} should generate spices and add them into the pot." }
115+
}
21116

22117
@Test
23118
fun getTomatoesForSoupMethodTest() {

culinaryServer/culinaryServerCookSalad/test/TomatoSoupFunctions.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,31 @@ internal val prepareTomatoesMethod = TestMethod(
2121
)
2222
)
2323

24+
internal val generateSpicesMethod = TestMethod(
25+
"generateSpices",
26+
TestKotlinType("Sequence", params = listOf("org.jetbrains.kotlin.course.culinary.models.food.SpiceType")),
27+
)
28+
29+
internal val addSpicesMethod = TestMethod(
30+
"addSpices",
31+
TestKotlinType("Unit"),
32+
returnTypeJava = "void",
33+
arguments = listOf(
34+
TestVariable(
35+
name = "spices",
36+
javaType = "Sequence",
37+
kotlinType = TestKotlinType("Sequence", params = listOf("org.jetbrains.kotlin.course.culinary.models.food.SpiceType")),
38+
),
39+
)
40+
)
41+
2442
internal val tomatoSoupKtTestClass = TestClass(
2543
"TomatoSoupKt",
2644
"org.jetbrains.kotlin.course.culinary.game.recipes",
2745
customMethods = listOf(
2846
getTomatoesForSoupMethod,
29-
prepareTomatoesMethod
47+
prepareTomatoesMethod,
48+
generateSpicesMethod,
49+
addSpicesMethod
3050
),
3151
)

culinaryServer/culinaryServerCookSmoothie/task-info.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ files:
5959
visible: true
6060
- name: src/main/kotlin/org/jetbrains/kotlin/course/culinary/game/recipes/Smoothie.kt
6161
visible: true
62-
- name: test/FridgeImplTestClass.kt
62+
- name: test/SaladFunctions.kt
6363
visible: false
6464
propagatable: false
65-
- name: test/TomatoSoupFunctions.kt
65+
- name: test/FridgeImplTestClass.kt
6666
visible: false
6767
propagatable: false
6868
- name: test/Tests.kt
6969
visible: false
7070
propagatable: false
71+
- name: test/TomatoSoupFunctions.kt
72+
visible: false
73+
propagatable: false
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.jetbrains.academy.test.system.core.models.TestKotlinType
2+
import org.jetbrains.academy.test.system.core.models.classes.TestClass
3+
import org.jetbrains.academy.test.system.core.models.method.TestMethod
4+
import org.jetbrains.academy.test.system.core.models.variable.TestVariable
5+
6+
internal val getFreshVegetablesMethod = TestMethod(
7+
"getFreshVegetables",
8+
TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.Vegetable")),
9+
)
10+
11+
internal val cutMethod = TestMethod(
12+
"cut",
13+
TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.CutVegetable")),
14+
arguments = listOf(
15+
TestVariable(
16+
name = "vegetables",
17+
javaType = "List",
18+
kotlinType = TestKotlinType("List", params = listOf("org.jetbrains.kotlin.course.culinary.ingredient.Vegetable")),
19+
),
20+
)
21+
)
22+
23+
internal val saladKtTestClass = TestClass(
24+
"SaladKt",
25+
"org.jetbrains.kotlin.course.culinary.game.recipes",
26+
customMethods = listOf(
27+
getFreshVegetablesMethod,
28+
cutMethod
29+
),
30+
)

0 commit comments

Comments
 (0)