Skip to content

Commit

Permalink
Restored project after bad merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathtone committed Apr 12, 2016
1 parent 471d52e commit 1951615
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 123 deletions.
70 changes: 20 additions & 50 deletions Mathtone.MIST.Builder/NotificationWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool ProcessType(TypeDefinition typeDef) {
//Search for a NotifyAttribute
if (notifierAttr != null) {
var mode = NotificationMode.Explicit;
//if (notifierAttributeType == null) notifierAttributeType = mdResolver.Resolve(notifierAttr.AttributeType);

//Locate the notification target method.
var notifyTarget = GetNotifyTarget(typeDef);

Expand All @@ -95,16 +95,21 @@ bool ProcessType(TypeDefinition typeDef) {
foreach (var propDef in typeDef.Properties) {

var propNames = GetNotifyPropertyNames(propDef);

if (!propNames.Any() && mode == NotificationMode.Implicit && propDef.GetMethod.IsPublic) {
propNames = new[] { propDef.Name };
}
if (propNames != null) {
InsertNotificationsIntoProperty(propDef, notifyTarget, propNames);
rtn = true;
if (!propDef.CustomAttributes.Any(a => a.AttributeType.FullName == "Mathtone.MIST.SuppressNotifyAttribute")) {
if (!propNames.Any() && mode == NotificationMode.Implicit && propDef.GetMethod.IsPublic) {
propNames = new[] { propDef.Name };
}
if (propNames != null) {
InsertNotificationsIntoProperty(propDef, notifyTarget, propNames);
rtn = true;
}
}
}
}

foreach (var type in typeDef.NestedTypes) {
ProcessType(type);
}
return rtn;
}

Expand Down Expand Up @@ -177,11 +182,8 @@ IEnumerable<string> GetNotifyPropertyNames(PropertyDefinition propDef) {
/// <param name="notifyTarget">The notify target.</param>
/// <param name="notifyPropertyNames">The notify property names.</param>
void InsertNotificationsIntoProperty(PropertyDefinition propDef, MethodReference notifyTarget, IEnumerable<string> notifyPropertyNames) {
if (propDef.SetMethod == null)
return;

var msil = propDef.SetMethod.Body.GetILProcessor();
//Should produce something like the following.
//Should produce something like the following:
/*
.method public hidebysig specialname instance void
.set_SomeProperty(string 'value') cil managed
Expand All @@ -196,6 +198,12 @@ .maxstack 8
} // end of method TestNotifier::set_SomeProperty
*/

if (propDef.SetMethod == null)
return;
else if (propDef.SetMethod.Body == null) {
throw new InvalidOperationException("NotifyAttribute cannot be set on abstract properties");
}
var msil = propDef.SetMethod.Body.GetILProcessor();
msil.InsertBefore(propDef.SetMethod.Body.Instructions[0], msil.Create(OpCodes.Nop));
foreach (var notifyPropertyName in notifyPropertyNames) {
var ldarg0 = msil.Create(OpCodes.Ldarg_0);
Expand All @@ -209,42 +217,4 @@ .maxstack 8
}

}

/// <summary>
/// Class NotificationWeaverBuildTask.
/// </summary>
/// <example>
/// place the following XML in the project file. The directorey containing Mathtone.MIST.Builder.dll should also contain Mathtone.MIST.dll, Mono.Cecil.dll and Mono.Cecil.pdb.dll
/// <UsingTask TaskName = "Mathtone.MIST.NotificationWeaverBuildTask"
/// AssemblyFile="...path to "
/// />
/// <Target Name = "AfterBuild" >
/// <NotificationWeaverBuildTask TargetPath="$(TargetPath)" DebugMode="True"/>
/// </Target>
/// </example>
public class NotificationWeaverBuildTask : Task {

/// <summary>
/// Gets or sets the target path.
/// </summary>
/// <value>The target path.</value>
[Required]
public string TargetPath { get; set; }

/// <summary>
/// Gets or sets a value indicating whether [debug mode].
/// </summary>
/// <value><c>true</c> if [debug mode]; otherwise, <c>false</c>.</value>
[Required]
public bool DebugMode { get; set; }

/// <summary>
/// When overridden in a derived class, executes the task.
/// </summary>
/// <returns>true if the task successfully executed; otherwise, false.</returns>
public override bool Execute() {
new NotificationWeaver(TargetPath).InsertNotifications(DebugMode);
return true;
}
}
}
36 changes: 14 additions & 22 deletions Mathtone.MIST.TestNotifier/TestNotifier.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
namespace Mathtone.MIST.Tests {


[Notifier]
[Notifier(NotificationMode.Implicit)]
public class TestNotifier : NotifierBase {

[Notify]
public string SomeProperty { get; set; }
[Notifier(NotificationMode.Implicit)]
public class NestedClass : NotifierBase {
public int Change1 { get; set; }

[Notify("SomeProperty", "AllProperties")]
public string AllProperties { get; set; }
}
public int Change2 { get; set; }
}

[Notifier(NotificationMode.Implicit)]
public class TestNotifier2 : NotifierBase {
public string Change1 { get; set; }

public string SomeProperty { get; set; }
public string Change2 { get; set; }

[Notify("SomeProperty", "AllProperties")]
public string AllProperties { get; set; }
}
public string ReadOnly => Change1 + Change2;

[Notifier(NotificationMode.Implicit)]
public class TestNotifier3 : NotifierBase {
public NestedClass Nested { get; } = new NestedClass();

public string Property1 { get; set; }
[SuppressNotify]
public string ChangeNone { get; set; }

public string Property2 { get { return Property1; } }
[Notify("Change1","Change2","ReadOnly")]
public string ChangeAll { get; set; }

int property3;
public int Property3 {
get { return property3; }
set { property3 = value; }
}
}
}
76 changes: 25 additions & 51 deletions Mathtone.MIST.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -27,66 +28,39 @@ public void InitializeTest() {
}

[TestMethod]
public void LoadAndTestNotifier1() {
WriteLine("Testing Notifier 1");
public void TestNotifier() {
WriteLine("Testing Notifier...");
var notifier = new TestNotifier();
var changedProps = new List<string>();

notifier.PropertyChanged += (a, b) => {
PropertyChangedEventHandler handler = (a, b) => {
changedProps.Add(b.PropertyName);
};

var change = new[] { "SomeProperty"};
notifier.SomeProperty = "CHANGE1";
Assert.IsTrue(changedProps.Intersect(change).Count() == change.Length);
changedProps.Clear();
notifier.PropertyChanged += handler;
notifier.Nested.PropertyChanged += handler;

change = new[] { "SomeProperty", "AllProperties" };
notifier.AllProperties = "CHANGE2";
Assert.IsTrue(changedProps.Intersect(change).Count() == change.Length);
changedProps.Clear();

}
[TestMethod]
public void LoadAndTestNotifier2() {
WriteLine("Testing Notifier 2");
var notifier = new TestNotifier2();
var changedProps = new List<string>();

notifier.PropertyChanged += (a, b) => {
changedProps.Add(b.PropertyName);
};

var change = new[] { "SomeProperty" };
notifier.SomeProperty = "CHANGE1";
Assert.IsTrue(changedProps.Intersect(change).Count() == change.Length);
changedProps.Clear();

change = new[] { "SomeProperty", "AllProperties" };
notifier.AllProperties = "CHANGE2";
Assert.IsTrue(changedProps.Intersect(change).Count() == change.Length);
changedProps.Clear();

}
[TestMethod]
public void LoadAndTestNotifier3() {
WriteLine("Testing Notifier 3");
var notifier = new TestNotifier3();
var changedProps = new List<string>();

notifier.PropertyChanged += (a, b) => {
changedProps.Add(b.PropertyName);
};
notifier.Change1 = "";
notifier.Change2 = "";
notifier.ChangeAll = "";
notifier.ChangeNone = "";
notifier.Nested.Change1 = 1;
notifier.Nested.Change2 = 2;

var change = new[] { "Property1" };
notifier.Property1= "CHANGE1";
Assert.IsTrue(changedProps.Intersect(change).Count() == change.Length);
changedProps.Clear();
Assert.IsTrue(changedProps.SequenceEqual(
new[] {
"Change1",
"Change2",
"Change1",
"Change2",
"ReadOnly",
"Change1",
"Change2"
}
));

change = new[] { "Property3" };
notifier.Property3 = 3;
Assert.IsTrue(changedProps.Intersect(change).Count() == change.Length);
changedProps.Clear();
notifier.PropertyChanged -= handler;
notifier.Nested.PropertyChanged -= handler;
}
}
}

0 comments on commit 1951615

Please sign in to comment.