Преглед на файлове

Only remove text content when setting new content. (#37)

Brucey преди 7 месеца
родител
ревизия
ea6c43f05b
променени са 2 файла, в които са добавени 200 реда и са изтрити 9 реда
  1. 16 9
      xml.mod/glue.c
  2. 184 0
      xml.mod/tests/test.bmx

+ 16 - 9
xml.mod/glue.c

@@ -172,15 +172,22 @@ int bmx_mxmlSaveStdout(mxml_node_t * node, int format) {
 }
 
 void bmx_mxmlSetContent(mxml_node_t * node, BBString * content) {
-	mxml_node_t * child = mxmlGetFirstChild(node);
-	while (child != NULL) {
-		mxml_node_t * next = mxmlGetNextSibling(child);
-		mxmlDelete(child);
-		child = next;
-	}
-	char * c = (char*)bbStringToUTF8String(content);
-	mxmlNewText(node, 0, c);
-	bbMemFree(c);
+    mxml_node_t * child = mxmlGetFirstChild(node);
+
+    while (child != NULL) {
+        mxml_node_t * next = mxmlGetNextSibling(child);
+
+        mxml_type_t type = mxmlGetType(child);
+        if (type == MXML_TEXT || type == MXML_INTEGER || type == MXML_OPAQUE || type == MXML_REAL || type == MXML_CUSTOM) {
+            mxmlDelete(child);
+        }
+
+        child = next;
+    }
+
+    char * c = bbStringToUTF8String(content);
+    mxmlNewText(node, 0, c);
+    bbMemFree(c);
 }
 
 BBString * bmx_mxmlSaveString(mxml_node_t * node, int format) {

+ 184 - 0
xml.mod/tests/test.bmx

@@ -0,0 +1,184 @@
+SuperStrict
+
+Framework brl.standardio
+Import Text.XML
+Import BRL.MaxUnit
+
+New TTestSuite.run()
+
+Type TXmlTest Extends TTest
+
+	Global BASIC_XML:String = """
+		<book>
+			<title>Animal Farm</title>
+			<author>George Orwell</author>
+			<year>1945</year>
+		</book>
+		"""
+
+	Global BASIC_XML_WITH_DECLARATION:String = """
+		<?xml version="1.0" encoding="UTF-8"?>
+		<book>
+			<title>Animal Farm</title>
+			<author>George Orwell</author>
+			<year>1945</year>
+		</book>
+		"""
+
+	Method testGetRootElement_withoutDeclaration() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc(BASIC_XML)
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		AssertEquals("book", root.getName())
+
+		doc.Free()
+	End Method
+
+	Method testGetRootElement_withDeclaration() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc(BASIC_XML_WITH_DECLARATION)
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		AssertEquals("book", root.getName())
+
+		doc.Free()
+	End Method
+
+	Method testSetRootElement_withoutDeclaration() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc(BASIC_XML)
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		Local newRoot:TXmlNode = TXmlNode.newNode("newRoot")
+
+		Local oldRoot:TxmlNode = doc.setRootElement(newRoot)
+
+		root = doc.getRootElement()
+
+		AssertEquals("book", oldRoot.getName())
+		AssertEquals("newRoot", root.getName())
+
+		oldRoot.Free()
+		doc.Free()
+	End Method
+
+	Method testSetRootElement_withDeclaration() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc(BASIC_XML_WITH_DECLARATION)
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		Local newRoot:TXmlNode = TXmlNode.newNode("newRoot")
+
+		Local oldRoot:TxmlNode = doc.setRootElement(newRoot)
+
+		root = doc.getRootElement()
+
+		AssertEquals("book", oldRoot.getName())
+		AssertEquals("newRoot", root.getName())
+
+		oldRoot.Free()
+		doc.Free()
+	End Method
+
+	Method testToString() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc(BASIC_XML)
+
+		AssertEquals("""
+		<?xml version="1.0" encoding="utf-8"?><book>
+			<title>Animal Farm</title>
+			<author>George Orwell</author>
+			<year>1945</year>
+		</book>
+		""", doc.ToString())
+
+		doc.Free()
+	End Method
+
+	Method testSetContent_Elements() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc("""
+		<parent>
+    		<child>Text Content</child>
+			Middle
+    		<child2>Another Text</child2>
+		</parent>
+		""")
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		root.setContent("New Content")
+
+		AssertEquals("""
+		<?xml version="1.0" encoding="utf-8"?><parent><child>Text Content</child><child2>Another Text</child2>New Content</parent>
+		""", doc.ToString())
+
+		doc.Free()
+	End Method
+
+	Method testSetContent_TextElementComment() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc("""
+		<parent>
+    		<!-- This is a comment -->
+			Text
+		    <child>Text Content</child>
+    		Another Text Node
+		</parent>
+		""")
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		root.setContent("New Content")
+
+		AssertEquals("""
+		<?xml version="1.0" encoding="utf-8"?><parent><!-- This is a comment --><child>Text Content</child>New Content</parent>
+		""", doc.ToString())
+
+		doc.Free()
+	End Method
+
+	Method testSetContent_ElementNumberText() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc("""
+		<parent>
+    		<value>123</value>
+    		456.789
+    		OpaqueString
+		</parent>
+		""")
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		root.setContent("New Content")
+
+		AssertEquals("""
+		<?xml version="1.0" encoding="utf-8"?><parent><value>123</value>New Content</parent>
+		""", doc.ToString())
+
+		doc.Free()
+	End Method
+
+	Method testSetContent_Empty() {test}
+
+		Local doc:TXmlDoc = TXmlDoc.readDoc("""
+		<parent></parent>
+		""")
+
+		Local root:TXmlNode = doc.getRootElement()
+
+		root.setContent("New Content")
+
+		AssertEquals("""
+		<?xml version="1.0" encoding="utf-8"?><parent>New Content</parent>
+		""", doc.ToString())
+
+		doc.Free()
+	End Method
+End Type
+