txmltree.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/[xmltree, assertions, xmlparser]
  5. block:
  6. var
  7. x: XmlNode
  8. x = <>a(href = "http://nim-lang.org", newText("Nim rules."))
  9. doAssert $x == """<a href="http://nim-lang.org">Nim rules.</a>"""
  10. x = <>outer(<>inner())
  11. doAssert $x == """<outer>
  12. <inner />
  13. </outer>"""
  14. x = <>outer(<>middle(<>inner1(), <>inner2(), <>inner3(), <>inner4()))
  15. doAssert $x == """<outer>
  16. <middle>
  17. <inner1 />
  18. <inner2 />
  19. <inner3 />
  20. <inner4 />
  21. </middle>
  22. </outer>"""
  23. x = <>l0(<>l1(<>l2(<>l3(<>l4()))))
  24. doAssert $x == """<l0>
  25. <l1>
  26. <l2>
  27. <l3>
  28. <l4 />
  29. </l3>
  30. </l2>
  31. </l1>
  32. </l0>"""
  33. x = <>l0(<>l1p1(), <>l1p2(), <>l1p3())
  34. doAssert $x == """<l0>
  35. <l1p1 />
  36. <l1p2 />
  37. <l1p3 />
  38. </l0>"""
  39. x = <>l0(<>l1(<>l2p1(), <>l2p2()))
  40. doAssert $x == """<l0>
  41. <l1>
  42. <l2p1 />
  43. <l2p2 />
  44. </l1>
  45. </l0>"""
  46. x = <>l0(<>l1(<>l2_1(), <>l2_2(<>l3_1(), <>l3_2(), <>l3_3(<>l4_1(), <>l4_2(), <>l4_3())), <>l2_3(), <>l2_4()))
  47. doAssert $x == """<l0>
  48. <l1>
  49. <l2_1 />
  50. <l2_2>
  51. <l3_1 />
  52. <l3_2 />
  53. <l3_3>
  54. <l4_1 />
  55. <l4_2 />
  56. <l4_3 />
  57. </l3_3>
  58. </l2_2>
  59. <l2_3 />
  60. <l2_4 />
  61. </l1>
  62. </l0>"""
  63. let
  64. innermost = newElement("innermost")
  65. middle = newXmlTree("middle", [innermost])
  66. innermost.add newText("innermost text")
  67. x = newXmlTree("outer", [middle])
  68. doAssert $x == """<outer>
  69. <middle>
  70. <innermost>innermost text</innermost>
  71. </middle>
  72. </outer>"""
  73. x = newElement("myTag")
  74. x.add newText("my text")
  75. x.add newElement("sonTag")
  76. x.add newEntity("my entity")
  77. doAssert $x == "<myTag>my text<sonTag />&my entity;</myTag>"
  78. block: # bug #21290
  79. let x = newXmlTree("foo",[
  80. newXmlTree("bar",[
  81. newText("Hola"),
  82. newXmlTree("qux",[
  83. newXmlTree("plugh",[])
  84. ])
  85. ])
  86. ])
  87. let s = $x
  88. doAssert $parseXml(s) == s
  89. doAssert s == """<foo>
  90. <bar>Hola<qux> <plugh /> </qux></bar>
  91. </foo>"""
  92. block: #21541
  93. let root = <>root()
  94. root.add <>child(newText("hello"))
  95. root.add <>more(newVerbatimText("hola"))
  96. let s = $root
  97. doAssert s == """<root>
  98. <child>hello</child>
  99. <more>hola</more>
  100. </root>"""
  101. let temp = newVerbatimText("Hello!")
  102. doAssert temp.text == "Hello!"
  103. temp.text = "Hola!"
  104. doAssert temp.text == "Hola!"