Permalink
Browse files

Adding NotifyPropertyChangedModule implementation and tests. Updating…

… readme with help to pull dependencies and build.
  • Loading branch information...
idavis committed Nov 1, 2012
1 parent d05d1e7 commit 0a7e85da39403825e06381dcf81c45c091a06deb
View
@@ -1,2 +1,3 @@
Archetype
============
+To build, open PowerShell console in source directory. Run chewie.ps1. Then open the solution.
@@ -40,6 +40,7 @@
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="ModuleTestsForGetMember.cs" />
+ <Compile Include="NotifyPropertyChangesModuleTests.cs" />
<Compile Include="PrototypalObjectTestsExpandoObjectPrototypes.cs" />
<Compile Include="PrototypalObjectTestsInheritanceOrder.cs" />
<Compile Include="StaticTests\StaticMethodTests.cs" />
@@ -1,61 +1,60 @@
-using System.Collections;
-using System.Collections.Generic;
-using System.ComponentModel;
-using NUnit.Framework;
+using NUnit.Framework;
namespace Archetype.Tests
{
- public class NotifyPropertyChangedModule : INotifyPropertyChanged
+ public class Animal
{
- public event PropertyChangedEventHandler PropertyChanged;
-
- protected virtual void OnPropertyChanged(string propertyName)
+ public virtual string Name
{
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
+ get { return GetType().Name; }
}
}
- public class Animal
- {
- public virtual string Name { get { return GetType().Name; } }
- }
-
public class Cat : Animal
{
- public string Noise { get { return "Meow"; } }
+ public string Noise
+ {
+ get { return "Meow"; }
+ }
}
public class CheshireCat : DelegatingPrototype
{
public CheshireCat(Cat cat = null) : base(cat)
- {
+ {
}
- public string Action { get { return "Grin"; } }
+ public string Action
+ {
+ get { return "Grin"; }
+ }
}
[TestFixture]
public class ModuleTestsForGetMember
{
- [Test]
- public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValueWhenThereIsNoPrototype()
+ [Test]
+ public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValue()
{
- dynamic value = new CheshireCat();
+ dynamic value = new CheshireCat(new Cat());
Assert.AreEqual("Grin", value.Action);
}
+
[Test]
- public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValue()
+ public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValueWhenThereIsNoPrototype()
{
- dynamic value = new CheshireCat(new Cat());
+ dynamic value = new CheshireCat();
Assert.AreEqual("Grin", value.Action);
}
+
[Test]
- public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIsOnTheLastModule()
+ public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueAndIsReturnedBottomUp()
{
dynamic value = new CheshireCat(new Cat());
- Assert.AreEqual("Cat", value.Name);
+ value.Prototypes.Add(new Animal());
+ Assert.AreEqual("Animal", value.Name);
}
+
[Test]
public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIsNotOnTheLastModule()
{
@@ -64,11 +63,10 @@ public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIs
}
[Test]
- public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueAndIsReturnedBottomUp()
+ public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIsOnTheLastModule()
{
dynamic value = new CheshireCat(new Cat());
- value.Prototypes.Add(new Animal());
- Assert.AreEqual("Animal", value.Name);
+ Assert.AreEqual("Cat", value.Name);
}
}
-}
+}
@@ -0,0 +1,65 @@
+using System.ComponentModel;
+using NUnit.Framework;
+
+namespace Archetype.Tests
+{
+ public class NotifyPropertyChangedModule : INotifyPropertyChanged
+ {
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ public virtual void OnPropertyChanged(string propertyName)
+ {
+ PropertyChangedEventHandler handler = PropertyChanged;
+ if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+
+ public class SampleModel : DelegatingPrototype
+ {
+ private string _Name;
+
+ public SampleModel()
+ : base(new NotifyPropertyChangedModule())
+ {
+ // Or you can add it to the prototypes list after the fact
+ //Prototypes.Add(new NotifyPropertyChangedModule());
+ }
+
+ public string Name
+ {
+ get { return _Name; }
+ set
+ {
+ if (_Name == value) return;
+ _Name = value;
+ This.OnPropertyChanged("Name");
+ }
+ }
+
+ protected dynamic This { get { return this; } }
+ }
+
+ [TestFixture]
+ public class NotifyPropertyChangesModuleTests
+ {
+ [Test]
+ public void OnPropertyChangedCanBeCalledFromTheImportingClass()
+ {
+ dynamic instance = new SampleModel();
+ INotifyPropertyChanged module = instance; // fancy casting of our object will return the module
+ bool called = false;
+ string propertyName = null;
+ module.PropertyChanged += (sender, args) =>
+ {
+ called = true;
+ propertyName = args.PropertyName;
+ };
+ instance.Name = null;
+ Assert.IsFalse(called);
+ instance.Name = "Ian";
+ Assert.IsTrue(called);
+ Assert.AreEqual("Ian", instance.Name);
+ Assert.AreEqual("Name", propertyName);
+ }
+ }
+}

0 comments on commit 0a7e85d

Please sign in to comment.