Skip to content

Commit

Permalink
Adding NotifyPropertyChangedModule implementation and tests. Updating…
Browse files Browse the repository at this point in the history
… readme with help to pull dependencies and build.
  • Loading branch information
idavis committed Nov 1, 2012
1 parent d05d1e7 commit 0a7e85d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Archetype
============
To build, open PowerShell console in source directory. Run chewie.ps1. Then open the solution.
1 change: 1 addition & 0 deletions src/Archetype.Tests/Archetype.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
56 changes: 27 additions & 29 deletions src/Archetype.Tests/ModuleTestsForGetMember.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -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);
}
}
}
}
65 changes: 65 additions & 0 deletions src/Archetype.Tests/NotifyPropertyChangesModuleTests.cs
Original file line number Diff line number Diff line change
@@ -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.