[Mono-devel-list] Patch to use ExpectedException in Test XmlTextWriterTests

Martin Willemoes Hansen mwh at sysrq.dk
Wed Jul 2 09:20:08 EDT 2003


Hi!

I changed mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests to
make use of the ExpectedExceptionAttribute

Okay to commit?
-- 
Martin Willemoes Hansen

--------------------------------------------------------
E-Mail	mwh at sysrq.dk	Website	mwh.sysrq.dk
IRC     MWH, freenode.net
--------------------------------------------------------               

-------------- next part --------------
? XmlTextWriterTests.cs.diff
Index: XmlTextWriterTests.cs
===================================================================
RCS file: /cvs/public/mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs,v
retrieving revision 1.5
diff -u -r1.5 XmlTextWriterTests.cs
--- XmlTextWriterTests.cs	15 Jun 2003 18:09:46 -0000	1.5
+++ XmlTextWriterTests.cs	1 Jul 2003 19:41:38 -0000
@@ -92,13 +92,13 @@
 		public void AttributeNamespacesXmlnsXmlns ()
 		{
 			xtw.WriteStartElement ("foo");
-			try {
-				xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
-				// This should not be allowed, even though
-				// MS.NET doesn't treat as an error.
-				Fail ("any prefix which name starts from \"xml\" must not be allowed.");
-			}
- 			catch (ArgumentException e) {}
+			try {
+				xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
+				// This should not be allowed, even though
+				// MS.NET doesn't treat as an error.
+				Fail ("any prefix which name starts from \"xml\" must not be allowed.");
+			}
+ 			catch (ArgumentException e) {}
 			xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
 		}
 
@@ -142,17 +142,13 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
 		public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
 		{
 			xtw.WriteStartElement ("foo");
 			xtw.WriteString ("bar");
 			
-			try 
-			{
-				xtw.WriteAttributeString ("baz", "quux");
-				Fail ("Expected an InvalidOperationException to be thrown.");
-			} 
-			catch (InvalidOperationException) {}
+			xtw.WriteAttributeString ("baz", "quux");
 		}
 
 		[Test]
