Click here to Skip to main content
15,884,629 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.ComponentModel;
using System.Xml;

using Vts.UnitTest;

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

namespace MyXamlCoreUnitTests
{
	public class Item
	{
		protected string text;

		public string Text
		{
			get {return text;}
			set {text=value;}
		}

		public Item()
		{
		}
	}

	public class Item2
	{
		protected string text;

		public string Text
		{
			get {return text;}
			set {text=value;}
		}
	}

	public class ChildTest
	{
	}

	public class ObjectGraphTestClass
	{
		protected string name;
		protected Item[] items;
		protected ChildTest childTest;

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

		public Item[] Items
		{
			get {return items;}
			set {items=value;}
		}

		public ChildTest MyChild
		{
			get {return childTest;}
			set {childTest=value;}
		}
	}

	[TestFixture]
	public class ObjectGraphTests
	{
		protected Parser parser;

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

		[Test]
		public void PropertyArrayInitializationTest()
		{
			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+="  <ObjectGraphTestClass>\r\n";
			xml+="    <Items>\r\n";
			xml+="      <Item Text='a'/>\r\n";
			xml+="      <Item Text='b'/>\r\n";
			xml+="      <Item Text='c'/>\r\n";
			xml+="    </Items>\r\n";
			xml+="  </ObjectGraphTestClass>\r\n";

			xml+="</MyXaml>\r\n";

			ObjectGraphTestClass tc=(ObjectGraphTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.Items[0].Text=="a", "Unexpected result.");
			Assertion.Assert(tc.Items[1].Text=="b", "Unexpected result.");
			Assertion.Assert(tc.Items[2].Text=="c", "Unexpected result.");
		}

		[Test]
		[ExpectedException(typeof(ArrayConversionException))]
		public void PropertyArrayInitializationFailureTest()
		{
			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+="  <ObjectGraphTestClass>\r\n";
			xml+="    <Items>\r\n";
			xml+="      <Item2 Text='a'/>\r\n";
			xml+="      <Item2 Text='b'/>\r\n";
			xml+="      <Item2 Text='c'/>\r\n";
			xml+="    </Items>\r\n";
			xml+="  </ObjectGraphTestClass>\r\n";

			xml+="</MyXaml>\r\n";

			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		public void ClassPropertyClassTest()
		{
			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+="  <ObjectGraphTestClass>\r\n";
			xml+="    <MyChild>\r\n";
			xml+="      <ChildTest/>\r\n";
			xml+="    </MyChild>\r\n";
			xml+="  </ObjectGraphTestClass>\r\n";

			xml+="</MyXaml>\r\n";

			ObjectGraphTestClass tc=(ObjectGraphTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.MyChild != null, "Unexpected result.");
		}

		[Test]
		[ExpectedException(typeof(ChildTypeNotPropertyTypeException))]
		public void ClassPropertyNotKindOfTest()
		{
			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+="  <ObjectGraphTestClass>\r\n";
			xml+="    <MyChild>\r\n";
			xml+="      <Item/>\r\n";
			xml+="    </MyChild>\r\n";
			xml+="  </ObjectGraphTestClass>\r\n";

			xml+="</MyXaml>\r\n";

			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		[ExpectedException(typeof(ExpectedSingleChildException))]
		public void MultipleChildrenForAPropertyTest()
		{
			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+="  <ObjectGraphTestClass>\r\n";
			xml+="    <MyChild>\r\n";
			xml+="      <ChildTest/>\r\n";
			xml+="      <ChildTest/>\r\n";
			xml+="    </MyChild>\r\n";
			xml+="  </ObjectGraphTestClass>\r\n";

			xml+="</MyXaml>\r\n";

			parser.InstantiateFromString(xml, "*");
		}

		// ICollection 
		// IList

		// Abstract property
		// Interface implemented by child

	}
}

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