-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...end/data/src/test/kotlin/io/tolgee/unit/formats/compose/in/ComposeStringsUnescaperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.tolgee.unit.formats.compose.`in` | ||
|
||
import io.tolgee.formats.compose.`in`.ComposeStringUnescaper | ||
import io.tolgee.testing.assert | ||
import org.junit.jupiter.api.Test | ||
|
||
class ComposeStringsUnescaperTest { | ||
@Test | ||
fun `keeps raw text unchanged`() { | ||
"\n\t\u0020\u2008\u2003\"".assertUnescaped("\n\t\u0020\u2008\u2003\"") | ||
} | ||
|
||
@Test | ||
fun `unquoted spaces are kept`() { | ||
" \n\t\u0020\u2008\u2003 a \n\t\u0020\u2008\u2003 a \n\t\u0020\u2008\u2003 ".assertUnescaped( | ||
" \n\t\u0020\u2008\u2003 a \n\t\u0020\u2008\u2003 a \n\t\u0020\u2008\u2003 " | ||
) | ||
} | ||
|
||
@Test | ||
fun `quote does not affect text`() { | ||
"a a\" ".assertUnescaped("a a\" ") | ||
} | ||
|
||
@Test | ||
fun `keeps leading space`() { | ||
" a".assertUnescaped(" a") | ||
} | ||
|
||
@Test | ||
fun `keeps trailing space`() { | ||
"a ".assertUnescaped("a ") | ||
} | ||
|
||
@Test | ||
fun `escaped chars are unescaped`() { | ||
"\\n \\t \\\\".assertUnescaped("\n \t \\") | ||
} | ||
|
||
@Test | ||
fun `chars that don't need escaping are kept unchanged`() { | ||
"\\\" \\' \" '".assertUnescaped("\\\" \\' \" '") | ||
} | ||
|
||
@Test | ||
fun `keeps apos`() { | ||
"So funktioniert's".assertUnescaped("So funktioniert's") | ||
} | ||
|
||
private fun String.assertUnescaped(expected: String) { | ||
ComposeStringUnescaper(this.asSequence()).result.assert.isEqualTo(expected) | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...d/data/src/test/kotlin/io/tolgee/unit/formats/compose/in/ComposeXmlResourcesParserTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package io.tolgee.unit.formats.compose.`in` | ||
|
||
import io.tolgee.formats.compose.`in`.ComposeStringUnescaper | ||
import io.tolgee.formats.xmlResources.StringUnit | ||
import io.tolgee.formats.xmlResources.XmlResourcesStringValue | ||
import io.tolgee.formats.xmlResources.XmlResourcesStringsModel | ||
import io.tolgee.formats.xmlResources.`in`.XmlResourcesParser | ||
import io.tolgee.testing.assert | ||
import org.junit.jupiter.api.Test | ||
import javax.xml.stream.XMLEventReader | ||
import javax.xml.stream.XMLInputFactory | ||
|
||
class ComposeXmlResourcesParserTest { | ||
@Test | ||
fun `it removes unsupported tags`() { | ||
"<unsupported><b>text</b></unsupported>".assertParsedTo("text", false) | ||
} | ||
|
||
@Test | ||
fun `replaces CDATA elements with inner text`() { | ||
"\n<![CDATA[<b>text</b>]]>\n".assertParsedTo("\n<b>text</b>\n", false) | ||
} | ||
|
||
@Test | ||
fun `sets wrapped with CDATA when the only node is wrapped with CDATA`() { | ||
"<![CDATA[<b>text</b>]]>".assertParsedTo("<b>text</b>", true) | ||
} | ||
|
||
@Test | ||
fun `it parses self-closing tag`() { | ||
"text <br/>".assertParsedTo("text ", false) | ||
} | ||
|
||
@Test | ||
fun `CDATA block is unescaped`() { | ||
"""<![CDATA[<a href="Cool">text\n</a>]]>""".assertParsedTo("<a href=\"Cool\">text\n</a>", true) | ||
} | ||
|
||
@Test | ||
fun `correctly handles spaces between tags`() { | ||
" <b> text </b><br/> <b> a a a </b> <b></b> " | ||
.assertParsedTo(" text a a a ", false) | ||
} | ||
|
||
@Test | ||
fun `doesn't escape XML to entities for pure text`() { | ||
"I am just a text! & < > '" | ||
.assertParsedTo("I am just a text! & < > '", false) | ||
} | ||
|
||
@Test | ||
fun `parses element with attributes`() { | ||
"<a href=\"hey\" />" | ||
.assertParsedTo("", false) | ||
} | ||
|
||
@Test | ||
fun `doesnt unescape amp XML entity when XML context is removed`() { | ||
"I am just a text! <b>&</b>" | ||
.assertParsedTo("I am just a text! &", false) | ||
} | ||
|
||
private fun getReader(data: String): XMLEventReader { | ||
val inputFactory: XMLInputFactory = XMLInputFactory.newInstance() | ||
return inputFactory.createXMLEventReader(data.byteInputStream()) | ||
} | ||
|
||
private fun getReaderWithSingleStringUnit(data: String): XMLEventReader { | ||
return getReader( | ||
""" | ||
<resources> | ||
<string name="name">$data</string> | ||
</resources> | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
private fun parse(reader: XMLEventReader): XmlResourcesStringsModel { | ||
val parser = | ||
XmlResourcesParser( | ||
reader, | ||
ComposeStringUnescaper.defaultFactory, | ||
emptySet(), | ||
) | ||
return parser.parse() | ||
} | ||
|
||
private fun parseSingleStringUnit(data: String): XmlResourcesStringValue? { | ||
val unit = parse(getReaderWithSingleStringUnit(data)).items["name"] as StringUnit | ||
unit.value.assert.isNotNull() | ||
return unit.value | ||
} | ||
|
||
private fun String.assertParsedTo( | ||
expected: String, | ||
isWrappedWithCdata: Boolean, | ||
) { | ||
val parsed = parseSingleStringUnit(this) | ||
parsed?.string.assert.isEqualTo(expected) | ||
parsed?.isWrappedCdata.assert.isEqualTo(isWrappedWithCdata) | ||
} | ||
} |