Skip to content

Commit

Permalink
Add id to FillBlanksItem
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderZhu committed Oct 18, 2023
1 parent 5d2b19f commit 65ea64b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.hyperskill.app.step_quiz_fill_blanks.model

sealed interface FillBlanksItem {
import ru.nobird.app.core.model.Identifiable

sealed interface FillBlanksItem : Identifiable<Int> {
data class Text(
override val id: Int,
val text: String,
val startsWithNewLine: Boolean
) : FillBlanksItem

data class Input(
override val id: Int,
val inputText: String?
) : FillBlanksItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ object FillBlanksItemMapper {
return if (match != null) {
val inputComponents = componentsDataset.slice(from = 1)
val (langClass, content) = match.destructured
val fillBlanks = splitContent(content) { inputIndex ->
val fillBlanks = splitContent(content) { id, inputIndex ->
FillBlanksItem.Input(
id = id,
inputText = replyBlanks?.getOrNull(inputIndex)
?: inputComponents.getOrNull(inputIndex)?.text,
)
Expand All @@ -56,21 +57,27 @@ object FillBlanksItemMapper {

private fun splitContent(
content: String,
produceInputItem: (Int) -> FillBlanksItem.Input
produceInputItem: (id: Int, inputIndex: Int) -> FillBlanksItem.Input
): List<FillBlanksItem> {
var nextDelimiterIndex = content.indexOfAny(DELIMITERS)
if (nextDelimiterIndex == -1) {
return listOf(
FillBlanksItem.Text(content, startsWithNewLine = false)
FillBlanksItem.Text(
id = 0,
text = content,
startsWithNewLine = false
)
)
}
return buildList {
var currentOffset = 0
var previousDelimiterIsLineBreak = false
var blankIndex = 0
var inputIndex = 0
var id = 0
do {
add(
FillBlanksItem.Text(
id = id++,
text = content.substring(currentOffset, nextDelimiterIndex),
startsWithNewLine = previousDelimiterIsLineBreak
)
Expand All @@ -79,7 +86,7 @@ object FillBlanksItemMapper {
val delimiter = content[nextDelimiterIndex]
if (delimiter == FillBlanksConfig.BLANK_FIELD_CHAR) {
add(
produceInputItem(blankIndex++)
produceInputItem(id++, inputIndex++)
)
}

Expand All @@ -89,6 +96,7 @@ object FillBlanksItemMapper {
} while(nextDelimiterIndex != -1)
add(
FillBlanksItem.Text(
id = id,
text = content.substring(currentOffset, content.length),
startsWithNewLine = previousDelimiterIsLineBreak
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ class FillBlanksMapperTest {

private fun expectedItems(firstReply: String? = null, secondReply: String? = null) =
listOf(
FillBlanksItem.Text("Mark the following function's return type as string:", false),
FillBlanksItem.Text("", true),
FillBlanksItem.Text("def function1() ", true),
FillBlanksItem.Input(firstReply),
FillBlanksItem.Text(":", startsWithNewLine = false),
FillBlanksItem.Text(" return \"This function should return a string!\" ", true),
FillBlanksItem.Text(text = "", startsWithNewLine = true),
FillBlanksItem.Text(0, "Mark the following function's return type as string:", false),
FillBlanksItem.Text(1, "", true),
FillBlanksItem.Text(2, "def function1() ", true),
FillBlanksItem.Input(3, firstReply),
FillBlanksItem.Text(4, ":", startsWithNewLine = false),
FillBlanksItem.Text(5, " return \"This function should return a string!\" ", true),
FillBlanksItem.Text(6, text = "", startsWithNewLine = true),
FillBlanksItem.Text(
7,
text = "Mark the following function's return type as a set of floats:",
startsWithNewLine = true
),
FillBlanksItem.Text(text = "", startsWithNewLine = true),
FillBlanksItem.Text(text = "def function2() ", startsWithNewLine = true),
FillBlanksItem.Input(secondReply),
FillBlanksItem.Text(":", startsWithNewLine = false),
FillBlanksItem.Text(" return {1, 2, 3, 4} ", startsWithNewLine = true)
FillBlanksItem.Text(8, text = "", startsWithNewLine = true),
FillBlanksItem.Text(9, text = "def function2() ", startsWithNewLine = true),
FillBlanksItem.Input(10, secondReply),
FillBlanksItem.Text(11, ":", startsWithNewLine = false),
FillBlanksItem.Text(12," return {1, 2, 3, 4} ", startsWithNewLine = true)
)

@Test
Expand Down

0 comments on commit 65ea64b

Please sign in to comment.