1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-08-20 07:49:50 +02:00
Files
.github
CommunityToolkit.Common
CommunityToolkit.Diagnostics
CommunityToolkit.HighPerformance
CommunityToolkit.Mvvm
CommunityToolkit.Mvvm.SourceGenerators
build
tests
CommunityToolkit.Common.UnitTests
CommunityToolkit.Diagnostics.UnitTests
CommunityToolkit.HighPerformance.UnitTests
Buffers
Enumerables
Extensions
Helpers
Memory
Streams
CommunityToolkit.HighPerformance.UnitTests.csproj
Test_Box{T}.cs
Test_NullableReadOnlyRef{T}.cs
Test_NullableRef{T}.cs
Test_ReadOnlyRef{T}.cs
Test_Ref{T}.cs
CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests
CommunityToolkit.Mvvm.ExternalAssembly
CommunityToolkit.Mvvm.Internals.UnitTests
CommunityToolkit.Mvvm.SourceGenerators.UnitTests
CommunityToolkit.Mvvm.UnitTests
.editorconfig
.git-blame-ignore-revs
.gitattributes
.gitignore
.runsettings
CODE_OF_CONDUCT.md
Contributing.md
Directory.Build.props
Directory.Build.targets
License.md
README.md
ThirdPartyNotices.txt
azure-pipelines.yml
dotnet Community Toolkit.sln
toolkit.snk
version.json
2021-11-22 17:55:01 +01:00

79 lines
2.1 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 NETCOREAPP3_1_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(value, 2);
}
[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]
[ExpectedException(typeof(InvalidOperationException))]
public void Test_NullableRefOfT_CreateNullableRefOfT_Null_Exception()
{
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]
[ExpectedException(typeof(InvalidOperationException))]
public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT_Exception()
{
NullableRef<int> invalid = default;
_ = (int)invalid;
}
}
#endif