Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

Romeo and Juliet

Rate me:
Please Sign up or sign in to vote.
4.87/5 (34 votes)
3 Dec 2011CPOL11 min read 58.2K   413   67  
Making relationships first class citizens.
using System;
using System.Xml;

using Vts.UnitTest;

using MyXaml.Core;
using MyXaml.Core.Exceptions;

namespace ANamespace
{
	public class ATestClass
	{
		protected string name;

		public string Name
		{
			get {return name;}
			set {name=value;}
		}
	}
}

namespace MyXamlCoreUnitTests
{
	public class ATestClass
	{
		protected string name;

		public string Name
		{
			get {return name;}
			set {name=value;}
		}
	}

	public class NoDefaultConstructor
	{
		public NoDefaultConstructor(int foo)
		{
		}
	}

	[TestFixture]
	public class SimpleInstantiationTests
	{
		protected Parser parser;

		[SetUp]
		public void SetUp()
		{
			parser=new Parser();
		}

		[Test]
		[ExpectedException(typeof(XmlException))]
		public void BadXmlTest()
		{
			string badxml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			badxml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			badxml+="<MyXaml>\r\n";												// missing closing tag.
			parser.InstantiateFromString(badxml, "*");
		}

		[Test]
		[ExpectedException(typeof(MissingGraphException))]
		public void MissingGraphTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		[ExpectedException(typeof(GraphNotFoundException))]
		public void GraphNotFoundTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml>\r\n";
			xml+="  <Test Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "Bar");
		}

		[Test]
		[ExpectedException(typeof(NoReferenceException))]
		public void NoReferenceTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\">\r\n";
			xml+="  <Test ref:Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "Foo");
		}

		[Test]
		[ExpectedException(typeof(ImproperNamespaceFormatException))]
		public void ImproperNamespaceTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="xmlns=\"System.Windows.Forms, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral\">\r\n";
			xml+="  <Form Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "Foo");
		}

		[Test]
		[ExpectedException(typeof(MissingAssemblyException))]
		public void MissingAssemblyTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="xmlns=\"FooTest\">\r\n";
			xml+="  <Form Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "Foo");
		}

		[Test]
		public void ShortFormTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"MyXamlCoreUnitTests\">\r\n";
			xml+="  <ATestClass Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			object obj=parser.InstantiateFromString(xml, "Foo");
			Assertion.Assert(obj is ATestClass, "Unexpected result.");
		}

		[Test]
		public void MediumFormTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"ANamespace, MyXamlCoreUnitTests\">\r\n";
			xml+="  <ATestClass Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			object obj=parser.InstantiateFromString(xml, "Foo");
			Assertion.Assert(obj is ANamespace.ATestClass, "Unexpected result.");
		}

		[Test]
		public void LongFormTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"ANamespace, MyXamlCoreUnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=\">\r\n";
			xml+="  <ATestClass Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			object obj=parser.InstantiateFromString(xml, "Foo");
			Assertion.Assert(obj is ANamespace.ATestClass, "Unexpected result.");
		}

		[Test]
		[ExpectedException(typeof(UnknownTypeException))]
		public void UnkownTypeTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"MyXamlCoreUnitTests\">\r\n";
			xml+="  <MissingClass Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "Foo");
		}

		[Test]
		[ExpectedException(typeof(InstantiationException))]
		public void InstantiationErrorTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"MyXamlCoreUnitTests\">\r\n";
			xml+="  <NoDefaultConstructor Name='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "Foo");
		}

		[Test]
		public void WildcardTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"MyXamlCoreUnitTests\">\r\n";
			xml+="  <ATestClass/>\r\n";
			xml+="</MyXaml>\r\n";
			object obj=parser.InstantiateFromString(xml, "*");
			Assertion.Assert(obj is ATestClass, "Unexpected result.");

		}

		[Test]
		public void AddToReferenceCollectionTest()
		{
			string xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			xml+="<!-- (c) 2005 MyXaml  All Rights Reserved  -->\r\n";
			xml+="<MyXaml\r\n";
			xml+="  xmlns:def=\"Definition\"\r\n";
			xml+="  xmlns:ref=\"Reference\"\r\n";
			xml+="  xmlns=\"MyXamlCoreUnitTests\">\r\n";
			xml+="  <ATestClass def:Name='testClass'/>\r\n";
			xml+="</MyXaml>\r\n";
			object obj=parser.InstantiateFromString(xml, "*");
			Assertion.Assert(obj is ATestClass, "Result is not ATestClass.");
			Assertion.Assert(parser.ContainsReference("testClass"), "Expected reference.");
			Assertion.Assert(parser.GetReference("testClass") is ATestClass, "Reference is not ATestClass.");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions