Bladeren bron

2003-05-20 Atsushi Enomoto <[email protected]>

	* added DeserializeTests.cs and XmlAttributesTest.cs

svn path=/trunk/mcs/; revision=14714
Atsushi Eno 22 jaren geleden
bovenliggende
commit
c0645bd71d

+ 4 - 0
mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog

@@ -1,3 +1,7 @@
+2003-05-20  Atsushi Enomoto <[email protected]>
+
+	* added DeserializeTests.cs and XmlAttributesTest.cs
+
 2003-05-18  Atsushi Enomoto <[email protected]>
 
 	* XmlSerializerTests.cs : fixed some AssertEquals() argument orders.

+ 70 - 0
mcs/class/System.XML/Test/System.Xml.Serialization/DeserializeTests.cs

@@ -0,0 +1,70 @@
+//
+// System.Xml.DeserializationTests
+//
+// Author:
+//	Atsushi Enomoto <[email protected]>
+//
+// (C) 2003 Atsushi Enomoto
+//
+//
+using System;
+using System.IO;
+using System.Xml;
+using System.Xml.Serialization;
+using NUnit.Framework;
+
+namespace MonoTests.System.Xml
+{
+	public class Sample
+	{
+		public string Text;
+		public string [] ArrayText;
+	}
+
+	[TestFixture]
+	public class DeserializationTests
+	{
+		object result;
+
+		private void Deserialize (Type t, string xml)
+		{
+			StringReader sr = new StringReader (xml);
+			XmlReader xr = new XmlTextReader (sr);
+			Deserialize (t, xr);
+		}
+
+		private void Deserialize (Type t, XmlReader xr)
+		{
+			XmlSerializer ser = new XmlSerializer (t);
+			result = ser.Deserialize (xr);
+		}
+
+		[Test]
+		public void SimpleDeserialize ()
+		{
+			Deserialize (typeof (Sample), "<Sample><Text>Test.</Text></Sample>");
+			Assertion.AssertEquals (typeof (Sample), result.GetType ());
+			Sample sample = result as Sample;
+			Assertion.AssertEquals ("Test.", sample.Text);
+		}
+
+		[Test]
+		public void DeserializeInt ()
+		{
+			Deserialize (typeof (int), "<int>10</int>");
+			Assertion.AssertEquals (typeof (int), result.GetType ());
+			Assertion.AssertEquals (10, result);
+		}
+
+		[Test]
+		public void DeserializeSimpleArray ()
+		{
+			Deserialize (typeof (Sample), "<Sample><ArrayText><string>Test1</string><string>Test2</string></ArrayText></Sample>");
+			Assertion.AssertEquals (typeof (Sample), result.GetType ());
+			Sample sample = result as Sample;
+			Assertion.AssertEquals ("Test1", sample.ArrayText [0]);
+			Assertion.AssertEquals ("Test2", sample.ArrayText [1]);
+		}
+
+	}
+}

+ 117 - 0
mcs/class/System.XML/Test/System.Xml.Serialization/XmlAttributesTests.cs

@@ -0,0 +1,117 @@
+//
+// System.Xml.XmlAttributesTests
+//
+// Author:
+//   Atsushi Enomoto
+//
+// (C) 2003 Atsushi Enomoto
+//
+
+using System;
+using System.IO;
+using System.Text;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.Serialization;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Xml
+{
+	[TestFixture]
+	public class XmlAttributesTests : Assertion
+	{
+		StringWriter sw;
+		XmlTextWriter xtw;
+		XmlSerializer xs;
+
+		private void SetUpWriter ()
+		{
+			sw = new StringWriter ();
+			xtw = new XmlTextWriter (sw);
+			xtw.QuoteChar = '\'';
+			xtw.Formatting = Formatting.None;
+		}
+		
+		private string WriterText 
+		{
+			get
+			{
+				string val = sw.GetStringBuilder ().ToString();
+				int offset = val.IndexOf ('>') + 1;
+				val = val.Substring (offset);
+				return val;
+			}
+		}
+
+		private void Serialize (object o, XmlAttributeOverrides ao)
+		{
+			SetUpWriter ();
+			xs = new XmlSerializer (o.GetType (), ao);
+			xs.Serialize (xtw, o);
+		}
+		
+		private void Serialize (object o, XmlRootAttribute root)
+		{
+			SetUpWriter ();
+			xs = new XmlSerializer (o.GetType(), root);
+			xs.Serialize (xtw, o);
+		}
+
+		// Testcases.
+
+		[Test]
+		public void NewXmlAttributes ()
+		{
+			// seems not different from Type specified ctor().
+			XmlAttributes atts = new XmlAttributes ();
+			AssertNull (atts.XmlAnyAttribute);
+			AssertNotNull (atts.XmlAnyElements);
+			AssertEquals (0, atts.XmlAnyElements.Count);
+			AssertNull (atts.XmlArray);
+			AssertNotNull (atts.XmlArrayItems);
+			AssertEquals (0, atts.XmlArrayItems.Count);
+			AssertNull (atts.XmlAttribute);
+			AssertNull (atts.XmlChoiceIdentifier);
+			AssertNotNull (atts.XmlDefaultValue);
+			// DBNull??
+			AssertEquals (DBNull.Value, atts.XmlDefaultValue);
+			AssertNotNull (atts.XmlElements);
+			AssertEquals (0, atts.XmlElements.Count);
+			AssertNull (atts.XmlEnum);
+			AssertNotNull (atts.XmlIgnore);
+			AssertEquals (TypeCode.Boolean, atts.XmlIgnore.GetTypeCode ());
+			AssertEquals (false, atts.Xmlns);
+			AssertNull (atts.XmlRoot);
+			AssertNull (atts.XmlText);
+			AssertNull (atts.XmlType);
+		}
+
+		[Test]
+		public void XmlTextAttribute ()
+		{
+			// based on default ctor.
+			XmlTextAttribute attr = new XmlTextAttribute ();
+			AssertEquals ("", attr.DataType);
+			AssertNull (attr.Type);
+			// based on a type.
+			XmlTextAttribute attr2 = new XmlTextAttribute (typeof (XmlNode));
+			AssertEquals ("", attr.DataType);
+			AssertNull (attr.Type);
+		}
+
+		[Test]
+		public void XmlInvalidElementAttribute ()
+		{
+			XmlAttributeOverrides ao = new XmlAttributeOverrides ();
+			XmlAttributes atts = new XmlAttributes ();
+			atts.XmlElements.Add (new XmlElementAttribute ("xInt"));
+			ao.Add (typeof (int), atts);
+			try {
+				Serialize (10, ao);
+				Fail ("Should be invalid.");
+			} catch (InvalidOperationException ex) {
+			}
+		}
+	}
+}