Skip to content

Commit

Permalink
Fix issed where occurences of XML elements containing text weren't be…
Browse files Browse the repository at this point in the history
…ing validated (#51)
  • Loading branch information
kdallmeyer-sr committed Jul 17, 2019
1 parent 03a371f commit 4d96a4d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,19 @@ internal class LineAwareHandler : DefaultHandler() {
elementStack.pop()
val current = elementStack.peek()
val old = current.hash[localName]!!
current.hash[localName] = XmlDataLeaf(value = textBuffer.trim(), line = old.line, column = old.column)

if (old is XmlDataLeaf && old.value is Iterable<*>) {
val head = old.value.flatMap {
when (it) {
is XmlDataLeaf -> listOf(it.value)
is String -> listOf(it)
else -> emptyList()
}
}
current.hash[localName] = XmlDataLeaf(head + textBuffer.trim(), old.line, old.column)
} else {
current.hash[localName] = XmlDataLeaf(value = textBuffer.trim(), line = old.line, column = old.column)
}
textBuffer.clear()
}
else -> elementStack.pop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.shoprunner.baleen.Baleen
import com.shoprunner.baleen.Context
import com.shoprunner.baleen.Data
import com.shoprunner.baleen.DataValue
import com.shoprunner.baleen.ValidationError
import com.shoprunner.baleen.ValidationInfo
import com.shoprunner.baleen.ValidationSuccess
import com.shoprunner.baleen.dataTrace
Expand Down Expand Up @@ -157,6 +158,61 @@ internal class XmlUtilTest {
))
}

@Test
fun `can validate a list of strings`() {
val pack = Baleen.describe("Pack") { p ->
p.attr(name = "dog",
type = OccurrencesType(StringType(max = 3)),
required = true)
}
val packContainer = Baleen.describe("PackContainer") { p ->
p.attr(name = "pack",
type = pack,
required = true)
}

val multipleOccurrences = """
<pack>
<dog>Dug</dog>
<dog>Fido</dog>
<dog>Bo</dog>
</pack>
""".trimIndent()

val dogLeaf = XmlDataLeaf(value = listOf("Dug", "Fido", "Bo"), line = 2, column = 7)

val packNode = XmlDataNode(1, 7).apply { hash["dog"] = dogLeaf }
val rootNode = XmlDataNode().apply { hash["pack"] = packNode }

val inputStream = multipleOccurrences.byteInputStream()
val context = XmlUtil.fromXmlToContext(dataTrace("example.xml"), inputStream)

val validation = packContainer.validate(context)
val results = validation.results.toList()

assertThat(validation.context).isEqualTo(context)

assertThat(results).hasSize(3)

assertThat(results[0]).isEqualTo(ValidationInfo(
dataTrace = dataTrace("example.xml"),
message = "has attribute \"pack\"",
value = rootNode
))

assertThat(results[1]).isEqualTo(ValidationInfo(
dataTrace = dataTrace("example.xml", "attribute \"pack\"").tag("line", "1").tag("column", "7"),
message = "has attribute \"dog\"",
value = packNode
))

assertThat(results[2]).isEqualTo(ValidationError(
dataTrace = dataTrace("example.xml", "attribute \"pack\"", "attribute \"dog\"", "index 1").tag("line", "2").tag("column", "10"),
message = "is more than 3 characters",
value = "Fido"
))
}

@Test
fun `data trace for root is correct`() {
val inputStream = multipleOccurrences.byteInputStream()
Expand Down

0 comments on commit 4d96a4d

Please sign in to comment.