using System.ComponentModel;
using SpdUp.Core;
namespace SpdUp.Tests;
///
/// Tests for ObservableObject base class.
///
public class ObservableObjectTests
{
private sealed class TestObservable : ObservableObject
{
private string _name = string.Empty;
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
private int _count;
public int Count
{
get => _count;
set => SetProperty(ref _count, value);
}
}
[Fact]
public void SetProperty_NewValue_RaisesPropertyChanged()
{
var obj = new TestObservable();
string? changedProp = null;
obj.PropertyChanged += (_, e) => changedProp = e.PropertyName;
obj.Name = "hello";
Assert.Equal("Name", changedProp);
Assert.Equal("hello", obj.Name);
}
[Fact]
public void SetProperty_SameValue_DoesNotRaisePropertyChanged()
{
var obj = new TestObservable { Name = "hello" };
bool raised = false;
obj.PropertyChanged += (_, _) => raised = true;
obj.Name = "hello";
Assert.False(raised);
}
[Fact]
public void SetProperty_ReturnsTrue_WhenValueChanged()
{
var obj = new TestObservable();
string? changedProp = null;
obj.PropertyChanged += (_, e) => changedProp = e.PropertyName;
obj.Count = 42;
Assert.Equal("Count", changedProp);
Assert.Equal(42, obj.Count);
}
[Fact]
public void SetProperty_ReturnsFalse_WhenValueUnchanged()
{
var obj = new TestObservable { Count = 5 };
bool raised = false;
obj.PropertyChanged += (_, _) => raised = true;
obj.Count = 5;
Assert.False(raised);
}
[Fact]
public void MultipleProperties_OnlyNotifyChanged()
{
var obj = new TestObservable();
var notifications = new List();
obj.PropertyChanged += (_, e) => notifications.Add(e.PropertyName!);
obj.Name = "a";
obj.Count = 1;
obj.Name = "a"; // same – no notification
obj.Count = 2;
Assert.Equal(3, notifications.Count);
Assert.Equal(["Name", "Count", "Count"], notifications);
}
}