Skip to content

Commit 0a7e85d

Browse files
committed
Adding NotifyPropertyChangedModule implementation and tests. Updating readme with help to pull dependencies and build.
1 parent d05d1e7 commit 0a7e85d

File tree

4 files changed

+94
-29
lines changed

4 files changed

+94
-29
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Archetype
22
============
3+
To build, open PowerShell console in source directory. Run chewie.ps1. Then open the solution.

src/Archetype.Tests/Archetype.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<ItemGroup>
4141
<Compile Include="Constants.cs" />
4242
<Compile Include="ModuleTestsForGetMember.cs" />
43+
<Compile Include="NotifyPropertyChangesModuleTests.cs" />
4344
<Compile Include="PrototypalObjectTestsExpandoObjectPrototypes.cs" />
4445
<Compile Include="PrototypalObjectTestsInheritanceOrder.cs" />
4546
<Compile Include="StaticTests\StaticMethodTests.cs" />
+27-29
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using NUnit.Framework;
1+
using NUnit.Framework;
52

63
namespace Archetype.Tests
74
{
8-
public class NotifyPropertyChangedModule : INotifyPropertyChanged
5+
public class Animal
96
{
10-
public event PropertyChangedEventHandler PropertyChanged;
11-
12-
protected virtual void OnPropertyChanged(string propertyName)
7+
public virtual string Name
138
{
14-
PropertyChangedEventHandler handler = PropertyChanged;
15-
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
9+
get { return GetType().Name; }
1610
}
1711
}
1812

19-
public class Animal
20-
{
21-
public virtual string Name { get { return GetType().Name; } }
22-
}
23-
2413
public class Cat : Animal
2514
{
26-
public string Noise { get { return "Meow"; } }
15+
public string Noise
16+
{
17+
get { return "Meow"; }
18+
}
2719
}
2820

2921
public class CheshireCat : DelegatingPrototype
3022
{
3123
public CheshireCat(Cat cat = null) : base(cat)
32-
{
24+
{
3325
}
3426

35-
public string Action { get { return "Grin"; } }
27+
public string Action
28+
{
29+
get { return "Grin"; }
30+
}
3631
}
3732

3833
[TestFixture]
3934
public class ModuleTestsForGetMember
4035
{
41-
[Test]
42-
public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValueWhenThereIsNoPrototype()
36+
[Test]
37+
public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValue()
4338
{
44-
dynamic value = new CheshireCat();
39+
dynamic value = new CheshireCat(new Cat());
4540
Assert.AreEqual("Grin", value.Action);
4641
}
42+
4743
[Test]
48-
public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValue()
44+
public void GettingAPropertyThatIsDefinedInTheRootObjectReturnsItsValueWhenThereIsNoPrototype()
4945
{
50-
dynamic value = new CheshireCat(new Cat());
46+
dynamic value = new CheshireCat();
5147
Assert.AreEqual("Grin", value.Action);
5248
}
49+
5350
[Test]
54-
public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIsOnTheLastModule()
51+
public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueAndIsReturnedBottomUp()
5552
{
5653
dynamic value = new CheshireCat(new Cat());
57-
Assert.AreEqual("Cat", value.Name);
54+
value.Prototypes.Add(new Animal());
55+
Assert.AreEqual("Animal", value.Name);
5856
}
57+
5958
[Test]
6059
public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIsNotOnTheLastModule()
6160
{
@@ -64,11 +63,10 @@ public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIs
6463
}
6564

6665
[Test]
67-
public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueAndIsReturnedBottomUp()
66+
public void GettingAPropertyThatIsNotDefinedInTheRootObjectReturnsItsValueIfItIsOnTheLastModule()
6867
{
6968
dynamic value = new CheshireCat(new Cat());
70-
value.Prototypes.Add(new Animal());
71-
Assert.AreEqual("Animal", value.Name);
69+
Assert.AreEqual("Cat", value.Name);
7270
}
7371
}
74-
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.ComponentModel;
2+
using NUnit.Framework;
3+
4+
namespace Archetype.Tests
5+
{
6+
public class NotifyPropertyChangedModule : INotifyPropertyChanged
7+
{
8+
public event PropertyChangedEventHandler PropertyChanged;
9+
10+
public virtual void OnPropertyChanged(string propertyName)
11+
{
12+
PropertyChangedEventHandler handler = PropertyChanged;
13+
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
14+
}
15+
}
16+
17+
public class SampleModel : DelegatingPrototype
18+
{
19+
private string _Name;
20+
21+
public SampleModel()
22+
: base(new NotifyPropertyChangedModule())
23+
{
24+
// Or you can add it to the prototypes list after the fact
25+
//Prototypes.Add(new NotifyPropertyChangedModule());
26+
}
27+
28+
public string Name
29+
{
30+
get { return _Name; }
31+
set
32+
{
33+
if (_Name == value) return;
34+
_Name = value;
35+
This.OnPropertyChanged("Name");
36+
}
37+
}
38+
39+
protected dynamic This { get { return this; } }
40+
}
41+
42+
[TestFixture]
43+
public class NotifyPropertyChangesModuleTests
44+
{
45+
[Test]
46+
public void OnPropertyChangedCanBeCalledFromTheImportingClass()
47+
{
48+
dynamic instance = new SampleModel();
49+
INotifyPropertyChanged module = instance; // fancy casting of our object will return the module
50+
bool called = false;
51+
string propertyName = null;
52+
module.PropertyChanged += (sender, args) =>
53+
{
54+
called = true;
55+
propertyName = args.PropertyName;
56+
};
57+
instance.Name = null;
58+
Assert.IsFalse(called);
59+
instance.Name = "Ian";
60+
Assert.IsTrue(called);
61+
Assert.AreEqual("Ian", instance.Name);
62+
Assert.AreEqual("Name", propertyName);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)