@@ -173,13 +169,10 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void CDataInvalid ()
 		{
-			try {
-				xtw.WriteCData("foo]]>bar");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
+			xtw.WriteCData("foo]]>bar");
 		}
 
 		[Test]
@@ -263,19 +256,11 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void CommentInvalid ()
 		{
-			try {
-				xtw.WriteComment("foo-");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
-
-			try {
-				xtw.WriteComment("foo-->bar");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
+			xtw.WriteComment("foo-");
+			xtw.WriteComment("foo-->bar");
 		}
 
 		[Test]
@@ -333,23 +318,13 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
 		public void DocumentStart ()
 		{
 			xtw.WriteStartDocument ();
 			AssertEquals ("XmlDeclaration is incorrect.", "<?xml version='1.0' encoding='utf-16'?>", StringWriterText);
 
-			try 
-			{
-				xtw.WriteStartDocument ();
-				Fail("Should have thrown an InvalidOperationException.");
-			} 
-			catch (InvalidOperationException e) {
-				// Don't rely on English message assertion.
-				// It is enough to check an exception occurs.
-//				AssertEquals ("Exception message is incorrect.",
-//					"WriteStartDocument should be the first call.", e.Message);
-			}
-
+			xtw.WriteStartDocument ();
 			xtw = new XmlTextWriter (sw = new StringWriter ());
 			xtw.QuoteChar = '\'';
 			xtw.WriteStartDocument (true);
@@ -471,6 +446,7 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void NamespacesElementsPassingInNamespaces ()
 		{
 			xtw.Namespaces = false;
@@ -490,35 +466,12 @@
 
 			// These should throw ArgumentException because they pass in a
 			// namespace when Namespaces = false.
-			try {
-				xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
-
-			try {
-				xtw.WriteStartElement ("foo", "http://netsack.com/");
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
-
-			try {
-				xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
-
-			try {
-				xtw.WriteStartElement ("foo", "bar", null);
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
-
-			try {
-				xtw.WriteStartElement ("foo", "bar", "");
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
-
-			try {
-				xtw.WriteStartElement ("foo", "", "");
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
+			xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
+			xtw.WriteStartElement ("foo", "http://netsack.com/");
+			xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
+			xtw.WriteStartElement ("foo", "bar", null);
+			xtw.WriteStartElement ("foo", "bar", "");
+			xtw.WriteStartElement ("foo", "", "");
 		}
 
 		[Test]
@@ -546,31 +499,19 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void NamespacesPrefixWithEmptyAndNullNamespace ()
 		{
-			try {
-				xtw.WriteStartElement ("foo", "bar", "");
-				Fail ("Should have thrown an ArgumentException.");
-			} catch (ArgumentException) {}
-
-			try 
-			{
-				xtw.WriteStartElement ("foo", "bar", null);
-				Fail ("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) {}
+			xtw.WriteStartElement ("foo", "bar", "");
+			xtw.WriteStartElement ("foo", "bar", null);
 		}
 
 		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
 		public void NamespacesSettingWhenWriteStateNotStart ()
 		{
 			xtw.WriteStartElement ("foo");
-			try 
-			{
-				xtw.Namespaces = false;
-				Fail ("Expected an InvalidOperationException.");
-			} 
-			catch (InvalidOperationException) {}
+			xtw.Namespaces = false;
 			AssertEquals (true, xtw.Namespaces);
 		}
 
@@ -582,35 +523,13 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void ProcessingInstructionInvalid ()
 		{
-			try 
-			{
-				xtw.WriteProcessingInstruction("fo?>o", "bar");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
-
-			try 
-			{
-				xtw.WriteProcessingInstruction("foo", "ba?>r");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
-
-			try 
-			{
-				xtw.WriteProcessingInstruction("", "bar");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
-
-			try 
-			{
-				xtw.WriteProcessingInstruction(null, "bar");
-				Fail("Should have thrown an ArgumentException.");
-			} 
-			catch (ArgumentException) { }
+			xtw.WriteProcessingInstruction("fo?>o", "bar");
+			xtw.WriteProcessingInstruction("foo", "ba?>r");
+			xtw.WriteProcessingInstruction("", "bar");
+			xtw.WriteProcessingInstruction(null, "bar");
 		}
 
 		[Test]
@@ -630,12 +549,10 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void QuoteCharInvalid ()
 		{
-			try {
-				xtw.QuoteChar = 'x';
-				Fail ("Should have thrown an ArgumentException.");
-			} catch (ArgumentException) {}
+			xtw.QuoteChar = 'x';
 		}
 
 		[Test]
@@ -668,6 +585,7 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void WriteCharEntity ()
 		{
 			xtw.WriteCharEntity ('a');
@@ -682,38 +600,23 @@
 			xtw.WriteCharEntity ('K');
 			AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
 
-			try {
-				xtw.WriteCharEntity ((char)0xd800);
-			} catch (ArgumentException) {}
+			xtw.WriteCharEntity ((char)0xd800);
 		}
 
 		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
 		public void WriteEndAttribute ()
 		{
-			try 
-			{
-				xtw.WriteEndAttribute ();
-				Fail ("Should have thrown an InvalidOperationException.");
-			}
-			catch (InvalidOperationException) {}
+			xtw.WriteEndAttribute ();
 		}
 
 		[Test]
+		[ExpectedException (typeof (ArgumentException))]
 		public void WriteEndDocument ()
 		{
-			try {
-				xtw.WriteEndDocument ();
-				Fail ("Expected an ArgumentException.");
-			} catch (ArgumentException) {}
-
+			xtw.WriteEndDocument ();
 			xtw.WriteStartDocument ();
-
-			try 
-			{
-				xtw.WriteEndDocument ();
-				Fail ("Expected an ArgumentException.");
-			} 
-			catch (ArgumentException) {}
+			xtw.WriteEndDocument ();
 
 			xtw.WriteStartElement ("foo");
 			xtw.WriteStartAttribute ("bar", null);
@@ -725,16 +628,13 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
 		public void WriteEndElement ()
 		{
-			try {
-				xtw.WriteEndElement ();
-				Fail ("Should have thrown an InvalidOperationException.");
-			} catch (InvalidOperationException e) {
-				// Don't rely on English message assertion.
-				// It is enough to check an exception occurs.
-//				AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
-			}
+			xtw.WriteEndElement ();
+			// Don't rely on English message assertion.
+			// It is enough to check an exception occurs.
+			// AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
 
 			xtw.WriteStartElement ("foo");
 			xtw.WriteEndElement ();
@@ -814,12 +714,11 @@
 		}
 
 		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
 		public void WriteString ()
 		{
 			xtw.WriteStartDocument ();
-			try {
-				xtw.WriteString("foo");
-			} catch (InvalidOperationException) {}
+			xtw.WriteString("foo");
 
 			// Testing attribute values
 
@@ -1039,34 +938,34 @@
 		[Test]
 		public void WriteWhitespace ()
 		{
-			xtw.WriteStartElement ("a");
-			xtw.WriteWhitespace ("\n\t");
-			xtw.WriteStartElement ("b");
-			xtw.WriteWhitespace ("\n\t");
-			xtw.WriteEndElement ();
-			xtw.WriteWhitespace ("\n");
-			xtw.WriteEndElement ();
-			xtw.WriteWhitespace ("\n");
-			xtw.Flush ();
+			xtw.WriteStartElement ("a");
+			xtw.WriteWhitespace ("\n\t");
+			xtw.WriteStartElement ("b");
+			xtw.WriteWhitespace ("\n\t");
+			xtw.WriteEndElement ();
+			xtw.WriteWhitespace ("\n");
+			xtw.WriteEndElement ();
+			xtw.WriteWhitespace ("\n");
+			xtw.Flush ();
 			AssertEquals ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
 		}
 
-		[Test]
-		public void FlushDoesntCloseTag ()
-		{
-			xtw.WriteStartElement ("foo");
-			xtw.WriteAttributeString ("bar", "baz");
-			xtw.Flush ();
-			AssertEquals ("<foo bar='baz'", StringWriterText);
-		}
-
-		[Test]
-		public void WriteWhitespaceClosesTag ()
-		{
-			xtw.WriteStartElement ("foo");
-			xtw.WriteAttributeString ("bar", "baz");
-			xtw.WriteWhitespace (" ");
-			AssertEquals ("<foo bar='baz'> ", StringWriterText);
-		}
+		[Test]
+		public void FlushDoesntCloseTag ()
+		{
+			xtw.WriteStartElement ("foo");
+			xtw.WriteAttributeString ("bar", "baz");
+			xtw.Flush ();
+			AssertEquals ("<foo bar='baz'", StringWriterText);
+		}
+
+		[Test]
+		public void WriteWhitespaceClosesTag ()
+		{
+			xtw.WriteStartElement ("foo");
+			xtw.WriteAttributeString ("bar", "baz");
+			xtw.WriteWhitespace (" ");
+			AssertEquals ("<foo bar='baz'> ", StringWriterText);
+		}
 	}
 }


More information about the Mono-devel-list mailing list