Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issed where occurences of XML elements containing text weren't being validated #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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