Last active
May 25, 2024 10:20
-
-
Save dacr/41c95c5485d327f75ea172be77afa737 to your computer and use it in GitHub Desktop.
simple scala xml operations / published by https://github.com/dacr/code-examples-manager #8f13bd90-9553-4225-b756-d708b4c7b6d0/7f94fb9a1e2b079042abd3fa76ff1e31afbc2602
This file contains hidden or 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
// summary : simple scala xml operations | |
// keywords : scala, xml, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 8f13bd90-9553-4225-b756-d708b4c7b6d0 | |
// created-on : 2020-08-25T10:53:15Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.scalatest::scalatest:3.2.10" | |
//> using dep "org.scala-lang.modules::scala-xml:2.0.1" | |
// --------------------- | |
import org.scalatest._, flatspec._, matchers._ | |
import scala.xml._ | |
object XMLBasicsTest extends AnyFlatSpec with should.Matchers { | |
override def suiteName = "XMLBasicsTest" | |
"scala.xml library" should "allow simple XML syntax" in { | |
val sample = { | |
<that> | |
<truc> | |
<bidule prop1="val1" prop2="val2"> | |
{1+1} | |
</bidule> | |
</truc> | |
</that> | |
} | |
(sample \ "truc" \ "bidule" \@ "prop1") shouldBe "val1" | |
(sample \\ "bidule" \@ "prop1") shouldBe "val1" | |
(sample \\ "bidule").text.trim shouldBe "2" | |
} | |
it should "be possible to parse an xml string" in { | |
val sample = """ | |
|<that> | |
| <truc> | |
| <bidule prop1="val1" prop2="val2"> | |
| </bidule> | |
| </truc> | |
|</that>""".stripMargin | |
val parsed = XML.loadString(sample) | |
(parsed \ "truc" \ "bidule" \@ "prop1") shouldBe "val1" | |
(parsed \\ "bidule" \@ "prop1") shouldBe "val1" | |
} | |
it should "be possible to serialize to a string" in { | |
val msg = "hello world" | |
val sample = <truc><bidule><machin msg={msg}></machin></bidule></truc> | |
sample.toString() shouldBe """<truc><bidule><machin msg="hello world"></machin></bidule></truc>""" | |
} | |
} | |
XMLBasicsTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment