-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* minor cleanup - corrected some comments - updated gen code formatting (added some indentation) - change `ApppendPropertyMetadata` to `GetPropertyMetadataDeclaration` - check cancellation during code gen - added param names for clarity * more unit tests
- Loading branch information
Showing
5 changed files
with
205 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright © Ian Good | ||
|
||
using System; | ||
using System.Windows; | ||
|
||
namespace Bpz.Test | ||
{ | ||
public partial class Widget : DependencyObject | ||
{ | ||
public static readonly DependencyProperty MyBool0Property = Gen.MyBool0(true); | ||
public static readonly DependencyProperty MyBool1Property = Gen.MyBool1<bool>(); | ||
public static readonly DependencyProperty MyBool2Property = Gen.MyBool2((bool?)false); | ||
|
||
public static readonly DependencyProperty MyString0Property = Gen.MyString0("asdf"); | ||
public static readonly DependencyProperty MyString1Property = Gen.MyString1<string?>(); | ||
public static readonly DependencyProperty MyString2Property = Gen.MyString2(default(string?)); | ||
public static readonly DependencyProperty MyString3Property = Gen.MyString3("qwer"); | ||
|
||
private static void MyString0PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((Widget)d).MyString0Changed?.Invoke(d, EventArgs.Empty); | ||
} | ||
|
||
private static void MyString1PropertyChanged(Widget self, DependencyPropertyChangedEventArgs e) | ||
{ | ||
self.MyString1Changed?.Invoke(self, EventArgs.Empty); | ||
} | ||
|
||
private void OnMyString2Changed(string? oldValue, string? newValue) | ||
{ | ||
this.MyString2Changed?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
private void OnMyString3Changed(DependencyPropertyChangedEventArgs e) | ||
{ | ||
this.MyString3Changed?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
public event EventHandler? MyString0Changed; | ||
public event EventHandler? MyString1Changed; | ||
public event EventHandler? MyString2Changed; | ||
public event EventHandler? MyString3Changed; | ||
|
||
private static readonly DependencyPropertyKey MyFloat0PropertyKey = Gen.MyFloat0(3.14f); | ||
protected static readonly DependencyPropertyKey MyFloat1PropertyKey = Gen.MyFloat1<float>(); | ||
|
||
private static float CoerceMyFloat1(Widget self, float baseValue) | ||
{ | ||
return Math.Abs(baseValue); | ||
} | ||
|
||
private void OnMyFloat1Changed(float old, float @new) | ||
{ | ||
this.MyFloat1Changed?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
public void SetMyFloat1(float value) => this.MyFloat1 = value; | ||
|
||
public event EventHandler? MyFloat1Changed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright © Ian Good | ||
|
||
using NUnit.Framework; | ||
using System; | ||
|
||
namespace Bpz.Test | ||
{ | ||
/// <summary> | ||
/// Exercises basic dependency property behavior. | ||
/// This won't compile if the properties we expect weren't generated. | ||
/// </summary> | ||
public class WidgetTests | ||
{ | ||
[Test(Description = "Checks default values.")] | ||
public void ExpectDefaults() | ||
{ | ||
var w = new Widget(); | ||
|
||
Assert.AreEqual(true, w.MyBool0); | ||
Assert.AreEqual(false, w.MyBool1); | ||
Assert.AreEqual(false, w.MyBool2); | ||
|
||
Assert.AreEqual("asdf", w.MyString0); | ||
Assert.AreEqual(null, w.MyString1); | ||
Assert.AreEqual(null, w.MyString2); | ||
Assert.AreEqual("qwer", w.MyString3); | ||
|
||
Assert.AreEqual(3.14f, w.MyFloat0); | ||
Assert.AreEqual(0, w.MyFloat1); | ||
} | ||
|
||
[Test(Description = "Change-handers should raise events.")] | ||
public void ExpectEvents() | ||
{ | ||
var w = new Widget(); | ||
|
||
bool eventWasRaised; | ||
EventHandler handler = (s, e) => | ||
{ | ||
Assert.AreSame(w, s); | ||
eventWasRaised = true; | ||
}; | ||
|
||
w.MyString0Changed += handler; | ||
w.MyString1Changed += handler; | ||
w.MyString2Changed += handler; | ||
w.MyString3Changed += handler; | ||
w.MyFloat1Changed += handler; | ||
|
||
{ | ||
eventWasRaised = false; | ||
w.MyString0 = "foo"; | ||
Assert.IsTrue(eventWasRaised); | ||
} | ||
{ | ||
eventWasRaised = false; | ||
w.MyString1 = "foo"; | ||
Assert.IsTrue(eventWasRaised); | ||
} | ||
{ | ||
eventWasRaised = false; | ||
w.MyString2 = "foo"; | ||
Assert.IsTrue(eventWasRaised); | ||
} | ||
{ | ||
eventWasRaised = false; | ||
w.MyString3 = "foo"; | ||
Assert.IsTrue(eventWasRaised); | ||
} | ||
{ | ||
eventWasRaised = false; | ||
w.SetMyFloat1(123.456f); | ||
Assert.IsTrue(eventWasRaised); | ||
|
||
eventWasRaised = false; | ||
w.SetMyFloat1(123.456f); | ||
Assert.IsFalse(eventWasRaised); | ||
} | ||
} | ||
|
||
[Test(Description = "Coercion should take place.")] | ||
public void ExpectCoercion() | ||
{ | ||
var w = new Widget(); | ||
|
||
bool eventWasRaised = false; | ||
w.MyFloat1Changed += (s, e) => | ||
{ | ||
Assert.AreSame(w, s); | ||
eventWasRaised = true; | ||
}; | ||
|
||
// `Float1` coerces values using `Math.Abs`. | ||
w.SetMyFloat1(-40); | ||
Assert.AreEqual(40, w.MyFloat1); | ||
Assert.IsTrue(eventWasRaised); | ||
|
||
// The value has been coerced from negative to positive, | ||
// so assigning the positive value should not produce a "changed" event. | ||
eventWasRaised = false; | ||
w.SetMyFloat1(40); | ||
Assert.AreEqual(40, w.MyFloat1); | ||
|
||
Assert.IsFalse(eventWasRaised); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters