diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt index 1ab6fe98..89397573 100644 --- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt +++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/writers/TomlEmitter.kt @@ -180,7 +180,9 @@ public abstract class TomlEmitter(config: TomlOutputConfig) { val quotes = if (isLiteral) "'''" else "\"\"\"" emit(quotes) + .emitNewLine() .emit(string) + .emitNewLine() .emit(quotes) } else { val quote = if (isLiteral) '\'' else '"' diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/EncodingAnnotationTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/EncodingAnnotationTest.kt index 3147c7b8..4a6ede5c 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/EncodingAnnotationTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/EncodingAnnotationTest.kt @@ -269,16 +269,22 @@ class EncodingAnnotationTest { value = File(), expectedToml = """ mlTextA = $tripleQuotes + \tMultiline text! + $tripleQuotes mlTextB = $tripleQuotes + Text with escaped quotes ""\"\ and line break + $tripleQuotes mlTextC = ''' + "Multiline text!" + ''' """.trimIndent() ) diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt index a2e1d3a9..7f9644be 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/encoders/PrimitiveEncoderTest.kt @@ -1,6 +1,7 @@ package com.akuleshov7.ktoml.encoders import com.akuleshov7.ktoml.annotations.TomlLiteral +import com.akuleshov7.ktoml.annotations.TomlMultiline import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlin.test.Test @@ -81,6 +82,38 @@ class PrimitiveEncoderTest { ) } + @Test + fun multilineStringsSpecifications() { + @Serializable + data class MultilineLiteralStr( + @TomlMultiline + @TomlLiteral + val a: String + ) + + @Serializable + data class MultilineBasicStr( + @TomlMultiline + val a: String + ) + + assertEncodedEquals( + value = MultilineLiteralStr("test \n test \n test \'\'\'"), + expectedToml = """ + |a = ''' + |test + | test + | test ''\' + |''' + """.trimMargin() + ) + + assertEncodedEquals( + value = MultilineBasicStr("test \n test \n test \'\'\'"), + expectedToml = "a = \"\"\"\ntest \n test \n test \'\'\'\n\"\"\"" + ) + } + @Test fun jsWholeDoubleRegression() { @Serializable diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/SetLineNoTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/SetLineNoTest.kt index 1a160329..5e49da51 100644 --- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/SetLineNoTest.kt +++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/parsers/SetLineNoTest.kt @@ -1,15 +1,14 @@ package com.akuleshov7.ktoml.parsers import com.akuleshov7.ktoml.Toml -import com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive import kotlin.test.Test -import kotlin.test.assertContentEquals import kotlin.test.assertEquals class SetLineNoTest { @Test - fun checkingLineNumbers() { - val string = """ + fun checkingLineNumbersGeneral() { + val string = + """ # comment 1 @@ -22,7 +21,7 @@ class SetLineNoTest { [[a.b]] # comment 5 test = 1 - mls = ''' + mlls = ''' 1 2 3 @@ -45,14 +44,43 @@ class SetLineNoTest { | - TomlArrayOfTables ([[a.b]])[line:10] | - TomlArrayOfTablesElement (technical_node)[line:10] | - TomlKeyValuePrimitive (test=1)[line:11] - | - TomlKeyValuePrimitive (mls=''' 1 + | - TomlKeyValuePrimitive (mlls=''' + | 1 | 2 | 3 - | ''')[line:13] + | + |''')[line:13] | - TomlKeyValueArray (mla=[ "a", "b", "c" ])[line:18] | """.trimMargin(), parsedToml.prettyStr(true) ) } + + @Test + fun checkingLineNumbers() { + val string = "\n\n" + + "mlls = '''\n" + + "1\n" + + "\n" + + "2\n" + + "3" + + "'''" + val parsedToml = Toml.tomlParser.parseString(string) + parsedToml.prettyPrint(true) + + assertEquals( + """ + | - TomlFile (rootNode)[line:0] + | - TomlKeyValuePrimitive (mlls=''' + |1 + | + |2 + |3 + |''')[line:3] + | + """.trimMargin(), + parsedToml.prettyStr(true) + ) + } }