From 7d763441cfc18c3b6d894a46c1cc8a7bb5e6303e Mon Sep 17 00:00:00 2001 From: Andre Hitchman Date: Fri, 12 Nov 2021 18:17:25 +0000 Subject: [PATCH] ignore empty lines from input file --- src/main/kotlin/util/FileReader.kt | 2 +- src/test/kotlin/util/FileReaderTest.kt | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/util/FileReader.kt b/src/main/kotlin/util/FileReader.kt index f0c3783..2a8ba96 100644 --- a/src/main/kotlin/util/FileReader.kt +++ b/src/main/kotlin/util/FileReader.kt @@ -8,7 +8,7 @@ class FileReader : Reader { var lines: List = emptyList() try { - lines = File(fileName).readLines() + lines = File(fileName).readLines().filter { it.isNotEmpty() } } catch (exception: Exception) { when (exception) { is FileNotFoundException -> println("Oops! - File not found.") diff --git a/src/test/kotlin/util/FileReaderTest.kt b/src/test/kotlin/util/FileReaderTest.kt index 821f1ec..6c46cb5 100644 --- a/src/test/kotlin/util/FileReaderTest.kt +++ b/src/test/kotlin/util/FileReaderTest.kt @@ -4,7 +4,6 @@ import FileReader import com.google.common.truth.Truth.assertThat import io.mockk.every import io.mockk.impl.annotations.MockK -import io.mockk.mockkStatic import org.junit.After import org.junit.Before import org.junit.Test @@ -56,6 +55,29 @@ class FileReaderTest { assertThat(result).isEqualTo(expectedOutputString) } + @Test + fun `readFile() - given file with empty lines then return list of strings of the file's contents data without empty lines`() { + // given + val fileName = "src/test/resources/test_vet_patient_file_with_empty_lines.txt" + val expectedOutputString: ArrayList = arrayListOf( + "customer:1:Sam", + "customer:2:Voicu", + "customer:3:Andre", + "customer:4:Harry", + "pet:1:dog:Barney:1", + "pet:2:cat:Mr Fuzzy:1", + "pet:3:cat:Kitkat:2", + "pet:4:goldfish:Harry:3", + "pet:5:dog:Bob:4" + ) + + // when + val result = subject.readFile(fileName) + + // then + assertThat(result).isEqualTo(expectedOutputString) + } + @Test fun `readFile() - given file name that does not exist then file not found message is printed`() { // given