Skip to content
Snippets Groups Projects
Commit 27126b25 authored by Lorenz Boguhn's avatar Lorenz Boguhn Committed by Sören Henning
Browse files

Csv exporter add quotations mark to quotation mark

- IOHandler.kt change addQuotationMarks()
- Adapt test to cover this case
parent 31318314
No related branches found
No related tags found
1 merge request!289Fix Csv export
Pipeline #8777 passed
...@@ -85,13 +85,20 @@ class IOHandler { ...@@ -85,13 +85,20 @@ class IOHandler {
/** /**
* For a list of Strings: * For a list of Strings:
* Adds quotation marks around strings that contain a comma * - adds additional quotation mark to existing one
* - adds quotation marks around entries that contain a comma
*/ */
private fun addQuotationMarks(stringList: List<String> ): List<String> { private fun addQuotationMarks(stringList: List<String> ): List<String> {
val stringMutableList = stringList.toMutableList() val stringMutableList = stringList.toMutableList()
stringMutableList.forEachIndexed { index, entry -> stringMutableList.forEachIndexed { index, entry ->
// add additional quotation marks to escape them in csv
if (entry.contains("\"")){
stringMutableList[index] = stringMutableList[index].replace('"'+"", "\"" + '"')
}
// add quotation marks around entries that contain a comma
if (entry.contains(",")){ if (entry.contains(",")){
stringMutableList[index] = "\"" + entry + "\"" stringMutableList[index] = '"' + stringMutableList[index] + '"'
} }
} }
return stringMutableList return stringMutableList
......
...@@ -68,7 +68,7 @@ internal class IOHandlerTest { ...@@ -68,7 +68,7 @@ internal class IOHandlerTest {
} }
/** /**
* Tests if values with commas are surrounded with quotation marks. * Tests if values with commas and quotation marks are surrounded with additional quotation marks.
*/ */
@Test @Test
fun testWriteToCSVFileWithComma() { fun testWriteToCSVFileWithComma() {
...@@ -79,13 +79,13 @@ internal class IOHandlerTest { ...@@ -79,13 +79,13 @@ internal class IOHandlerTest {
val expectedColumns = listOf("\"Fruit, Fruit2\"", "Color") val expectedColumns = listOf("\"Fruit, Fruit2\"", "Color")
val testContent = listOf( val testContent = listOf(
listOf("apples, paprika", "red"), listOf("apples, "+ '"' + "paprika" + '"', "red"),
listOf("bananas, pineapple", "yellow"), listOf("bananas, pineapple", "yellow"),
listOf("avocado, coconut", "brown") listOf("avocado, coconut", "brown")
) )
val expectedContent = listOf( val expectedContent = listOf(
listOf("\"apples, paprika\"", "red"), listOf("\"apples, " + '"' + '"' + "paprika" + '"' + '"' + '"', "red"),
listOf("\"bananas, pineapple\"", "yellow"), listOf("\"bananas, pineapple\"", "yellow"),
listOf("\"avocado, coconut\"", "brown") listOf("\"avocado, coconut\"", "brown")
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment