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

84 lines
2.2 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.
#if NET8_0_OR_GREATER
using System;
using System.Runtime.CompilerServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommunityToolkit.HighPerformance.UnitTests;
[TestClass]
public class Test_NullableRefOfT
{
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_Ok()
{
int value = 1;
NullableRef<int> reference = new(ref value);
Assert.IsTrue(reference.HasValue);
Assert.IsTrue(Unsafe.AreSame(ref value, ref reference.Value));
reference.Value++;
Assert.AreEqual(2, value);
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_Null()
{
Assert.IsFalse(default(NullableRef<int>).HasValue);
Assert.IsFalse(NullableRef<int>.Null.HasValue);
Assert.IsFalse(default(NullableRef<string>).HasValue);
Assert.IsFalse(NullableRef<string>.Null.HasValue);
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_Null_Exception()
{
_ = Assert.ThrowsExactly<InvalidOperationException>(() =>
{
NullableRef<int> reference = default;
_ = reference.Value;
});
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_ImplicitRefCast()
{
int value = 42;
Ref<int> reference = new(ref value);
NullableRef<int> nullableRef = reference;
Assert.IsTrue(nullableRef.HasValue);
Assert.IsTrue(Unsafe.AreSame(ref reference.Value, ref nullableRef.Value));
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT()
{
int value = 42;
NullableRef<int> reference = new(ref value);
Assert.AreEqual(value, (int)reference);
}
[TestMethod]
public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT_Exception()
{
_ = Assert.ThrowsExactly<InvalidOperationException>(() =>
{
NullableRef<int> invalid = default;
_ = (int)invalid;
});
}
}
#endif