Skip to content

Commit

Permalink
Rename getRelativePosition to getTaskIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 committed Jan 9, 2025
1 parent d4a3ba0 commit 2be6aab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TaskSequenceHandler(private val tasks: List<Task>) {
}

/** Generates the task sequence based on conditions and overrides. */
private fun generateTaskSequence(
fun generateTaskSequence(
tag: String,
taskValueOverride: Pair<String, TaskData?>? = null,
): Sequence<Task> {
Expand Down Expand Up @@ -167,25 +167,20 @@ class TaskSequenceHandler(private val tasks: List<Task>) {
return index
}

/** Retrieves the relative position of a task in the sequence. */
fun getTaskIndex(taskId: String): Int {
validateTaskId(taskId)
val index = getTaskSequence().indexOfFirst { it.id == taskId }
require(index >= 0) { "Task '$taskId' not found in the sequence." }
return index
}

/**
* Retrieves the relative index in the computed task sequence.
* Retrieves the relative index of task in the computed task sequence.
*
* The relative index represents the task's position within the currently displayed sequence.
*
* @param taskId The ID of the task for which to find the relative position.
* @throws IllegalArgumentException if the provided [taskId] is blank.
* @throws NoSuchElementException if the task is not found in the computed task sequence.
*/
fun getRelativePosition(taskId: String): Int {
return getTaskIndex(taskId)
fun getTaskIndex(taskId: String): Int {
validateTaskId(taskId)
val index = getTaskSequence().indexOfFirst { it.id == taskId }
require(index >= 0) { "Task '$taskId' not found in the sequence." }
return index
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class TaskSequenceHandlerTest {
@Test
fun `createTaskSequence returns all tasks when shouldIncludeTask always returns true`() {
val handler = createHandler()
val sequence = handler.createTaskSequence("")
val sequence = handler.generateTaskSequence("")
assertThat(sequence.toList()).isEqualTo(allTasks)
}

@Test
fun `createTaskSequence filters tasks based on shouldIncludeTask`() {
val handler =
createHandler(shouldIncludeTask = { task, _ -> task.id != "task2" && task.id != "task4" })
val sequence = handler.createTaskSequence("")
val sequence = handler.generateTaskSequence("")
assertThat(sequence.toList()).isEqualTo(listOf(task1, task3, task5))
}

Expand All @@ -73,7 +73,7 @@ class TaskSequenceHandlerTest {
!(task.id == "task3" && taskValueOverride?.first == "task3")
}
)
val sequence = handler.createTaskSequence("", taskValueOverride = "task3" to null)
val sequence = handler.generateTaskSequence("", taskValueOverride = "task3" to null)
assertThat(sequence.toList()).isEqualTo(listOf(task1, task5))
}

Expand Down Expand Up @@ -180,22 +180,22 @@ class TaskSequenceHandlerTest {
}

@Test
fun `getRelativePosition returns the correct position`() {
fun `getTaskIndex returns the correct position`() {
val handler =
createHandler(shouldIncludeTask = { task, _ -> task.id != "task2" && task.id != "task4" })
assertThat(handler.getRelativePosition("task3")).isEqualTo(1)
assertThat(handler.getTaskIndex("task3")).isEqualTo(1)
}

@Test
fun `getRelativePosition throws error for invalid task id`() {
fun `getTaskIndex throws error for invalid task id`() {
val handler = createHandler()
assertThrows(IllegalArgumentException::class.java) { handler.getRelativePosition("") }
assertThrows(IllegalArgumentException::class.java) { handler.getTaskIndex("") }
}

@Test
fun `getRelativePosition throws error when task is not found`() {
fun `getTaskIndex throws error when task is not found`() {
val handler = createHandler()
assertThrows(IllegalArgumentException::class.java) { handler.getRelativePosition("invalid") }
assertThrows(IllegalArgumentException::class.java) { handler.getTaskIndex("invalid") }
}

@Test
Expand Down

0 comments on commit 2be6aab

Please sign in to comment.