Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
improved testing for TaskStorageService.kt #2 #10
Browse files Browse the repository at this point in the history
  • Loading branch information
V3lop5 committed Apr 6, 2021
1 parent 76f2146 commit 2798b1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
12 changes: 3 additions & 9 deletions src/services/TaskStorageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.adclock.util.createOrEmptyFile
import com.adclock.util.readLinesIndexed
import java.io.File

class TaskStorageService {
class TaskStorageService(private val taskFolder: File) {

fun loadTask(name: String): Task {
val file = getTaskFile(name)
Expand Down Expand Up @@ -45,20 +45,14 @@ class TaskStorageService {

fun exists(name: String) = getTaskFile(name).exists()

fun taskNames(): Array<String> = TASK_FOLDER.list { _, name -> name.endsWith(FILE_EXTENSION) }
fun taskNames(): Array<String> = taskFolder.list { _, name -> name.endsWith(FILE_EXTENSION) }
?: throw IllegalStateException("Error loading task names.")


private fun getTaskFile(name: String) = File(TASK_FOLDER, "$name$FILE_EXTENSION")
private fun getTaskFile(name: String) = File(taskFolder, "$name$FILE_EXTENSION")

companion object {
private val TASK_FOLDER = File("tasks/")
private const val FILE_EXTENSION = ".adc"

init {
if (!TASK_FOLDER.exists())
TASK_FOLDER.mkdirs()
}
}

}
27 changes: 23 additions & 4 deletions test/services/TaskServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
package com.adclock.services

import com.adclock.instructionset.instructions.wall.RunInstruction
import org.junit.After
import org.junit.Before
import org.junit.rules.TemporaryFolder
import kotlin.test.Test
import kotlin.test.assertEquals

class TaskServiceTest {

val tempFolder = TemporaryFolder()

@Before
fun prepareTempFolder() {
tempFolder.create()
}

@After
fun cleanupTempFolder() {
tempFolder.create()
}

private fun getTaskStorageService(): TaskStorageService {
return TaskStorageService(tempFolder.newFolder("tasks/"))
}

@Test
fun shouldReturnEmptyTaskNameList() {
val service = TaskService(TaskStorageService())
val service = TaskService(getTaskStorageService())
assertEquals(emptyList(), service.getTaskNameList(), "Task list")
}

@Test
fun shouldReturnOneTaskNameList() {
val service = TaskService(TaskStorageService())
val service = TaskService(getTaskStorageService())
service.createTask("Foo")
assertEquals(listOf("Foo"), service.getTaskNameList(), "Task list")
}

@Test
fun addTaskWithNoInstructions() {
val service = TaskService(TaskStorageService())
val service = TaskService(getTaskStorageService())
service.createTask("Foo")
val task = service.getTask("Foo")
assertEquals("Foo", task.name, "Task name")
Expand All @@ -31,7 +50,7 @@ class TaskServiceTest {

@Test
fun addTaskWithSingleInstruction() {
val service = TaskService(TaskStorageService())
val service = TaskService(getTaskStorageService())
service.createTask("Simple", listOf("RUN"))
val task = service.getTask("Simple")
assertEquals(1, task.instructions.size, "Task instructions list size")
Expand Down

0 comments on commit 2798b1e

Please sign in to comment.