Skip to content

Commit

Permalink
fix: add tests - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Anty0 committed Dec 18, 2024
1 parent 5c05299 commit 3da85ff
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
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)
}
}
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! &amp; &lt; &gt; &apos;"
.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>&amp;</b>"
.assertParsedTo("I am just a text! &amp;", 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)
}
}

0 comments on commit 3da85ff

Please sign in to comment.