Skip to content

Commit

Permalink
Merge pull request #13 from BoBoBaSs84/feature/add-unit-tests
Browse files Browse the repository at this point in the history
feat: add unit tests
  • Loading branch information
BoBoBaSs84 authored Oct 21, 2023
2 parents 3f4a7f1 + 0160ae2 commit a66ac99
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>1</VersionPatch>
<VersionMinor>1</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="$(Configuration.Equals('Debug'))">Development</VersionSuffix>
</PropertyGroup>
Expand Down
12 changes: 6 additions & 6 deletions src/BB84.Notifications/BindableProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public T Value
get => _value;
set
{
if (EqualityComparer<T>.Default.Equals(_value, value))
return;

Changing?.Invoke(this, new BindablePropertyChangingEventArgs<T>(_value));
_value = value;
Changed?.Invoke(this, new BindablePropertyChangedEventArgs<T>(value));
if (!EqualityComparer<T>.Default.Equals(_value, value))
{
Changing?.Invoke(this, new BindablePropertyChangingEventArgs<T>(_value));
_value = value;
Changed?.Invoke(this, new BindablePropertyChangedEventArgs<T>(value));
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/BB84.Notifications/Components/CollectionChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ public sealed class CollectionChangedEventArgs : EventArgs
/// <summary>
/// Initializes a instance of the collection changed event args class.
/// </summary>
/// <param name="type">The event type that causes the change.</param>
/// <param name="action">The action that causes the change.</param>
/// <param name="item">The item that is changed.</param>
public CollectionChangedEventArgs(CollectionEventType type, object? item)
public CollectionChangedEventArgs(CollectionChangeAction action, object? item)
{
Event = type;
Action = action;
Item = item;
}

/// <summary>
/// The event type that causes the change.
/// The action that causes the change.
/// </summary>
public CollectionEventType Event { get; }
public CollectionChangeAction Action { get; }

/// <summary>
/// The item that is changed.
Expand Down
10 changes: 5 additions & 5 deletions src/BB84.Notifications/Components/CollectionChangingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ public sealed class CollectionChangingEventArgs : EventArgs
/// <summary>
/// Initializes a instance of the collection changing event args class.
/// </summary>
/// <param name="type">The event type that causes the change.</param>
/// <param name="action">The action that causes the change.</param>
/// <param name="item">The item that is changing.</param>
public CollectionChangingEventArgs(CollectionEventType type, object? item)
public CollectionChangingEventArgs(CollectionChangeAction action, object? item)
{
Event = type;
Action = action;
Item = item;
}

/// <summary>
/// The event type that causes the change.
/// The action that causes the change.
/// </summary>
public CollectionEventType Event { get; }
public CollectionChangeAction Action { get; }

/// <summary>
/// The item that is changing.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace BB84.Notifications.Enumerators;

/// <summary>
/// The collection event type enumerator.
/// The collection change action enumerator.
/// </summary>
public enum CollectionEventType
public enum CollectionChangeAction
{
/// <summary>
/// An item was added to the collection.
Expand Down
12 changes: 6 additions & 6 deletions src/BB84.Notifications/NotifyCollectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public abstract class NotifyCollectionBase : NotifyPropertyBase, INotifyCollecti
/// <summary>
/// Raises the <see cref="CollectionChanged"/> event.
/// </summary>
/// <param name="eventType">The event that causes the change.</param>
/// <param name="action">The action that causes the change.</param>
/// <param name="value">The item that is changed.</param>
protected void RaiseCollectionChanged(CollectionEventType eventType, object? value = null)
=> CollectionChanged?.Invoke(this, new(eventType, value));
protected void RaiseCollectionChanged(CollectionChangeAction action, object? value = null)
=> CollectionChanged?.Invoke(this, new(action, value));

/// <summary>
/// Raises the <see cref="CollectionChanging"/> event.
/// </summary>
/// <param name="eventType">The event that causes the change.</param>
/// <param name="action">The action that causes the change.</param>
/// <param name="value">The item that is changing.</param>
protected void RaiseCollectionChanging(CollectionEventType eventType, object? value = null)
=> CollectionChanging?.Invoke(this, new(eventType, value));
protected void RaiseCollectionChanging(CollectionChangeAction action, object? value = null)
=> CollectionChanging?.Invoke(this, new(action, value));
}
17 changes: 17 additions & 0 deletions tests/BB84.NotificationsTests/BindablePropertyTests.Changed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BB84.NotificationsTests;

public sealed partial class BindablePropertyTests
{
[TestMethod]
public void Changed()
{
int changedValue = default;
TestClass testClass = new(1);
testClass.BindableProperty.Changed += (sender, e) => changedValue = e.Value;

testClass.BindableProperty.Value = 2;

Assert.AreEqual(2, changedValue);
Assert.AreEqual(2, testClass.BindableProperty.Value);
}
}
17 changes: 17 additions & 0 deletions tests/BB84.NotificationsTests/BindablePropertyTests.Changing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BB84.NotificationsTests;

public sealed partial class BindablePropertyTests
{
[TestMethod]
public void Changing()
{
int changingValue = default;
TestClass testClass = new(1);
testClass.BindableProperty.Changing += (sender, e) => changingValue = e.Value;

testClass.BindableProperty.Value = 2;

Assert.AreEqual(1, changingValue);
Assert.AreEqual(2, testClass.BindableProperty.Value);
}
}
16 changes: 16 additions & 0 deletions tests/BB84.NotificationsTests/BindablePropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using BB84.Notifications;
using BB84.Notifications.Interfaces;

namespace BB84.NotificationsTests;

[TestClass]
public sealed partial class BindablePropertyTests
{
private sealed class TestClass
{
public TestClass(int bindableProperty)
=> BindableProperty = new BindableProperty<int>(bindableProperty);

public IBindableProperty<int> BindableProperty { get; set; }
}
}
20 changes: 20 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.Changed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Changed()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new();
strings.CollectionChanged += (sender, e) => { item = (string)e.Item!; action = e.Action; };

strings.Add("UnitTest");

Assert.AreEqual(CollectionChangeAction.Add, action);
Assert.AreEqual("UnitTest", item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

public sealed partial class NotifyCollectionBaseTests
{
[TestMethod]
public void Changing()
{
CollectionChangeAction action = default!;
string item = string.Empty;
MyCollection strings = new();
strings.CollectionChanging += (sender, e) => { item = (string)e.Item!; action = e.Action; };

strings.Add("UnitTest");

Assert.AreEqual(CollectionChangeAction.Add, action);
Assert.AreEqual("UnitTest", item);
}
}
47 changes: 47 additions & 0 deletions tests/BB84.NotificationsTests/NotifyCollectionBaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.ObjectModel;

using BB84.Notifications;
using BB84.Notifications.Enumerators;

namespace BB84.NotificationsTests;

[TestClass]
public sealed partial class NotifyCollectionBaseTests
{
private class MyCollection : NotifyCollectionBase, ICollection<string>
{
private readonly Collection<string> _collection;

public MyCollection()
=> _collection = new Collection<string>();

public int Count => _collection.Count;
public bool IsReadOnly => false;

public void Add(string item)
{
RaiseCollectionChanging(CollectionChangeAction.Add, item);
_collection.Add(item);
RaiseCollectionChanged(CollectionChangeAction.Add, item);
}

public void Clear()
=> _collection.Clear();

public bool Contains(string item)
=> _collection.Contains(item);

public void CopyTo(string[] array, int arrayIndex)
=> _collection.CopyTo(array, arrayIndex);

public IEnumerator<string> GetEnumerator()
=> _collection.GetEnumerator();

public bool Remove(string item)
=> _collection.Remove(item);

IEnumerator IEnumerable.GetEnumerator()
=> _collection.GetEnumerator();
}
}
19 changes: 19 additions & 0 deletions tests/BB84.NotificationsTests/NotifyPropertyBaseTests.Changed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BB84.NotificationsTests;

public sealed partial class NotifyPropertyBaseTests
{
[TestMethod]
public void Changed()
{
string changedProperty = string.Empty;
int changedValue = default;
TestClass testClass = new();
testClass.PropertyChanged += (sender, e) => { changedValue = (int)e.Value!; changedProperty = e.Name; };

testClass.Property = 1;

Assert.AreEqual(nameof(testClass.Property), changedProperty);
Assert.AreEqual(1, changedValue);
Assert.AreEqual(1, testClass.Property);
}
}
19 changes: 19 additions & 0 deletions tests/BB84.NotificationsTests/NotifyPropertyBaseTests.Changing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BB84.NotificationsTests;

public sealed partial class NotifyPropertyBaseTests
{
[TestMethod]
public void Changing()
{
string changingProperty = string.Empty;
int changingValue = default;
TestClass testClass = new() { Property = 1 };
testClass.PropertyChanging += (sender, e) => { changingValue = (int)e.Value!; changingProperty = e.Name; };

testClass.Property = 2;

Assert.AreEqual(nameof(testClass.Property), changingProperty);
Assert.AreEqual(1, changingValue);
Assert.AreNotEqual(1, testClass.Property);
}
}
18 changes: 18 additions & 0 deletions tests/BB84.NotificationsTests/NotifyPropertyBaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using BB84.Notifications;

namespace BB84.NotificationsTests;

[TestClass]
public sealed partial class NotifyPropertyBaseTests
{
private sealed class TestClass : NotifyPropertyBase
{
private int _property;

public int Property
{
get => _property;
set => SetProperty(ref _property, value);
}
}
}

0 comments on commit a66ac99

Please sign in to comment.