Click here to Skip to main content
11,706,639 members (36,800 online)
Click here to Skip to main content
Add your own
alternative version

Romeo and Juliet

, 3 Dec 2011 CPOL 25.6K 351 66
Making relationships first class citizens.
XTreePropertyGrid.zip
XTreePropertyGrid
Clifton.Tools.Strings
bin
Debug
Clifton.Tools.Strings.dll
Release
Xml Documentation
obj
Debug
Refactor
TempPE
Xml Documentation
TempPE
UnitTests
Clifton.Windows.Forms
bin
Debug
Clifton.Tools.Strings.dll
Clifton.Windows.Forms.dll
Release
obj
Debug
Refactor
TempPE
Properties
Resources
tv_minus.bmp
tv_plus.bmp
XmlTree
.svn
all-wcprops
entries
prop-base
props
text-base
INode.cs.svn-base
IXtreeNode.cs.svn-base
Node.cs.svn-base
NodeDef.cs.svn-base
NodeInstance.cs.svn-base
NodeMenuItem.cs.svn-base
PlaceholderInstance.cs.svn-base
Popup.cs.svn-base
RootNode.cs.svn-base
XtreeNodeController.cs.svn-base
tmp
prop-base
props
text-base
Myxaml 2.0
MyXaml.Core
bin
Debug
Clifton.Tools.Strings.dll
MyXaml.Core.dll
UnitTest.dll
Release
Xml Documentation
doc
MyXamlCoreUnitTests
obj
Debug
Refactor
TempPE
Release
Xml Documentation
TempPE
MyXaml.WinForms
bin
Debug
Clifton.Tools.Strings.dll
MyXaml.Core.dll
MyXaml.WinForms.dll
UnitTest.dll
Release
Xml Documentation
obj
Debug
Refactor
TempPE
Xml Documentation
TempPE
ROPLib
bin
Debug
ROPLib.dll
XTreeInterfaces.dll
Release
obj
Debug
Refactor
TempPE
Properties
ROPLib.csproj.user
UnitTest
bin
Debug
UnitTest.dll
Release
Xml Documentation
obj
Debug
Refactor
TempPE
Xml Documentation
TempPE
UnitTest.csproj.user
XTreeIIDemo
bin
Debug
Clifton.Tools.Strings.dll
Clifton.Windows.Forms.dll
MyXaml.Core.dll
MyXaml.WinForms.dll
ROPLib.dll
schemaEditor.myxaml
UnitTest.dll
XTreeIIDemo.exe
XTreeIIDemo.vshost.exe
XTreeIIDemo.vshost.exe.manifest
XTreeInterfaces.dll
Properties
XTreeInterfaces
bin
Debug
XTreeInterfaces.dll
Release
obj
Debug
TempPE
Properties
using System;
using System.ComponentModel;
using System.Xml;

using Vts.UnitTest;

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

namespace MyXamlCoreUnitTests
{
	public struct AStruct
	{
		private int a;
		private string b;

		public int Num
		{
			get {return a;}
			set {a=value;}
		}

		public string Str
		{
			get {return b;}
			set {b=value;}
		}
	}

	public class Provider : IExtenderProvider
	{
		protected object obj;
		protected string name;
		protected string text;

		public object Object
		{
			get {return obj;}
		}

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

		public string Text
		{
			get {return text;}
		}

		public void SetText(object obj, string text)
		{
			this.obj=obj;
			this.text=text;
		}

		public bool CanExtend(object extendee)
		{
			return extendee is APropertyTestClass;
		}
	}

	public class DynamicClass
	{
		protected string name;
		protected string text;

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

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

		public override string ToString()
		{
			return text;
		}

	}

	public class APropertyTestClass
	{
		protected string name;
		protected string val;
		protected string text;
		protected string[] strArray;
		protected int[] intArray;
		protected AStruct myStruct;

		protected Type myType;

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

		public string ReadOnlyValue
		{
			get {return val;}
		}

		public string ReadWriteValue
		{
			get {return val;}
			set {val=value;}
		}

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

		public string[] StringArray
		{
			get {return strArray;}
			set {strArray=value;}
		}

		public int[] IntArray
		{
			get {return intArray;}
			set {intArray=value;}
		}

		public Type MyType
		{
			get {return myType;}
			set {myType=value;}
		}

		public AStruct MyStruct
		{
			get {return myStruct;}
			set {myStruct=value;}
		}

		public APropertyTestClass()
		{
		}
	}

	[TestFixture]
	public class PropertyAssignmentTests
	{
		protected Parser parser;

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

		[Test]
		[ExpectedException(typeof(UnknownPropertyException))]
		public void MissingPropertyTest()
		{
			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+="  <APropertyTestClass NoProperty='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		[ExpectedException(typeof(ReadOnlyInstanceNullException))]
		public void ReadOnlyPropertyTest()
		{
			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+="  <APropertyTestClass ReadOnlyValue='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		public void ReadWritePropertyTest()
		{
			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+="  <APropertyTestClass ReadWriteValue='Foo'/>\r\n";
			xml+="</MyXaml>\r\n";
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.ReadWriteValue=="Foo", "Unexpected result.");
		}

		[Test]
		public void ReferenceTest()
		{
			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+="  <APropertyTestClass ReadWriteValue='{SomeVal}'/>\r\n";
			xml+="</MyXaml>\r\n";

			parser.AddReference("SomeVal", "Marc");
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.ReadWriteValue=="Marc", "Unexpected result.");
		}

		[Test]
		public void InnerTextTest()
		{
			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+="  <APropertyTestClass>Marc</APropertyTestClass>\r\n";
			xml+="</MyXaml>\r\n";

			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.Text=="Marc", "Unexpected result.");
		}

		[Test]
		[ExpectedException(typeof(ForwardReferenceException))]
		public void EmbeddedReferenceFailureTest()
		{
			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+="  <APropertyTestClass Text='M{text}c'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		public void EmbeddedReferenceTest()
		{
			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+="  <APropertyTestClass Text='M{text}c'/>\r\n";
			xml+="</MyXaml>\r\n";

			parser.AddReference("text", "ar");
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.Text=="Marc", "Unexpected result.");
		}

		[Test]
		public void ArrayTest()
		{
			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+="  <APropertyTestClass StringArray='a, b, c'/>\r\n";
			xml+="</MyXaml>\r\n";
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.StringArray.Length==3, "Unexpected result.");
			Assertion.Assert(tc.StringArray[0]=="a", "Unexpected result.");
			Assertion.Assert(tc.StringArray[1]=="b", "Unexpected result.");
			Assertion.Assert(tc.StringArray[2]=="c", "Unexpected result.");
		}

		[Test]
		[ExpectedException(typeof(ArrayConversionException))]
		public void ArrayAssignmentFailureTest()
		{
			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+="  <APropertyTestClass IntArray='a, b, c'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		public void TypeAssignmentTest()
		{
			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+="  <APropertyTestClass MyType='System.Int32'/>\r\n";
			xml+="</MyXaml>\r\n";
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.MyType==typeof(System.Int32), "Unexpected result.");
		}

		[Test]
		[ExpectedException(typeof(TypeAssignmentException))]
		public void TypeAssignmentFailureTest()
		{
			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+="  <APropertyTestClass MyType='NoType'/>\r\n";
			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
		}

		[Test]
		public void LateBindingAssignment()
		{
			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+="  <APropertyTestClass ReadWriteValue='{lateBound.Text}'>\r\n";
			xml+="    <APropertyTestClass def:Name='lateBound' Text='Marc'/>\r\n";
			xml+="  </APropertyTestClass>\r\n";
			xml+="</MyXaml>\r\n";
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.ReadWriteValue=="Marc", "Unexpected result.");
		}

		[Test]
		public void ReferencePropertyAssignment()
		{
			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+="  <APropertyTestClass ReadWriteValue='{dynamic;Text=Marc}'>\r\n";
			xml+="    <DynamicClass def:Name='dynamic'/>\r\n";
			xml+="  </APropertyTestClass>\r\n";

			xml+="</MyXaml>\r\n";
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.ReadWriteValue=="Marc", "Unexpected result.");
		}

		[Test]
		public void ExtenderProviderTest()
		{
			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+="  <APropertyTestClass ExtendedProperty='{provider; SetText=Marc}'>\r\n";
			xml+="    <Provider def:Name='provider'/>\r\n";
			xml+="  </APropertyTestClass>\r\n";

			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
			Provider p=(Provider)parser.GetReference("provider");
			Assertion.Assert(p.Object is APropertyTestClass, "Object is unexpected type.");
			Assertion.Assert(p.Text=="Marc", "Text is unexpected value.");
		}

		[Test]
		[ExpectedException(typeof(ExtenderProviderException))]
		public void ExtenderProviderFailTest()
		{
			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+="  <DynamicClass ExtendedProperty='{provider; SetText=Marc}'>\r\n";
			xml+="    <Provider def:Name='provider'/>\r\n";
			xml+="  </DynamicClass>\r\n";

			xml+="</MyXaml>\r\n";
			parser.InstantiateFromString(xml, "*");
			Provider p=(Provider)parser.GetReference("provider");
			Assertion.Assert(p.Object is APropertyTestClass, "Object is unexpected type.");
			Assertion.Assert(p.Text=="Marc", "Text is unexpected value.");
		}

		[Test]
		public void StructTest()
		{
			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+="  <APropertyTestClass>\r\n";
			xml+="    <MyStruct>\r\n";
			xml+="      <AStruct Num='1' Str='Foo'/>\r\n";
			xml+="    </MyStruct>\r\n";
			xml+="  </APropertyTestClass>\r\n";

			xml+="</MyXaml>\r\n";
			APropertyTestClass tc=(APropertyTestClass)parser.InstantiateFromString(xml, "*");
			Assertion.Assert(tc.MyStruct.Num==1, "a not assigned.");
			Assertion.Assert(tc.MyStruct.Str=="Foo", "b not assigned.");
		}
	}
}

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)

Share

About the Author

Marc Clifton
United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 3 Dec 2011
Article Copyright 2011 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid