Files
Sergio Pedri e88c524c3e Refactor unit tests to use expected-first Assert.AreEqual
Updated all unit tests to consistently use the expected value as the first argument in Assert.AreEqual calls. This improves readability and aligns with best practices for test assertions.
2025-11-09 18:33:21 -08:00

154 lines
4.6 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
#if !NET6_0_OR_GREATER
using System.Collections.Generic;
using System.Linq;
#endif
using System.Reflection;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable CS0618
namespace CommunityToolkit.Mvvm.UnitTests;
[TestClass]
public class Test_ObservableRecipient
{
[TestMethod]
[DataRow(typeof(StrongReferenceMessenger))]
[DataRow(typeof(WeakReferenceMessenger))]
public void Test_ObservableRecipient_Activation(Type type)
{
IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!;
SomeRecipient<int>? viewmodel = new(messenger);
Assert.IsFalse(viewmodel.IsActivatedCheck);
viewmodel.IsActive = true;
Assert.IsTrue(viewmodel.IsActivatedCheck);
Assert.IsTrue(viewmodel.CurrentMessenger.IsRegistered<SampleMessage>(viewmodel));
viewmodel.IsActive = false;
Assert.IsFalse(viewmodel.IsActivatedCheck);
Assert.IsFalse(viewmodel.CurrentMessenger.IsRegistered<SampleMessage>(viewmodel));
}
[TestMethod]
[DataRow(typeof(StrongReferenceMessenger))]
[DataRow(typeof(WeakReferenceMessenger))]
public void Test_ObservableRecipient_IsSame(Type type)
{
IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!;
SomeRecipient<int>? viewmodel = new(messenger);
Assert.AreSame(viewmodel.CurrentMessenger, messenger);
}
[TestMethod]
public void Test_ObservableRecipient_Default()
{
SomeRecipient<int>? viewmodel = new();
Assert.AreSame(viewmodel.CurrentMessenger, WeakReferenceMessenger.Default);
}
[TestMethod]
[DataRow(typeof(StrongReferenceMessenger))]
[DataRow(typeof(WeakReferenceMessenger))]
public void Test_ObservableRecipient_Injection(Type type)
{
IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!;
SomeRecipient<int>? viewmodel = new(messenger);
Assert.AreSame(viewmodel.CurrentMessenger, messenger);
}
[TestMethod]
[DataRow(typeof(StrongReferenceMessenger))]
[DataRow(typeof(WeakReferenceMessenger))]
public void Test_ObservableRecipient_Broadcast(Type type)
{
IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!;
SomeRecipient<int>? viewmodel = new(messenger);
PropertyChangedMessage<int>? message = null;
messenger.Register<PropertyChangedMessage<int>>(messenger, (r, m) => message = m);
viewmodel.Data = 42;
Assert.IsNotNull(message);
Assert.AreSame(message.Sender, viewmodel);
Assert.AreEqual(0, message.OldValue);
Assert.AreEqual(42, message.NewValue);
Assert.AreEqual(nameof(SomeRecipient<int>.Data), message.PropertyName);
}
[TestMethod]
public void Test_IRecipient_VerifyTrimmingAnnotation()
{
#if NET6_0_OR_GREATER
System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute? attribute =
typeof(Messaging.__Internals.__IMessengerExtensions)
.GetCustomAttribute<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>();
Assert.IsNotNull(attribute);
Assert.AreEqual(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods, attribute.MemberTypes);
#else
IEnumerable<Attribute> attributes = typeof(Messaging.__Internals.__IMessengerExtensions).GetCustomAttributes();
Assert.IsFalse(attributes.Any(static a => a.GetType().Name is "DynamicallyAccessedMembersAttribute"));
#endif
}
public class SomeRecipient<T> : ObservableRecipient
{
public SomeRecipient()
{
}
public SomeRecipient(IMessenger messenger)
: base(messenger)
{
}
public IMessenger CurrentMessenger => Messenger;
private T? data;
public T? Data
{
get => this.data;
set => SetProperty(ref this.data, value, true);
}
public bool IsActivatedCheck { get; private set; }
protected override void OnActivated()
{
IsActivatedCheck = true;
Messenger.Register<SampleMessage>(this, (r, m) => { });
}
protected override void OnDeactivated()
{
base.OnDeactivated();
IsActivatedCheck = false;
}
}
public class SampleMessage
{
}
}