From 27126b25bddc9d09f6b2566a2949718e293ce5e6 Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Sun, 19 Jun 2022 18:04:36 +0200
Subject: [PATCH] Csv exporter add quotations mark to quotation mark

- IOHandler.kt change addQuotationMarks()
- Adapt test to cover this case
---
 .../main/kotlin/rocks/theodolite/core/IOHandler.kt    | 11 +++++++++--
 .../kotlin/rocks/theodolite/core/IOHandlerTest.kt     |  6 +++---
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
index 78b353a1e..e70779afb 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
@@ -85,13 +85,20 @@ class IOHandler {
 
     /**
      * 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> {
         val stringMutableList = stringList.toMutableList()
         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(",")){
-                stringMutableList[index] = "\"" + entry + "\""
+                stringMutableList[index] = '"' + stringMutableList[index] + '"'
             }
         }
         return stringMutableList
diff --git a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
index 938eeaa7a..e54bf7851 100644
--- a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
+++ b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
@@ -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
     fun testWriteToCSVFileWithComma() {
@@ -79,13 +79,13 @@ internal class IOHandlerTest {
         val expectedColumns = listOf("\"Fruit, Fruit2\"", "Color")
 
         val testContent = listOf(
-            listOf("apples, paprika", "red"),
+            listOf("apples, "+ '"' + "paprika" + '"', "red"),
             listOf("bananas, pineapple", "yellow"),
             listOf("avocado, coconut", "brown")
         )
 
         val expectedContent = listOf(
-            listOf("\"apples, paprika\"", "red"),
+            listOf("\"apples, " + '"' + '"' + "paprika" + '"' + '"' + '"', "red"),
             listOf("\"bananas, pineapple\"", "yellow"),
             listOf("\"avocado, coconut\"", "brown")
         )
-- 
GitLab